#pragma GCC optimize("Ofast") //#pragma GCC target ("avx2") //#pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include<bits/stdc++.h> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> //using namespace __gnu_pbds; using namespace std; //typedef tree<pair<int,int>,null_type,less<pair<int,int>>,rb_tree_tag,tree_order_statistics_node_update>ordered_set; #define ll long long #define ull unsigned long long int #define pb push_back #define mp make_pair #define vi vector<int> #define pii pair<int,int> #define pss pair<short,short> #define pld pair<long double,long double > #define ld long double #define piii pair<pii,int> #define vii vector<pair<int,int> > #define st first #define nd second #define pll pair<ll,ll> #define speed ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define M_PI 3.14159265358979323846 #define int long long const int mod=1000000007; //const int mod=1009; //const int mod=998244353; const int inf=1000000009; const long long INF=1000000000000000009; const long long big=1000000000000000; const long double eps=0.000000000000000000001; const int rozmiar=4e5+5; int F[rozmiar],OF[rozmiar],dwa[rozmiar]; int Pow(int a,int b){ int wynik=1; while(b){ if(b&1) wynik=(wynik*a)%mod; a=(a*a)%mod; b/=2; } return wynik; } int symbol(int a,int b){ if(a<b) return 0; return (((F[a]*OF[b])%mod)*OF[a-b])%mod; } vi P[rozmiar]; int T[rozmiar],L[rozmiar]; int R[2][2]; bool O[rozmiar]; bool cycle=0; int ile=0; void dfs(int n,int p){ //cout<<"elo "<<n<<endl; L[n]=L[p]+1; ile++; R[L[n]%2][T[n]]++; O[n]=1; for(auto v:P[n]){ if(!O[v]) dfs(v,n); else{ int roznica=abs(L[v]-L[n]); if(roznica%2==0){ cycle=1; } } } } void solve() { int n,m; cin>>n>>m; F[0]=1; OF[0]=1; dwa[0]=1; for(int i=1;i<=n;i++){ cin>>T[i]; F[i]=(F[i-1]*i)%mod; OF[i]=Pow(F[i],mod-2); dwa[i]=(dwa[i-1]*2)%mod; } for(int i=1;i<=m;i++){ int a,b; cin>>a>>b; P[a].pb(b); P[b].pb(a); } int wynik=1; for(int i=1;i<=n;i++){ if(!O[i]){ //cout<<"siema "<<i<<endl; dfs(i,0); int aktual=0; if(cycle){ aktual=dwa[ile-1]; } else{ if(R[0][0]>R[1][0]){ swap(R[0][0],R[1][0]); swap(R[0][1],R[1][1]); } int cur=min(R[0][0],R[1][0])+min(R[0][1],R[1][1]); while(R[1][0]>R[0][0]&&R[1][1]>R[0][1]){ R[0][0]++; R[0][1]++; cur++; } aktual=symbol(ile,cur); } R[0][0]=0; R[0][1]=0; R[1][0]=0; R[1][1]=0; cycle=0; ile=0; wynik=(wynik*aktual)%mod; } } cout<<wynik; } int32_t main(){ speed int t = 1; //cin >> t; while(t--){ solve(); } return 0; }
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 | #pragma GCC optimize("Ofast") //#pragma GCC target ("avx2") //#pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include<bits/stdc++.h> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> //using namespace __gnu_pbds; using namespace std; //typedef tree<pair<int,int>,null_type,less<pair<int,int>>,rb_tree_tag,tree_order_statistics_node_update>ordered_set; #define ll long long #define ull unsigned long long int #define pb push_back #define mp make_pair #define vi vector<int> #define pii pair<int,int> #define pss pair<short,short> #define pld pair<long double,long double > #define ld long double #define piii pair<pii,int> #define vii vector<pair<int,int> > #define st first #define nd second #define pll pair<ll,ll> #define speed ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define M_PI 3.14159265358979323846 #define int long long const int mod=1000000007; //const int mod=1009; //const int mod=998244353; const int inf=1000000009; const long long INF=1000000000000000009; const long long big=1000000000000000; const long double eps=0.000000000000000000001; const int rozmiar=4e5+5; int F[rozmiar],OF[rozmiar],dwa[rozmiar]; int Pow(int a,int b){ int wynik=1; while(b){ if(b&1) wynik=(wynik*a)%mod; a=(a*a)%mod; b/=2; } return wynik; } int symbol(int a,int b){ if(a<b) return 0; return (((F[a]*OF[b])%mod)*OF[a-b])%mod; } vi P[rozmiar]; int T[rozmiar],L[rozmiar]; int R[2][2]; bool O[rozmiar]; bool cycle=0; int ile=0; void dfs(int n,int p){ //cout<<"elo "<<n<<endl; L[n]=L[p]+1; ile++; R[L[n]%2][T[n]]++; O[n]=1; for(auto v:P[n]){ if(!O[v]) dfs(v,n); else{ int roznica=abs(L[v]-L[n]); if(roznica%2==0){ cycle=1; } } } } void solve() { int n,m; cin>>n>>m; F[0]=1; OF[0]=1; dwa[0]=1; for(int i=1;i<=n;i++){ cin>>T[i]; F[i]=(F[i-1]*i)%mod; OF[i]=Pow(F[i],mod-2); dwa[i]=(dwa[i-1]*2)%mod; } for(int i=1;i<=m;i++){ int a,b; cin>>a>>b; P[a].pb(b); P[b].pb(a); } int wynik=1; for(int i=1;i<=n;i++){ if(!O[i]){ //cout<<"siema "<<i<<endl; dfs(i,0); int aktual=0; if(cycle){ aktual=dwa[ile-1]; } else{ if(R[0][0]>R[1][0]){ swap(R[0][0],R[1][0]); swap(R[0][1],R[1][1]); } int cur=min(R[0][0],R[1][0])+min(R[0][1],R[1][1]); while(R[1][0]>R[0][0]&&R[1][1]>R[0][1]){ R[0][0]++; R[0][1]++; cur++; } aktual=symbol(ile,cur); } R[0][0]=0; R[0][1]=0; R[1][0]=0; R[1][1]=0; cycle=0; ile=0; wynik=(wynik*aktual)%mod; } } cout<<wynik; } int32_t main(){ speed int t = 1; //cin >> t; while(t--){ solve(); } return 0; } |