#include <iostream> #include <vector> #include <utility> using namespace std; void input(int & n, int & m, vector<pair<int,int>> & orders) { cin >> n >> m; orders.resize(m, {0,0}); for(int i = 0; i < m; i++) { cin >> orders[i].first >> orders[i].second; orders[i].first--; orders[i].second--; } } void get_results(int const ord_ind, int const n, int const m, vector<pair<int,int>> const & orders, vector<char> & states, vector<char> & results) { if(ord_ind == m) { int first_ready = -1; int last_ready = -1; for(int i = 0; i < n; i++) { if(states[i] == 1) { if(first_ready == -1) { first_ready = i; } last_ready = i; } } if(first_ready != -1) { for(int i = first_ready; i <= last_ready; i++) { if(states[i] == 0) { return; } } int first_unready_before = -1; int first_unready_after = n; for(int i = first_ready; i >= 0; i--) { if(states[i] == 0) { first_unready_before = i; break; } } for(int i = last_ready; i < n; i++) { if(states[i] == 0) { first_unready_after = i; break; } } for(int i = 1; i <= n; i++) { if(last_ready - first_ready + 1 > i) { continue; } if(first_unready_after - first_unready_before - 1 < i) { break; } results[i-1] = 1 & (results[i-1] + ((first_ready < first_unready_after - i) ? first_ready : (first_unready_after - i)) - ((last_ready - i > first_unready_before) ? (last_ready - i) : first_unready_before)); } } else { int last_unready = -1; int last_quest = -2; for(int i = 0; i < n; i++) { if(states[i] == 0) { if(last_quest == i-1) { for(int j = 1; j <= last_quest - last_unready; j++) { results[j-1] = 1 & (results[j-1] + last_quest - last_unready - j + 1); } } last_unready = i; } else { last_quest = i; } } if(last_quest == n-1) { for(int j = 1; j <= last_quest - last_unready; j++) { results[j-1] = 1 & (results[j-1] + last_quest - last_unready - j + 1); } } } return; } int const a = orders[ord_ind].first; int const b = orders[ord_ind].second; switch(states[a]) { case -1: switch(states[b]) { case -1: states[a] = 0; states[b] = 0; get_results(ord_ind+1, n, m, orders, states, results); states[a] = 1; states[b] = 1; get_results(ord_ind+1, n, m, orders, states, results); states[a] = -1; states[b] = -1; break; case 0: states[a] = 0; states[b] = -1; get_results(ord_ind+1, n, m, orders, states, results); states[a] = -1; states[b] = 0; break; case 1: get_results(ord_ind+1, n, m, orders, states, results); break; } break; case 0: switch(states[b]) { case -1: get_results(ord_ind+1, n, m, orders, states, results); break; case 0: get_results(ord_ind+1, n, m, orders, states, results); break; case 1: get_results(ord_ind+1, n, m, orders, states, results); break; } break; case 1: switch(states[b]) { case -1: states[a] = -1; states[b] = 1; get_results(ord_ind+1, n, m, orders, states, results); states[a] = 1; states[b] = -1; break; case 0: states[a] = 0; states[b] = 1; get_results(ord_ind+1, n, m, orders, states, results); states[a] = 1; states[b] = 0; break; case 1: get_results(ord_ind+1, n, m, orders, states, results); break; } break; } } void solve() { int n, m; vector<pair<int,int>> orders; input(n, m, orders); vector<char> states(n, -1); vector<char> results(n, 0); get_results(0, n, m, orders, states, results); for(int i = 0; i < n; i++) { cout << (int)results[i] << " "; } cout << "\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); solve(); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | #include <iostream> #include <vector> #include <utility> using namespace std; void input(int & n, int & m, vector<pair<int,int>> & orders) { cin >> n >> m; orders.resize(m, {0,0}); for(int i = 0; i < m; i++) { cin >> orders[i].first >> orders[i].second; orders[i].first--; orders[i].second--; } } void get_results(int const ord_ind, int const n, int const m, vector<pair<int,int>> const & orders, vector<char> & states, vector<char> & results) { if(ord_ind == m) { int first_ready = -1; int last_ready = -1; for(int i = 0; i < n; i++) { if(states[i] == 1) { if(first_ready == -1) { first_ready = i; } last_ready = i; } } if(first_ready != -1) { for(int i = first_ready; i <= last_ready; i++) { if(states[i] == 0) { return; } } int first_unready_before = -1; int first_unready_after = n; for(int i = first_ready; i >= 0; i--) { if(states[i] == 0) { first_unready_before = i; break; } } for(int i = last_ready; i < n; i++) { if(states[i] == 0) { first_unready_after = i; break; } } for(int i = 1; i <= n; i++) { if(last_ready - first_ready + 1 > i) { continue; } if(first_unready_after - first_unready_before - 1 < i) { break; } results[i-1] = 1 & (results[i-1] + ((first_ready < first_unready_after - i) ? first_ready : (first_unready_after - i)) - ((last_ready - i > first_unready_before) ? (last_ready - i) : first_unready_before)); } } else { int last_unready = -1; int last_quest = -2; for(int i = 0; i < n; i++) { if(states[i] == 0) { if(last_quest == i-1) { for(int j = 1; j <= last_quest - last_unready; j++) { results[j-1] = 1 & (results[j-1] + last_quest - last_unready - j + 1); } } last_unready = i; } else { last_quest = i; } } if(last_quest == n-1) { for(int j = 1; j <= last_quest - last_unready; j++) { results[j-1] = 1 & (results[j-1] + last_quest - last_unready - j + 1); } } } return; } int const a = orders[ord_ind].first; int const b = orders[ord_ind].second; switch(states[a]) { case -1: switch(states[b]) { case -1: states[a] = 0; states[b] = 0; get_results(ord_ind+1, n, m, orders, states, results); states[a] = 1; states[b] = 1; get_results(ord_ind+1, n, m, orders, states, results); states[a] = -1; states[b] = -1; break; case 0: states[a] = 0; states[b] = -1; get_results(ord_ind+1, n, m, orders, states, results); states[a] = -1; states[b] = 0; break; case 1: get_results(ord_ind+1, n, m, orders, states, results); break; } break; case 0: switch(states[b]) { case -1: get_results(ord_ind+1, n, m, orders, states, results); break; case 0: get_results(ord_ind+1, n, m, orders, states, results); break; case 1: get_results(ord_ind+1, n, m, orders, states, results); break; } break; case 1: switch(states[b]) { case -1: states[a] = -1; states[b] = 1; get_results(ord_ind+1, n, m, orders, states, results); states[a] = 1; states[b] = -1; break; case 0: states[a] = 0; states[b] = 1; get_results(ord_ind+1, n, m, orders, states, results); states[a] = 1; states[b] = 0; break; case 1: get_results(ord_ind+1, n, m, orders, states, results); break; } break; } } void solve() { int n, m; vector<pair<int,int>> orders; input(n, m, orders); vector<char> states(n, -1); vector<char> results(n, 0); get_results(0, n, m, orders, states, results); for(int i = 0; i < n; i++) { cout << (int)results[i] << " "; } cout << "\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); solve(); return 0; } |