#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> using namespace __gnu_pbds; using namespace std; #define pb push_back #define st first #define nd second typedef long long ll; typedef long double ld; const ll I = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL; const ll M = 1000LL * 1000LL * 1000LL + 7LL; const int N = 1000 * 1000 + 7; ll sil[N], osil[N]; bool vis[N], lit[N], clr[N]; int il[2]; vector<int> ed[N]; ll QP(ll a, ll n) { ll ans = 1LL; while(n > 0LL) { if(n % 2LL == 1LL) ans = (ans * a) % M; a = (a * a) % M; n /= 2LL; } return ans; } void PreSil(int n) { sil[0] = 1LL; for(int i = 1; i <= n; ++i) sil[i] = (sil[i - 1] * (ll)i) % M; osil[n] = QP(sil[n], M - 2LL); for(int i = n - 1; i >= 0; --i) osil[i] = (osil[i + 1] * (ll)(i + 1)) % M; } inline ll NK(int n, int k) { return (((sil[n] * osil[k]) % M) * osil[n - k]) % M; } void DFS(int v, int c, bool &npc) { vis[v] = true; if(!lit[v]) ++il[c]; c ^= 1; clr[v] = c; if(lit[v]) ++il[c]; for(int i = 0; i < (int)ed[v].size(); ++i) { if(vis[ed[v][i]] && clr[ed[v][i]] == c) npc = true; if(vis[ed[v][i]]) continue; DFS(ed[v][i], c, npc); } } inline ll CntNP(int n) { ll ans = 0LL; for(int i = 0; i <= n; i += 2) ans += NK(n, i); return ans % M; } inline ll CntP(ll n1, ll n2) { ll ans = 0; for(int i = 0; i <= min(n1, n2); ++i) ans += (NK(n1, i) * NK(n2, i)) % M; return ans % M; } void Solve() { int n, m, a, b; ll ans = 1LL; cin >> n >> m; PreSil(n + 3); for(int i = 1; i <= n; ++i) cin >> lit[i]; for(int i = 1; i <= m; ++i) { cin >> a >> b; ed[a].pb(b); ed[b].pb(a); } for(int i = 1; i <= n; ++i) { if(vis[i]) continue; bool npc = false; il[0] = 0; il[1] = 0; DFS(i, 0, npc); if(npc) ans *= CntNP(il[1] + il[0]); else ans *= CntP(il[1], il[0]); ans %= M; } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); //int t; 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 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> using namespace __gnu_pbds; using namespace std; #define pb push_back #define st first #define nd second typedef long long ll; typedef long double ld; const ll I = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL; const ll M = 1000LL * 1000LL * 1000LL + 7LL; const int N = 1000 * 1000 + 7; ll sil[N], osil[N]; bool vis[N], lit[N], clr[N]; int il[2]; vector<int> ed[N]; ll QP(ll a, ll n) { ll ans = 1LL; while(n > 0LL) { if(n % 2LL == 1LL) ans = (ans * a) % M; a = (a * a) % M; n /= 2LL; } return ans; } void PreSil(int n) { sil[0] = 1LL; for(int i = 1; i <= n; ++i) sil[i] = (sil[i - 1] * (ll)i) % M; osil[n] = QP(sil[n], M - 2LL); for(int i = n - 1; i >= 0; --i) osil[i] = (osil[i + 1] * (ll)(i + 1)) % M; } inline ll NK(int n, int k) { return (((sil[n] * osil[k]) % M) * osil[n - k]) % M; } void DFS(int v, int c, bool &npc) { vis[v] = true; if(!lit[v]) ++il[c]; c ^= 1; clr[v] = c; if(lit[v]) ++il[c]; for(int i = 0; i < (int)ed[v].size(); ++i) { if(vis[ed[v][i]] && clr[ed[v][i]] == c) npc = true; if(vis[ed[v][i]]) continue; DFS(ed[v][i], c, npc); } } inline ll CntNP(int n) { ll ans = 0LL; for(int i = 0; i <= n; i += 2) ans += NK(n, i); return ans % M; } inline ll CntP(ll n1, ll n2) { ll ans = 0; for(int i = 0; i <= min(n1, n2); ++i) ans += (NK(n1, i) * NK(n2, i)) % M; return ans % M; } void Solve() { int n, m, a, b; ll ans = 1LL; cin >> n >> m; PreSil(n + 3); for(int i = 1; i <= n; ++i) cin >> lit[i]; for(int i = 1; i <= m; ++i) { cin >> a >> b; ed[a].pb(b); ed[b].pb(a); } for(int i = 1; i <= n; ++i) { if(vis[i]) continue; bool npc = false; il[0] = 0; il[1] = 0; DFS(i, 0, npc); if(npc) ans *= CntNP(il[1] + il[0]); else ans *= CntP(il[1], il[0]); ans %= M; } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); //int t; cin >> t; //while(t--) Solve(); return 0; } |