#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; #pragma GCC maksuje_zadania_na_essie("pot200"); #define loop(i, a, b) for(int i = a; i <= b; i++) #define loop_rev(i, a, b) for(int i = a; i >= b; i--) #define all(x) x.begin(), x.end() #define sz(x) int(x.size()) #define pb push_back using ll = int64_t; using ull = uint64_t; using uint = uint32_t; vector<vector<int>> graph; constexpr ll MOD = 1e9 + 7; ll res = 0; int n, m; constexpr int MAX_STACK = 180'000'000; bitset<2'000'000'000> mem; void solve(ll start_arr) { static int st[MAX_STACK]; int stack_ind = 1; st[stack_ind] = start_arr; while(stack_ind) { int arr = st[stack_ind]; if(!mem[arr]) { mem[arr] = 1; ++res; loop(v, 0, n-1) { for(int s : graph[v]) { if((!(arr & (1<<s))) == (!(arr & (1<<v)))) { st[stack_ind++] = arr ^ (1<<s) ^ (1<<v); } } } } -- stack_ind; } } int main() { cin.tie(0)->sync_with_stdio(false); cin >> n >> m; int arr = 0; loop(i, 0, n-1) { int x; cin >> x; if(x) arr ^= (1<<i); } graph.resize(n+1); loop(i, 0, m-1) { int a, b; cin >> a >> b; if(a < b) graph[a-1].pb(b-1); else graph[b-1].pb(a-1); } solve(arr); cout << res % MOD << '\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 | #include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; #pragma GCC maksuje_zadania_na_essie("pot200"); #define loop(i, a, b) for(int i = a; i <= b; i++) #define loop_rev(i, a, b) for(int i = a; i >= b; i--) #define all(x) x.begin(), x.end() #define sz(x) int(x.size()) #define pb push_back using ll = int64_t; using ull = uint64_t; using uint = uint32_t; vector<vector<int>> graph; constexpr ll MOD = 1e9 + 7; ll res = 0; int n, m; constexpr int MAX_STACK = 180'000'000; bitset<2'000'000'000> mem; void solve(ll start_arr) { static int st[MAX_STACK]; int stack_ind = 1; st[stack_ind] = start_arr; while(stack_ind) { int arr = st[stack_ind]; if(!mem[arr]) { mem[arr] = 1; ++res; loop(v, 0, n-1) { for(int s : graph[v]) { if((!(arr & (1<<s))) == (!(arr & (1<<v)))) { st[stack_ind++] = arr ^ (1<<s) ^ (1<<v); } } } } -- stack_ind; } } int main() { cin.tie(0)->sync_with_stdio(false); cin >> n >> m; int arr = 0; loop(i, 0, n-1) { int x; cin >> x; if(x) arr ^= (1<<i); } graph.resize(n+1); loop(i, 0, m-1) { int a, b; cin >> a >> b; if(a < b) graph[a-1].pb(b-1); else graph[b-1].pb(a-1); } solve(arr); cout << res % MOD << '\n'; } |