#include <bits/stdc++.h> using namespace std; #ifdef DEBUG auto &operator<<(auto &o, pair<auto, auto> p) { return o << "()" << p.first << ", " << p.second <<")"; } auto operator<<(auto &o, auto x)-> decltype(x.end(), o) { o << "{";int i = 0; for(auto e : x) o << ", "+!i++<<e; return o <<"}"; } #define debug(x...) cerr << "["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(x) #else #define debug(...) {} #endif #define all(x) x.begin(), x.end() #define pb push_back #define fi first #define se second typedef pair <int, int> pii; set <vector <bool> > T; constexpr int MOD = 1e9+7; void rek(vector <bool> & mask, vector <pii> & kraw) { if (T.find(mask) != T.end()) return; T.insert(mask); for (auto &[x, y] : kraw) { if (mask[x] == mask[y]) { mask[x] = 1-mask[x]; mask[y] = 1-mask[y]; rek(mask, kraw); mask[x] = 1-mask[x]; mask[y] = 1-mask[y]; } } } void dfs(auto & G, auto & vis, int v, auto & path) { path.pb(v); vis[v] = true; for (auto x : G[v]) { if (!vis[x]) { dfs(G, vis, x, path); } } } long long qpow(long long a, int b) { if (b == 0) return 1ll; if (b & 1) { return (a * qpow(a, b-1))%MOD; } long long x = qpow(a, b/2); return (x*x)%MOD; } constexpr int N = 2e5+7; long long fact[N]; long long C(int a, int b) { return max(1ll, (((fact[a]*qpow(fact[b], MOD-2))%MOD)*qpow(fact[a-b], MOD-2))%MOD); } void test() { int n, m; cin>>n>>m; vector <pii> k(m); vector <bool> mas(n+1); for (int i=1; i<=n; ++i) { int x; cin>>x; mas[i] = x; } for (int i=0; i<m; ++i) { cin>>k[i].fi>>k[i].se; } if (n > 24) { fact[0] = 1; for (int i=1; i<N; ++i) { fact[i] = (fact[i-1]*i)%MOD; } int res = 1; vector <bool> m2[2]; m2[0] = m2[1] = mas; vector <vector <int> > G(n+1); vector <int> deg(n+1); for (auto &[a, b] : k) { G[a].pb(b); G[b].pb(a); deg[a]++; deg[b]++; } vector <bool> vis(n+1); vector <bool> vis2(n+1); for (int i=1; i<=n; ++i) { if (!vis[i]) { vector <int> path; dfs(G, vis, i, path); int st = path[0]; for (auto x : path) { if (deg[x] == 1) { st = x; } } vector <int> path2; dfs(G, vis2, st, path2); int ile[2] = {0, 0}; for (int l=0; l<2; l++) { for (int x=0; x<path2.size()-1; ++x) { if (m2[l][path2[x]] == m2[l][path2[x+1]] && m2[l][path2[x]] == l) { m2[l][path2[x]] = m2[l][path2[x+1]] = 1-l; } } for (int x=0; x<path2.size()-1; ++x) { if (m2[l][path2[x]] == 1-l && m2[l][path2[x+1]] == 1-l) { ile[l]++; x++; } } } int ilem = max(ile[0], ile[1]); res = (res * C(path2.size(), ilem))%MOD; } } cout<<res<<'\n'; return; } rek(mas, k); cout<<T.size()<<'\n'; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; // cin>>t; while (t--) { test(); } }
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 | #include <bits/stdc++.h> using namespace std; #ifdef DEBUG auto &operator<<(auto &o, pair<auto, auto> p) { return o << "()" << p.first << ", " << p.second <<")"; } auto operator<<(auto &o, auto x)-> decltype(x.end(), o) { o << "{";int i = 0; for(auto e : x) o << ", "+!i++<<e; return o <<"}"; } #define debug(x...) cerr << "["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(x) #else #define debug(...) {} #endif #define all(x) x.begin(), x.end() #define pb push_back #define fi first #define se second typedef pair <int, int> pii; set <vector <bool> > T; constexpr int MOD = 1e9+7; void rek(vector <bool> & mask, vector <pii> & kraw) { if (T.find(mask) != T.end()) return; T.insert(mask); for (auto &[x, y] : kraw) { if (mask[x] == mask[y]) { mask[x] = 1-mask[x]; mask[y] = 1-mask[y]; rek(mask, kraw); mask[x] = 1-mask[x]; mask[y] = 1-mask[y]; } } } void dfs(auto & G, auto & vis, int v, auto & path) { path.pb(v); vis[v] = true; for (auto x : G[v]) { if (!vis[x]) { dfs(G, vis, x, path); } } } long long qpow(long long a, int b) { if (b == 0) return 1ll; if (b & 1) { return (a * qpow(a, b-1))%MOD; } long long x = qpow(a, b/2); return (x*x)%MOD; } constexpr int N = 2e5+7; long long fact[N]; long long C(int a, int b) { return max(1ll, (((fact[a]*qpow(fact[b], MOD-2))%MOD)*qpow(fact[a-b], MOD-2))%MOD); } void test() { int n, m; cin>>n>>m; vector <pii> k(m); vector <bool> mas(n+1); for (int i=1; i<=n; ++i) { int x; cin>>x; mas[i] = x; } for (int i=0; i<m; ++i) { cin>>k[i].fi>>k[i].se; } if (n > 24) { fact[0] = 1; for (int i=1; i<N; ++i) { fact[i] = (fact[i-1]*i)%MOD; } int res = 1; vector <bool> m2[2]; m2[0] = m2[1] = mas; vector <vector <int> > G(n+1); vector <int> deg(n+1); for (auto &[a, b] : k) { G[a].pb(b); G[b].pb(a); deg[a]++; deg[b]++; } vector <bool> vis(n+1); vector <bool> vis2(n+1); for (int i=1; i<=n; ++i) { if (!vis[i]) { vector <int> path; dfs(G, vis, i, path); int st = path[0]; for (auto x : path) { if (deg[x] == 1) { st = x; } } vector <int> path2; dfs(G, vis2, st, path2); int ile[2] = {0, 0}; for (int l=0; l<2; l++) { for (int x=0; x<path2.size()-1; ++x) { if (m2[l][path2[x]] == m2[l][path2[x+1]] && m2[l][path2[x]] == l) { m2[l][path2[x]] = m2[l][path2[x+1]] = 1-l; } } for (int x=0; x<path2.size()-1; ++x) { if (m2[l][path2[x]] == 1-l && m2[l][path2[x+1]] == 1-l) { ile[l]++; x++; } } } int ilem = max(ile[0], ile[1]); res = (res * C(path2.size(), ilem))%MOD; } } cout<<res<<'\n'; return; } rek(mas, k); cout<<T.size()<<'\n'; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; // cin>>t; while (t--) { test(); } } |