#include <bits/stdc++.h> using namespace std; #define st first #define nd second typedef long long ll; typedef long double ld; typedef __int128 int128; typedef vector<int> vi; typedef pair<int,int> pi; typedef pair<double,double> pd; typedef pair<ll,ll> pl; const int max_n = 2e5+7; const ll mod = 1e9+7; bool on[max_n]; bool odw[max_n]; vi g[max_n]; int ile[max_n]; pi rodzaj[max_n]; bool color[max_n]; bool check[max_n]; bool dobry[max_n]; ll power[max_n]; ll fact[max_n]; ll pow(ll x, ll a){ if(a == 1) return x; ll prev = pow(x,a/2); ll akt = (prev*prev)%mod; if(a%2 == 1) akt = (akt*x)%mod; return akt; } ll get_inverse(ll a, ll b){ return (a*pow(b,mod-2))%mod; } void dfs(int v, int num){ //odw[v] = 1; ile[num]++; //cout << "v: " << v << " num: " << num << '\n'; if(color[v]) rodzaj[num].st++; else rodzaj[num].nd++; for(auto x:g[v]){ if(on[v] == on[x]) check[num] = 1; if(odw[x]){ if(color[v] == color[x]) dobry[num] = 1; continue; } color[x] = !color[v]; odw[x] = 1; dfs(x,num); } } int main(){ ios::sync_with_stdio(0); cin.tie(0); int n,m; cin >> n >> m; power[0] = 1; fact[0] = 1; for(ll i = 1; i <= n; i++){ power[i] = (power[i-1]*2LL)%mod; fact[i] = (fact[i-1]*i)%mod; } for(int i = 1; i <= n; i++) cin >> on[i]; for(int i = 1; i <= m; i++){ int a,b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } int akt_s = 0; for(int i = 1; i <= n; i++){ if(!odw[i]){ odw[i] = 1; dfs(i,++akt_s); } } ll ans = 1; for(int i = 1; i <= akt_s; i++){ //cout << "i: " << i << " ile: " << ile[i] // << " check: " << check[i] << " dobry: " << dobry[i] << '\n'; if(!check[i]) continue; ll val = power[ile[i]-1]; //cout << "first val: " << val << '\n'; if(!dobry[i] && ile[i] > 2){ ll a = rodzaj[i].st; ll b = rodzaj[i].nd; if(a < b) swap(a,b); //cout << "a: " << a << " b: " << b << '\n'; val = 1; for(int i = 1; i <= b; i++){ ll up = (fact[a]*fact[b])%mod; ll down = (fact[i]*fact[i])%mod; down *= (fact[a-i]*fact[b-i])%mod; down %= mod; //cout << "i: " << i << " up: " << up << " down: " << down << '\n'; val += get_inverse(up,down); } } //cout << "val: " << val << "\n"; ans *= val; ans %= mod; } cout << ans << '\n'; return 0; } //g++ -O3 -static -Wall .cpp -std=c++17
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 | #include <bits/stdc++.h> using namespace std; #define st first #define nd second typedef long long ll; typedef long double ld; typedef __int128 int128; typedef vector<int> vi; typedef pair<int,int> pi; typedef pair<double,double> pd; typedef pair<ll,ll> pl; const int max_n = 2e5+7; const ll mod = 1e9+7; bool on[max_n]; bool odw[max_n]; vi g[max_n]; int ile[max_n]; pi rodzaj[max_n]; bool color[max_n]; bool check[max_n]; bool dobry[max_n]; ll power[max_n]; ll fact[max_n]; ll pow(ll x, ll a){ if(a == 1) return x; ll prev = pow(x,a/2); ll akt = (prev*prev)%mod; if(a%2 == 1) akt = (akt*x)%mod; return akt; } ll get_inverse(ll a, ll b){ return (a*pow(b,mod-2))%mod; } void dfs(int v, int num){ //odw[v] = 1; ile[num]++; //cout << "v: " << v << " num: " << num << '\n'; if(color[v]) rodzaj[num].st++; else rodzaj[num].nd++; for(auto x:g[v]){ if(on[v] == on[x]) check[num] = 1; if(odw[x]){ if(color[v] == color[x]) dobry[num] = 1; continue; } color[x] = !color[v]; odw[x] = 1; dfs(x,num); } } int main(){ ios::sync_with_stdio(0); cin.tie(0); int n,m; cin >> n >> m; power[0] = 1; fact[0] = 1; for(ll i = 1; i <= n; i++){ power[i] = (power[i-1]*2LL)%mod; fact[i] = (fact[i-1]*i)%mod; } for(int i = 1; i <= n; i++) cin >> on[i]; for(int i = 1; i <= m; i++){ int a,b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } int akt_s = 0; for(int i = 1; i <= n; i++){ if(!odw[i]){ odw[i] = 1; dfs(i,++akt_s); } } ll ans = 1; for(int i = 1; i <= akt_s; i++){ //cout << "i: " << i << " ile: " << ile[i] // << " check: " << check[i] << " dobry: " << dobry[i] << '\n'; if(!check[i]) continue; ll val = power[ile[i]-1]; //cout << "first val: " << val << '\n'; if(!dobry[i] && ile[i] > 2){ ll a = rodzaj[i].st; ll b = rodzaj[i].nd; if(a < b) swap(a,b); //cout << "a: " << a << " b: " << b << '\n'; val = 1; for(int i = 1; i <= b; i++){ ll up = (fact[a]*fact[b])%mod; ll down = (fact[i]*fact[i])%mod; down *= (fact[a-i]*fact[b-i])%mod; down %= mod; //cout << "i: " << i << " up: " << up << " down: " << down << '\n'; val += get_inverse(up,down); } } //cout << "val: " << val << "\n"; ans *= val; ans %= mod; } cout << ans << '\n'; return 0; } //g++ -O3 -static -Wall .cpp -std=c++17 |