#include <bits/stdc++.h> using namespace std; #ifdef DEBUG auto&operator<<(auto &o, pair<auto, auto> p) {o << "(" << p.first << ", " << p.second << ")"; return o;} auto operator<<(auto &o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto e : x) o<<e<<", "; return o<<"}";} #define debug(X) cerr << "["#X"]: " << X << '\n'; #else #define cerr if(0)cout #define debug(X) ; #endif using ll = long long; #define all(v) (v).begin(), (v).end() #define ssize(x) int(x.size()) #define fi first #define se second #define mp make_pair #define eb emplace_back int val(unsigned int bm, int idx) { return (bm&(1<<idx))>>idx; } void brut1(int n, int m) { vector<pair<int, int>> roz(m); for(auto &[a, b] : roz) { cin >> a >> b; a--; b--; } // auto bin = [&](unsigned int a) { // for(int j = n-1; j >= 0; j--) cerr << val(a, j); // cerr << endl; // }; vector<unsigned int> possible; auto add_b = [&](unsigned int bm) { unsigned int x = bm&((1<<roz[0].se)-1); bm ^= x; bm <<= 1; bm ^= x; if(val(bm, roz[0].fi)) bm ^= (1<<roz[0].se); return bm; }; for(unsigned int mask = 1; mask < (1<<(n-1)); ++mask) { unsigned int bm = add_b(mask); possible.eb(bm); } vector<int> cnt(1<<n); for(int i = 1; i < m; ++i) { auto [a, b] = roz[i]; for(unsigned int &bm: possible) { if(val(bm, a) == 1 && val(bm, b) == 0) bm ^= (1<<a)^(1<<b); ++cnt[bm]; cnt[bm] &= 1; } vector<unsigned int> new_possible; for(int bm : possible) if(cnt[bm] == 1) {new_possible.eb(bm); cnt[bm] = 0;} possible = new_possible; } auto seg = [&](unsigned int x) { bool had_zero = false, had_one = false, ok = true; for(int i = 0; i < n; ++i) { if(val(x, i) == 1 && had_zero && had_one) {ok = false; break;} if(val(x, i) == 1) had_one = true; if(val(x, i) == 0 && had_one) had_zero = true; } return ok; }; vector<int> res(n); for(unsigned int bm : possible) { if(seg(bm)) { int k = __builtin_popcountll(bm); if(k >= 1) res[k-1] ^= 1; } } for(int x : res) cout << x << ' '; return; } void brut2(int n, int m) { vector<pair<int, int>> roz(m); for(auto &[a, b] : roz) { cin >> a >> b; a--; b--; } // auto bin = [&](unsigned int a) { // for(int j = n-1; j >= 0; j--) cerr << val(a, j); // cerr << endl; // }; vector<unsigned int> possible; auto add_b = [&](unsigned int bm) { unsigned int x = bm&((1<<roz[0].se)-1); bm ^= x; bm <<= 1; bm ^= x; if(val(bm, roz[0].fi)) bm ^= (1<<roz[0].se); return bm; }; for(unsigned int mask = 1; mask < (1<<(n-1)); ++mask) { unsigned int bm = add_b(mask); possible.eb(bm); } unordered_map<unsigned int, int> cnt; for(int i = 1; i < m; ++i) { auto [a, b] = roz[i]; for(unsigned int &bm: possible) { if(val(bm, a) == 1 && val(bm, b) == 0) bm ^= (1<<a)^(1<<b); ++cnt[bm]; cnt[bm] &= 1; } vector<unsigned int> new_possible; for(int bm : possible) if(cnt[bm] == 1) {new_possible.eb(bm); cnt[bm] = 0;} possible = new_possible; debug(possible); debug(ssize(possible)); } auto seg = [&](unsigned int x) { bool had_zero = false, had_one = false, ok = true; for(int i = 0; i < n; ++i) { if(val(x, i) == 1 && had_zero && had_one) {ok = false; break;} if(val(x, i) == 1) had_one = true; if(val(x, i) == 0 && had_one) had_zero = true; } return ok; }; vector<int> res(n); for(unsigned int bm : possible) { if(seg(bm)) { int k = __builtin_popcountll(bm); if(k >= 1) res[k-1] ^= 1; } } for(int x : res) cout << x << ' '; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; if(n <= 26) { brut1(n, m); } else { brut2(n, m); } 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 | #include <bits/stdc++.h> using namespace std; #ifdef DEBUG auto&operator<<(auto &o, pair<auto, auto> p) {o << "(" << p.first << ", " << p.second << ")"; return o;} auto operator<<(auto &o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto e : x) o<<e<<", "; return o<<"}";} #define debug(X) cerr << "["#X"]: " << X << '\n'; #else #define cerr if(0)cout #define debug(X) ; #endif using ll = long long; #define all(v) (v).begin(), (v).end() #define ssize(x) int(x.size()) #define fi first #define se second #define mp make_pair #define eb emplace_back int val(unsigned int bm, int idx) { return (bm&(1<<idx))>>idx; } void brut1(int n, int m) { vector<pair<int, int>> roz(m); for(auto &[a, b] : roz) { cin >> a >> b; a--; b--; } // auto bin = [&](unsigned int a) { // for(int j = n-1; j >= 0; j--) cerr << val(a, j); // cerr << endl; // }; vector<unsigned int> possible; auto add_b = [&](unsigned int bm) { unsigned int x = bm&((1<<roz[0].se)-1); bm ^= x; bm <<= 1; bm ^= x; if(val(bm, roz[0].fi)) bm ^= (1<<roz[0].se); return bm; }; for(unsigned int mask = 1; mask < (1<<(n-1)); ++mask) { unsigned int bm = add_b(mask); possible.eb(bm); } vector<int> cnt(1<<n); for(int i = 1; i < m; ++i) { auto [a, b] = roz[i]; for(unsigned int &bm: possible) { if(val(bm, a) == 1 && val(bm, b) == 0) bm ^= (1<<a)^(1<<b); ++cnt[bm]; cnt[bm] &= 1; } vector<unsigned int> new_possible; for(int bm : possible) if(cnt[bm] == 1) {new_possible.eb(bm); cnt[bm] = 0;} possible = new_possible; } auto seg = [&](unsigned int x) { bool had_zero = false, had_one = false, ok = true; for(int i = 0; i < n; ++i) { if(val(x, i) == 1 && had_zero && had_one) {ok = false; break;} if(val(x, i) == 1) had_one = true; if(val(x, i) == 0 && had_one) had_zero = true; } return ok; }; vector<int> res(n); for(unsigned int bm : possible) { if(seg(bm)) { int k = __builtin_popcountll(bm); if(k >= 1) res[k-1] ^= 1; } } for(int x : res) cout << x << ' '; return; } void brut2(int n, int m) { vector<pair<int, int>> roz(m); for(auto &[a, b] : roz) { cin >> a >> b; a--; b--; } // auto bin = [&](unsigned int a) { // for(int j = n-1; j >= 0; j--) cerr << val(a, j); // cerr << endl; // }; vector<unsigned int> possible; auto add_b = [&](unsigned int bm) { unsigned int x = bm&((1<<roz[0].se)-1); bm ^= x; bm <<= 1; bm ^= x; if(val(bm, roz[0].fi)) bm ^= (1<<roz[0].se); return bm; }; for(unsigned int mask = 1; mask < (1<<(n-1)); ++mask) { unsigned int bm = add_b(mask); possible.eb(bm); } unordered_map<unsigned int, int> cnt; for(int i = 1; i < m; ++i) { auto [a, b] = roz[i]; for(unsigned int &bm: possible) { if(val(bm, a) == 1 && val(bm, b) == 0) bm ^= (1<<a)^(1<<b); ++cnt[bm]; cnt[bm] &= 1; } vector<unsigned int> new_possible; for(int bm : possible) if(cnt[bm] == 1) {new_possible.eb(bm); cnt[bm] = 0;} possible = new_possible; debug(possible); debug(ssize(possible)); } auto seg = [&](unsigned int x) { bool had_zero = false, had_one = false, ok = true; for(int i = 0; i < n; ++i) { if(val(x, i) == 1 && had_zero && had_one) {ok = false; break;} if(val(x, i) == 1) had_one = true; if(val(x, i) == 0 && had_one) had_zero = true; } return ok; }; vector<int> res(n); for(unsigned int bm : possible) { if(seg(bm)) { int k = __builtin_popcountll(bm); if(k >= 1) res[k-1] ^= 1; } } for(int x : res) cout << x << ' '; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; if(n <= 26) { brut1(n, m); } else { brut2(n, m); } return 0; } |