#include <iostream> #include <vector> #include <unordered_map> #include <bitset> #include <algorithm> using namespace std; int n, m, res; vector<pair<int, int>> edges; unordered_map<long long, int> cache; int count(long long state, int i) { long long key = (state << 20) + i; // Assuming m <= 10^6 for shifting if (cache.find(key) != cache.end()) { return cache[key]; } if (i == m) { res += 1; return 1; } int a = edges[i].first, b = edges[i].second; int ba = (state >> (a-1)) & 1, bb = (state >> (b-1)) & 1; if (ba == bb) { return cache[key] = count(state, i+1); } if (ba == 1) { return 0; } return cache[key] = count(state, i+1) + count((state | (1LL << (a-1))) - (1LL << (b-1)), i+1); } int main() { cin >> n >> m; edges.reserve(m); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; edges.emplace_back(a, b); } reverse(edges.begin(), edges.end()); vector<int> o(n, 0); for (int i = 0; i < n; i++) { res = 0; for (int j = 0; j < n - i; j++) { count(((1LL << (i + 1)) - 1) << j, 0); } o[i] = res % 2; } for (auto& val : o) { cout << val << " "; } cout << endl; 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 | #include <iostream> #include <vector> #include <unordered_map> #include <bitset> #include <algorithm> using namespace std; int n, m, res; vector<pair<int, int>> edges; unordered_map<long long, int> cache; int count(long long state, int i) { long long key = (state << 20) + i; // Assuming m <= 10^6 for shifting if (cache.find(key) != cache.end()) { return cache[key]; } if (i == m) { res += 1; return 1; } int a = edges[i].first, b = edges[i].second; int ba = (state >> (a-1)) & 1, bb = (state >> (b-1)) & 1; if (ba == bb) { return cache[key] = count(state, i+1); } if (ba == 1) { return 0; } return cache[key] = count(state, i+1) + count((state | (1LL << (a-1))) - (1LL << (b-1)), i+1); } int main() { cin >> n >> m; edges.reserve(m); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; edges.emplace_back(a, b); } reverse(edges.begin(), edges.end()); vector<int> o(n, 0); for (int i = 0; i < n; i++) { res = 0; for (int j = 0; j < n - i; j++) { count(((1LL << (i + 1)) - 1) << j, 0); } o[i] = res % 2; } for (auto& val : o) { cout << val << " "; } cout << endl; return 0; } |