// [4B] Desant 3, Kamil Debowski #include <bits/stdc++.h> using namespace std; int main() { int n, mm; cin >> n >> mm; assert(n <= 30); vector<pair<int,int>> e(mm); for (auto& p : e) { cin >> p.first >> p.second; --p.first; --p.second; p.first = 1 << p.first; p.second = 1 << p.second; } vector<bool> answer(n+1); for (int mask = 0; mask < (1 << n); mask++) { if (bool(mask & e[0].first) == bool(mask & e[0].second)) { int m = mask; for (const pair<int,int>& p : e) { if ((m & p.first) && !(m & p.second)) { m ^= p.first ^ p.second; } } for (int i = 0; i < n; i++) { if (m & (1 << i)) { int block = 1; while (i + 1 < n && (m & (1 << (i + 1)))) { block++; i++; } for (int j = i + 1; j < n; j++) { if (m & (1 << j)) { block = 0; break; } } if (block) { answer[block] = !answer[block]; } break; } } } } for (int i = 1; i <= n; i++) { cout << int(answer[i]) << " "; } cout << endl; } /* #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " // debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ")"; return dd; } struct Group { set<int> nodes; set<long long> masks; void merge(const Group& a, const Group& b, int i, int j) { assert(a.nodes != b.nodes); nodes = a.nodes; nodes.insert(b.nodes.begin(), b.nodes.end()); for (long long m : a.masks) { for (long long m2 : b.masks) { long long M = m + m2; if ((M & (1LL << i)) && !(M & (1LL << j))) { M ^= (1LL << i) ^ (1LL << j); } if (masks.count(M)) { masks.erase(M); } else { masks.insert(M); } } } } }; map<int,int> big; void maxi(int& a, int b) { if (b > a) { a = b; cerr << endl; for (pair<int,int> p : big) { cerr << p.first << ": " << p.second << endl; } } } Group merge(const Group& a, const Group& b, int i, int j) { Group total; total.merge(a, b, i, j); // debug() << imie(total.nodes) imie(total.masks.size()); maxi(big[total.nodes.size()], total.masks.size()); return total; } void test(int n, int m, vector<pair<int,int>> edges) { vector<Group> groups; vector<int> g(n+1); for (int i = 1; i <= n; i++) { groups.push_back(Group{{i}, {0LL, 1LL<<i}}); g[i] = i - 1; } for (pair<int,int> edge : edges) { int i = edge.first; int j = edge.second; if (g[i] != g[j]) { groups[min(g[i],g[j])] = merge(groups[g[i]], groups[g[j]], i, j); groups.erase(groups.begin() + max(g[i], g[j])); for (int a = 0; a < (int) groups.size(); a++) { for (int node : groups[a].nodes) { g[node] = a; } } } else { // TODO, inside } } } int r(int a, int b) { return a + rand() % (b - a + 1); } int main() { for (int rep = 0; rep < 1000000; rep++) { int n = r(2, 15); int m = r(1, 70); m = n - 1; vector<pair<int,int>> edges; for (int i = 2; i <= n; i++) { int a = r(1, n); a = i-1; int b = i; // if (rand() % 2) swap(a, b); // swap(a, b); edges.emplace_back(a, b); } // vector<pair<int,int>> edges(m); // for (pair<int,int>& e : edges) { // do { // e.first = r(1, n); // e.second = r(1, n); // } while (e.first == e.second); // } test(n, m, edges); } } */
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 176 177 178 179 180 181 182 183 184 | // [4B] Desant 3, Kamil Debowski #include <bits/stdc++.h> using namespace std; int main() { int n, mm; cin >> n >> mm; assert(n <= 30); vector<pair<int,int>> e(mm); for (auto& p : e) { cin >> p.first >> p.second; --p.first; --p.second; p.first = 1 << p.first; p.second = 1 << p.second; } vector<bool> answer(n+1); for (int mask = 0; mask < (1 << n); mask++) { if (bool(mask & e[0].first) == bool(mask & e[0].second)) { int m = mask; for (const pair<int,int>& p : e) { if ((m & p.first) && !(m & p.second)) { m ^= p.first ^ p.second; } } for (int i = 0; i < n; i++) { if (m & (1 << i)) { int block = 1; while (i + 1 < n && (m & (1 << (i + 1)))) { block++; i++; } for (int j = i + 1; j < n; j++) { if (m & (1 << j)) { block = 0; break; } } if (block) { answer[block] = !answer[block]; } break; } } } } for (int i = 1; i <= n; i++) { cout << int(answer[i]) << " "; } cout << endl; } /* #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " // debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ")"; return dd; } struct Group { set<int> nodes; set<long long> masks; void merge(const Group& a, const Group& b, int i, int j) { assert(a.nodes != b.nodes); nodes = a.nodes; nodes.insert(b.nodes.begin(), b.nodes.end()); for (long long m : a.masks) { for (long long m2 : b.masks) { long long M = m + m2; if ((M & (1LL << i)) && !(M & (1LL << j))) { M ^= (1LL << i) ^ (1LL << j); } if (masks.count(M)) { masks.erase(M); } else { masks.insert(M); } } } } }; map<int,int> big; void maxi(int& a, int b) { if (b > a) { a = b; cerr << endl; for (pair<int,int> p : big) { cerr << p.first << ": " << p.second << endl; } } } Group merge(const Group& a, const Group& b, int i, int j) { Group total; total.merge(a, b, i, j); // debug() << imie(total.nodes) imie(total.masks.size()); maxi(big[total.nodes.size()], total.masks.size()); return total; } void test(int n, int m, vector<pair<int,int>> edges) { vector<Group> groups; vector<int> g(n+1); for (int i = 1; i <= n; i++) { groups.push_back(Group{{i}, {0LL, 1LL<<i}}); g[i] = i - 1; } for (pair<int,int> edge : edges) { int i = edge.first; int j = edge.second; if (g[i] != g[j]) { groups[min(g[i],g[j])] = merge(groups[g[i]], groups[g[j]], i, j); groups.erase(groups.begin() + max(g[i], g[j])); for (int a = 0; a < (int) groups.size(); a++) { for (int node : groups[a].nodes) { g[node] = a; } } } else { // TODO, inside } } } int r(int a, int b) { return a + rand() % (b - a + 1); } int main() { for (int rep = 0; rep < 1000000; rep++) { int n = r(2, 15); int m = r(1, 70); m = n - 1; vector<pair<int,int>> edges; for (int i = 2; i <= n; i++) { int a = r(1, n); a = i-1; int b = i; // if (rand() % 2) swap(a, b); // swap(a, b); edges.emplace_back(a, b); } // vector<pair<int,int>> edges(m); // for (pair<int,int>& e : edges) { // do { // e.first = r(1, n); // e.second = r(1, n); // } while (e.first == e.second); // } test(n, m, edges); } } */ |