#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<<"{"; for(auto v : x) o<<v<<", "; return o<<"}";} #define debug(X) cout<<"["#X"]"<<X<<endl; #else #define debug(X) {} #endif #define int long long const int MOD = (int)1e9 + (int)7; vector<int> sil; int fpow(int a, int b) { if(b == 0) return 1; int c = fpow(a, b/2); if(b%2 == 0) return (c*c)%MOD; return (((c*c)%MOD)*a)%MOD; } int newton(int n, int k) { if(k > n) return 0; return (sil[n]*fpow((sil[k]*sil[n-k])%MOD, MOD-2))%MOD; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin>>n>>m; sil.push_back(1); for(int i=1;i<=n+10;i++) sil.push_back((sil[i-1]*i)%MOD); vector<int> tab(n+1); for(int i=1;i<=n;i++) cin>>tab[i]; vector<vector<int> > graph(n+1); vector<int> blacks(n+1); for(int i=0;i<m;i++) { int a, b; cin>>a>>b; graph[a].push_back(b); graph[b].push_back(a); if(tab[a] == 1) blacks[b]++; if(tab[b] == 1) blacks[a]++; } priority_queue<pair<int, int> > q; for(int i=1;i<=n;i++) if(tab[i] == 1 && blacks[i] > 0) q.push({-blacks[i], i}); while(!q.empty()) { auto a = q.top().second; q.pop(); if(tab[a] == 0) continue; debug(a); for(auto v : graph[a]) { if(tab[v] == 1) { tab[a] = 0; tab[v] = 0; for(auto u : graph[v]) { blacks[u]--; if(blacks[u] > 0) q.push({-blacks[u], u}); } break; } } } vector<int> parz(n+1, -1); function<bool(int, int, int&, int&, int&, int&, int&, int&)> dfs = [&](int a, int d, int& kp, int& knp, int& wp, int& wnp, int& p, int& np) { parz[a] = d; if(tab[a] == 1) { if(d%2 == 0) kp++; else knp++; } if(tab[a] == 0) { if(d%2 == 0) wp++; else wnp++; } if(d%2 == 0) p++; else np++; bool out = false; for(auto v : graph[a]) { if(parz[v] == -2) continue; if(parz[v] != -1) { if(parz[v]%2 != (d+1)%2) out = true; continue; } out |= dfs(v, d+1, kp, knp, wp, wnp, p, np); } parz[a] = -2; return out; }; debug(tab); int result = 1; for(int i=1;i<=n;i++) { if(parz[i] != -1) continue; int kp = 0, knp = 0, wp = 0, wnp = 0, p = 0, np = 0; bool npCycle = false; npCycle = dfs(i, 0, kp, knp, wp, wnp, p, np); int res = 0; if(npCycle == false) { int delt = abs(kp - knp); if(kp > knp) kp = delt, knp = 0; else knp = delt, kp = 0; wp = p - kp; wnp = np - knp; for(int j=0;j<min(wp, wnp);j++) { res += (newton(p, kp)*newton(np, knp))%MOD; res %= MOD; kp++; knp++; } res += (newton(p, kp)*newton(np, knp))%MOD; res %= MOD; } else { kp = (kp+knp)%2; knp = 0; for(int j=0;j<(p+np)/2;j++) { res += newton(p+np, kp); res %= MOD; kp+=2; } res += newton(p+np, kp); res %= MOD; } debug(i); debug(npCycle); debug(res); result *= res; result %= MOD; } cout<<result%MOD; }
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 | #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<<"{"; for(auto v : x) o<<v<<", "; return o<<"}";} #define debug(X) cout<<"["#X"]"<<X<<endl; #else #define debug(X) {} #endif #define int long long const int MOD = (int)1e9 + (int)7; vector<int> sil; int fpow(int a, int b) { if(b == 0) return 1; int c = fpow(a, b/2); if(b%2 == 0) return (c*c)%MOD; return (((c*c)%MOD)*a)%MOD; } int newton(int n, int k) { if(k > n) return 0; return (sil[n]*fpow((sil[k]*sil[n-k])%MOD, MOD-2))%MOD; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin>>n>>m; sil.push_back(1); for(int i=1;i<=n+10;i++) sil.push_back((sil[i-1]*i)%MOD); vector<int> tab(n+1); for(int i=1;i<=n;i++) cin>>tab[i]; vector<vector<int> > graph(n+1); vector<int> blacks(n+1); for(int i=0;i<m;i++) { int a, b; cin>>a>>b; graph[a].push_back(b); graph[b].push_back(a); if(tab[a] == 1) blacks[b]++; if(tab[b] == 1) blacks[a]++; } priority_queue<pair<int, int> > q; for(int i=1;i<=n;i++) if(tab[i] == 1 && blacks[i] > 0) q.push({-blacks[i], i}); while(!q.empty()) { auto a = q.top().second; q.pop(); if(tab[a] == 0) continue; debug(a); for(auto v : graph[a]) { if(tab[v] == 1) { tab[a] = 0; tab[v] = 0; for(auto u : graph[v]) { blacks[u]--; if(blacks[u] > 0) q.push({-blacks[u], u}); } break; } } } vector<int> parz(n+1, -1); function<bool(int, int, int&, int&, int&, int&, int&, int&)> dfs = [&](int a, int d, int& kp, int& knp, int& wp, int& wnp, int& p, int& np) { parz[a] = d; if(tab[a] == 1) { if(d%2 == 0) kp++; else knp++; } if(tab[a] == 0) { if(d%2 == 0) wp++; else wnp++; } if(d%2 == 0) p++; else np++; bool out = false; for(auto v : graph[a]) { if(parz[v] == -2) continue; if(parz[v] != -1) { if(parz[v]%2 != (d+1)%2) out = true; continue; } out |= dfs(v, d+1, kp, knp, wp, wnp, p, np); } parz[a] = -2; return out; }; debug(tab); int result = 1; for(int i=1;i<=n;i++) { if(parz[i] != -1) continue; int kp = 0, knp = 0, wp = 0, wnp = 0, p = 0, np = 0; bool npCycle = false; npCycle = dfs(i, 0, kp, knp, wp, wnp, p, np); int res = 0; if(npCycle == false) { int delt = abs(kp - knp); if(kp > knp) kp = delt, knp = 0; else knp = delt, kp = 0; wp = p - kp; wnp = np - knp; for(int j=0;j<min(wp, wnp);j++) { res += (newton(p, kp)*newton(np, knp))%MOD; res %= MOD; kp++; knp++; } res += (newton(p, kp)*newton(np, knp))%MOD; res %= MOD; } else { kp = (kp+knp)%2; knp = 0; for(int j=0;j<(p+np)/2;j++) { res += newton(p+np, kp); res %= MOD; kp+=2; } res += newton(p+np, kp); res %= MOD; } debug(i); debug(npCycle); debug(res); result *= res; result %= MOD; } cout<<result%MOD; } |