#include <iostream> #include <vector> using namespace std; struct DfsState { long long state; int level; int dir; } s; int n, m; vector<pair<int, int>> orders; bool isBitSet(long long bits, int n) { return (1ll << n) & bits; } bool setBit(long long &bits, int n) { return bits = bits | (1ll << n); } bool resetBit(long long &bits, int n) { return bits = bits & ~(1ll << n); } bool shouldGoDown() { return s.level == size(orders) || s.dir == 2 || (isBitSet(s.state, orders[s.level].first) && !isBitSet(s.state, orders[s.level].second)); } void goDown() { --s.level; if (!isBitSet(s.state, orders[s.level].first) && isBitSet(s.state, orders[s.level].second)) { s.dir = 1; } else { s.dir = 2; } if (isBitSet(s.state, orders[s.level].first) && !isBitSet(s.state, orders[s.level].second)) { resetBit(s.state, orders[s.level].first); setBit(s.state, orders[s.level].second); } } void goUp() { if (s.dir == 1) { setBit(s.state, orders[s.level].first); resetBit(s.state, orders[s.level].second); } s.dir = 0; ++s.level; } void step() { if (shouldGoDown()) { goDown(); } else { goUp(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; orders.resize(m); for (int i = 0; i < m; ++i) { cin >> orders[m - i - 1].first >> orders[m - i - 1].second; } cout << n % 2 << ' '; for (int i = 2; i < n; ++i) { long long res{}; for (int j = 0; j < n - i + 1; ++j) { s = {0, 0, 0}; for (int l = j + 1; l <= j + i; ++l) { setBit(s.state, l); } if (isBitSet(s.state, orders[0].first) && !isBitSet(s.state, orders[0].second)) { s.dir = 2; } for (;;) { if (s.level == 0 && s.dir == 2) { break; } if (s.level == m) { ++res; } step(); } } cout << (res % 2) << ' '; } cout << 1 << " \n"; }
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 | #include <iostream> #include <vector> using namespace std; struct DfsState { long long state; int level; int dir; } s; int n, m; vector<pair<int, int>> orders; bool isBitSet(long long bits, int n) { return (1ll << n) & bits; } bool setBit(long long &bits, int n) { return bits = bits | (1ll << n); } bool resetBit(long long &bits, int n) { return bits = bits & ~(1ll << n); } bool shouldGoDown() { return s.level == size(orders) || s.dir == 2 || (isBitSet(s.state, orders[s.level].first) && !isBitSet(s.state, orders[s.level].second)); } void goDown() { --s.level; if (!isBitSet(s.state, orders[s.level].first) && isBitSet(s.state, orders[s.level].second)) { s.dir = 1; } else { s.dir = 2; } if (isBitSet(s.state, orders[s.level].first) && !isBitSet(s.state, orders[s.level].second)) { resetBit(s.state, orders[s.level].first); setBit(s.state, orders[s.level].second); } } void goUp() { if (s.dir == 1) { setBit(s.state, orders[s.level].first); resetBit(s.state, orders[s.level].second); } s.dir = 0; ++s.level; } void step() { if (shouldGoDown()) { goDown(); } else { goUp(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; orders.resize(m); for (int i = 0; i < m; ++i) { cin >> orders[m - i - 1].first >> orders[m - i - 1].second; } cout << n % 2 << ' '; for (int i = 2; i < n; ++i) { long long res{}; for (int j = 0; j < n - i + 1; ++j) { s = {0, 0, 0}; for (int l = j + 1; l <= j + i; ++l) { setBit(s.state, l); } if (isBitSet(s.state, orders[0].first) && !isBitSet(s.state, orders[0].second)) { s.dir = 2; } for (;;) { if (s.level == 0 && s.dir == 2) { break; } if (s.level == m) { ++res; } step(); } } cout << (res % 2) << ' '; } cout << 1 << " \n"; } |