#include <bits/stdc++.h> #define ll long long #define fors(u, n, s) for(ll u = (s); u < (n); u++) #define foru(u, n) fors(u, n, 0) #define vec vector #define pb push_back #define f first #define s second #define ir(a, b, x) (((a) <= (x)) && ((x) <= (b))) #define pint pair<int, int> #define us unsigned using namespace std; const int N = 3e5; const int M = 1e9+7; int inv(int a) { return a <= 1 ? a : M - (long long)(M/a) * inv(M % a) % M; } ll power_of_two[N]; ll factorial[N]; vec<int> adj[N]; int color[N]; bool visited[N]; bool my_set[N]; bool dwudzielny = true; int sz = 0; int cnt = 0; void dfs(int x, int c){ if(color[x]==0){ color[x] = c; }else if(color[x] != c) dwudzielny=false; if(visited[x]) return; visited[x]=true; sz ++; cnt += my_set[x]^(color[x]-1); for(auto i : adj[x]) dfs(i, 3-c); } ll solve_compontent(int x){ dwudzielny = true; sz = 0; cnt = 0; dfs(x, 1); if(dwudzielny){ ll ans = factorial[sz]; ans *= inv(factorial[cnt]); ans %= M; ans *= inv(factorial[sz-cnt]); ans %=M; return ans; }else{ return power_of_two[sz-1]; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); power_of_two[0]=1; factorial[0]=1; fors(i, N, 1){ power_of_two[i] = (2*power_of_two[i-1])%M; factorial[i] = (i*factorial[i-1])%M; } int n; cin >> n; int k; cin >> k; foru(i, n) { int x; cin >> x; my_set[i]=x; } foru(_i, k){ int a, b; cin >> a >> b; a--; b--; adj[a].pb(b); adj[b].pb(a); } ll out = 1; foru(i, n){ if(visited[i]) continue; out *= solve_compontent(i); out = out%M; } cout << out; }
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 | #include <bits/stdc++.h> #define ll long long #define fors(u, n, s) for(ll u = (s); u < (n); u++) #define foru(u, n) fors(u, n, 0) #define vec vector #define pb push_back #define f first #define s second #define ir(a, b, x) (((a) <= (x)) && ((x) <= (b))) #define pint pair<int, int> #define us unsigned using namespace std; const int N = 3e5; const int M = 1e9+7; int inv(int a) { return a <= 1 ? a : M - (long long)(M/a) * inv(M % a) % M; } ll power_of_two[N]; ll factorial[N]; vec<int> adj[N]; int color[N]; bool visited[N]; bool my_set[N]; bool dwudzielny = true; int sz = 0; int cnt = 0; void dfs(int x, int c){ if(color[x]==0){ color[x] = c; }else if(color[x] != c) dwudzielny=false; if(visited[x]) return; visited[x]=true; sz ++; cnt += my_set[x]^(color[x]-1); for(auto i : adj[x]) dfs(i, 3-c); } ll solve_compontent(int x){ dwudzielny = true; sz = 0; cnt = 0; dfs(x, 1); if(dwudzielny){ ll ans = factorial[sz]; ans *= inv(factorial[cnt]); ans %= M; ans *= inv(factorial[sz-cnt]); ans %=M; return ans; }else{ return power_of_two[sz-1]; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); power_of_two[0]=1; factorial[0]=1; fors(i, N, 1){ power_of_two[i] = (2*power_of_two[i-1])%M; factorial[i] = (i*factorial[i-1])%M; } int n; cin >> n; int k; cin >> k; foru(i, n) { int x; cin >> x; my_set[i]=x; } foru(_i, k){ int a, b; cin >> a >> b; a--; b--; adj[a].pb(b); adj[b].pb(a); } ll out = 1; foru(i, n){ if(visited[i]) continue; out *= solve_compontent(i); out = out%M; } cout << out; } |