#include <bits/stdc++.h> #define FOR(i,a,b) for(int i = a; i < b;++i) #define pb emplace_back #define int long long #define f first #define s second #define mp make_pair using namespace std; const int M = 1e9 + 7; const int maxn = 200 * 1000 + 5; vector<int> pol[maxn],vis(maxn,0),kol(maxn,0),tab(maxn,0); vector<pair<int,int>> pary; map<pair<int,int>,int> odw; int cnt = 0,wie = 0; inline void dfs(int v){ wie+= ((1 << cnt) * kol[v]); tab[v] = cnt; ++cnt; for(auto &y : pol[v]){ if(odw[mp(min(v,y),max(v,y))] == 0){ odw[mp(min(v,y),max(v,y))] = 1; pary.pb(mp(v,y)); } if(vis[y] == 0){ vis[y] = 1; dfs(y); } } } int32_t main(){ cin.tie(0); ios_base::sync_with_stdio(0); int n,m; cin>>n >>m; FOR(i,1,n + 1){cin>>kol[i];} FOR(i,0,m){ int x,y; cin>>x >>y; pol[x].pb(y); pol[y].pb(x); } int ans = 1; FOR(i,1,n + 1){ if(vis[i] == 0){ map<int,int> stan; cnt = 0,wie = 0; vis[i] = 1; pary.clear(); dfs(i); queue<int> Q; int il = 1; stan[wie] = 1; Q.push(wie); while(!Q.empty()){ auto x = Q.front(); Q.pop(); FOR(j,0,pary.size()){ int x1 = tab[pary[j].f],x2 = tab[pary[j].s]; if(((x & (1 << x1)) == (1 << x1)) == ((x & (1 << x2)) == (1 << x2))){ x^= (1 << x1); x^= (1 << x2); if(stan[x] == 0){ stan[x] = 1; ++il; Q.push(x); } x^= (1 << x1); x^= (1 << x2); } } } ans*=il; ans%=M; } } cout<<ans; }
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 | #include <bits/stdc++.h> #define FOR(i,a,b) for(int i = a; i < b;++i) #define pb emplace_back #define int long long #define f first #define s second #define mp make_pair using namespace std; const int M = 1e9 + 7; const int maxn = 200 * 1000 + 5; vector<int> pol[maxn],vis(maxn,0),kol(maxn,0),tab(maxn,0); vector<pair<int,int>> pary; map<pair<int,int>,int> odw; int cnt = 0,wie = 0; inline void dfs(int v){ wie+= ((1 << cnt) * kol[v]); tab[v] = cnt; ++cnt; for(auto &y : pol[v]){ if(odw[mp(min(v,y),max(v,y))] == 0){ odw[mp(min(v,y),max(v,y))] = 1; pary.pb(mp(v,y)); } if(vis[y] == 0){ vis[y] = 1; dfs(y); } } } int32_t main(){ cin.tie(0); ios_base::sync_with_stdio(0); int n,m; cin>>n >>m; FOR(i,1,n + 1){cin>>kol[i];} FOR(i,0,m){ int x,y; cin>>x >>y; pol[x].pb(y); pol[y].pb(x); } int ans = 1; FOR(i,1,n + 1){ if(vis[i] == 0){ map<int,int> stan; cnt = 0,wie = 0; vis[i] = 1; pary.clear(); dfs(i); queue<int> Q; int il = 1; stan[wie] = 1; Q.push(wie); while(!Q.empty()){ auto x = Q.front(); Q.pop(); FOR(j,0,pary.size()){ int x1 = tab[pary[j].f],x2 = tab[pary[j].s]; if(((x & (1 << x1)) == (1 << x1)) == ((x & (1 << x2)) == (1 << x2))){ x^= (1 << x1); x^= (1 << x2); if(stan[x] == 0){ stan[x] = 1; ++il; Q.push(x); } x^= (1 << x1); x^= (1 << x2); } } } ans*=il; ans%=M; } } cout<<ans; } |