#include <bits/stdc++.h> using namespace std; #ifndef LOCAL #pragma GCC optimize("O3") #endif #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define endl '\n' #define sp <<" "<< #define eb emplace_back #define MOD 1000000007 #define gcd(a,b) __gcd(a,b) #define lcm(a,b) (a*(b/gcd(a,b))) #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() using ll = long long; #define vec vector template <class T> void print_v(vector<T> &v) { cout << "{"; for (auto x : v) cout << x << ","; cout << "}\n"; } template <class T> void print_m(vector<vector<T>> &m) { for (auto v : m) print_v(v); cout << '\n'; } #define fora(a) for(auto e:a) #define it(i,s,e) for(long long int i=s;i<e;i++) #define ita(i,s,e) for(long long int i=s;i<=e;i++) #define itr(i,e,s) for(long long int i=e-1;i>=s;i--) #define urs(r...)typename decay<decltype(r)>::type #define rep(i,n)for(urs(n)i=0;i<(n);++i) const int MAX_N = 200000+10; int n; vec<int> painting(MAX_N); vec<bool> switches(MAX_N); vec<vec<int>> graph(MAX_N); ll f[MAX_N]; ll RES = 1; void precompute_f() { f[0] = 1; it(i, 1, MAX_N) { f[i] = (f[i-1]*i)%MOD; } } ll powmod(ll a, ll x) { ll res = 1; ll exp = a; while (x) { if (x&1) { res = (res*exp) % MOD; } exp = (exp*exp) % MOD; x = x >> 1; } return res; } ll inverse_euler(ll n) { return powmod(n, MOD-2); } ll choose(ll n, ll r) { return (f[n]*((inverse_euler(f[r]) * inverse_euler(f[n-r])) % MOD)) % MOD; } bool is_two_colorable(int root) { const int TOKEN = -1; queue<int> q; q.push(root); q.push(TOKEN); int curr_color = 1; while (q.size()>1) { int curr = q.front(); // cout << "taken: " << curr <<endl; // cout << "color: " << curr_color <<endl; // cout << "painting[curr]: " << painting[curr] <<endl; q.pop(); if (curr == TOKEN) { curr_color = 3-curr_color; q.push(TOKEN); continue; } if (painting[curr] != 0) { if (painting[curr] != curr_color) return false; continue; } painting[curr] = curr_color; for (int neigh: graph[curr]) q.push(neigh); } return true; } // total on even, true on even; total on odd, true on odd tuple<int, int, int, int> analyze_two_colorable(int root) { int even = 0; int true_even = 0; int odd = 0; int true_odd = 0; queue<int> q; q.push(root); while (q.size()) { int curr = q.front(); q.pop(); if (painting[curr] == 3) { continue; } if (painting[curr]==1) { //cout << curr << " even"; even++; true_even += switches[curr]; } else { //cout << curr << " odd"; odd++; true_odd += switches[curr]; } painting[curr] = 3; for (int neigh: graph[curr]) q.push(neigh); } return make_tuple(even, true_even, odd, true_odd); } // total true, total false pair<int, int> analyze_non_two_colorable(int root) { int tru = 0; int fals = 0; queue<int> q; q.push(root); while (q.size()) { int curr = q.front(); q.pop(); if (painting[curr] == 3) { continue; } painting[curr] = 3; if (switches[curr]) { tru++; } else { fals++; } for (int neigh: graph[curr]) q.push(neigh); } return mp(tru, fals); } void solve() { ita(node, 1, n) { if (painting[node] != 0) continue; if (is_two_colorable(node)) { auto [e,te,o,to] = analyze_two_colorable(node); //cout << e sp te sp o sp to << endl; int sub = min(te,to); te -= sub; to -= sub; ll states = 0; while (te <= e && to <= o) { //cout << e sp te sp o sp to << endl; states = (states + choose(e,te) * choose(o, to)) % MOD; te++; to++; } RES = (RES * states) % MOD; //cout << e sp te sp o sp to << endl; } else { auto [t,f] = analyze_non_two_colorable(node); //cout << t sp f << endl; int total = t+f; t %= 2; ll states = 0; while (t <= total) { states = (states + choose(total,t)) % MOD; t+=2; } RES = (RES * states) % MOD; } } cout << RES << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); precompute_f(); int m; cin >> n >> m; rep(i, n) { int v; cin >> v; switches[i+1] = v; } rep(_, m) { int a,b; cin >> a >> b; graph[a].eb(b); graph[b].eb(a); } solve(); 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | #include <bits/stdc++.h> using namespace std; #ifndef LOCAL #pragma GCC optimize("O3") #endif #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define endl '\n' #define sp <<" "<< #define eb emplace_back #define MOD 1000000007 #define gcd(a,b) __gcd(a,b) #define lcm(a,b) (a*(b/gcd(a,b))) #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() using ll = long long; #define vec vector template <class T> void print_v(vector<T> &v) { cout << "{"; for (auto x : v) cout << x << ","; cout << "}\n"; } template <class T> void print_m(vector<vector<T>> &m) { for (auto v : m) print_v(v); cout << '\n'; } #define fora(a) for(auto e:a) #define it(i,s,e) for(long long int i=s;i<e;i++) #define ita(i,s,e) for(long long int i=s;i<=e;i++) #define itr(i,e,s) for(long long int i=e-1;i>=s;i--) #define urs(r...)typename decay<decltype(r)>::type #define rep(i,n)for(urs(n)i=0;i<(n);++i) const int MAX_N = 200000+10; int n; vec<int> painting(MAX_N); vec<bool> switches(MAX_N); vec<vec<int>> graph(MAX_N); ll f[MAX_N]; ll RES = 1; void precompute_f() { f[0] = 1; it(i, 1, MAX_N) { f[i] = (f[i-1]*i)%MOD; } } ll powmod(ll a, ll x) { ll res = 1; ll exp = a; while (x) { if (x&1) { res = (res*exp) % MOD; } exp = (exp*exp) % MOD; x = x >> 1; } return res; } ll inverse_euler(ll n) { return powmod(n, MOD-2); } ll choose(ll n, ll r) { return (f[n]*((inverse_euler(f[r]) * inverse_euler(f[n-r])) % MOD)) % MOD; } bool is_two_colorable(int root) { const int TOKEN = -1; queue<int> q; q.push(root); q.push(TOKEN); int curr_color = 1; while (q.size()>1) { int curr = q.front(); // cout << "taken: " << curr <<endl; // cout << "color: " << curr_color <<endl; // cout << "painting[curr]: " << painting[curr] <<endl; q.pop(); if (curr == TOKEN) { curr_color = 3-curr_color; q.push(TOKEN); continue; } if (painting[curr] != 0) { if (painting[curr] != curr_color) return false; continue; } painting[curr] = curr_color; for (int neigh: graph[curr]) q.push(neigh); } return true; } // total on even, true on even; total on odd, true on odd tuple<int, int, int, int> analyze_two_colorable(int root) { int even = 0; int true_even = 0; int odd = 0; int true_odd = 0; queue<int> q; q.push(root); while (q.size()) { int curr = q.front(); q.pop(); if (painting[curr] == 3) { continue; } if (painting[curr]==1) { //cout << curr << " even"; even++; true_even += switches[curr]; } else { //cout << curr << " odd"; odd++; true_odd += switches[curr]; } painting[curr] = 3; for (int neigh: graph[curr]) q.push(neigh); } return make_tuple(even, true_even, odd, true_odd); } // total true, total false pair<int, int> analyze_non_two_colorable(int root) { int tru = 0; int fals = 0; queue<int> q; q.push(root); while (q.size()) { int curr = q.front(); q.pop(); if (painting[curr] == 3) { continue; } painting[curr] = 3; if (switches[curr]) { tru++; } else { fals++; } for (int neigh: graph[curr]) q.push(neigh); } return mp(tru, fals); } void solve() { ita(node, 1, n) { if (painting[node] != 0) continue; if (is_two_colorable(node)) { auto [e,te,o,to] = analyze_two_colorable(node); //cout << e sp te sp o sp to << endl; int sub = min(te,to); te -= sub; to -= sub; ll states = 0; while (te <= e && to <= o) { //cout << e sp te sp o sp to << endl; states = (states + choose(e,te) * choose(o, to)) % MOD; te++; to++; } RES = (RES * states) % MOD; //cout << e sp te sp o sp to << endl; } else { auto [t,f] = analyze_non_two_colorable(node); //cout << t sp f << endl; int total = t+f; t %= 2; ll states = 0; while (t <= total) { states = (states + choose(total,t)) % MOD; t+=2; } RES = (RES * states) % MOD; } } cout << RES << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); precompute_f(); int m; cin >> n >> m; rep(i, n) { int v; cin >> v; switches[i+1] = v; } rep(_, m) { int a,b; cin >> a >> b; graph[a].eb(b); graph[b].eb(a); } solve(); return 0; } |