#include <bits/stdc++.h> using namespace std; constexpr int maxN = 2e5; constexpr long long MOD = 1e9 + 7; int n, m; int c[maxN + 1]; int a, b; vector<int> V[maxN + 1]; bool visited[maxN + 1]; bool col[maxN + 1]; int cnt0, cnt1, lit0, lit1, sz; long long pow_2[maxN], bin1[maxN + 1], bin2[maxN + 1]; long long ans = 1; bool DFS(int v) { bool biparite = true; visited[v] = true; if(col[v]) { cnt1++; lit1 += c[v]; } else { cnt0++; lit0 += c[v]; } sz++; for(int u : V[v]) { if(!visited[u]) { col[u] = !col[v]; biparite &= DFS(u); } else { if(col[v] == col[u]) { biparite = false; } } } return biparite; } long long extGcd(long long a, long long b, long long &x, long long &y) { if(a == 0) { x = 0; y = 1; return b; } long long x1, y1; long long gcd = extGcd(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return gcd; } long long inverse(long long v) { long long x, y; long long g = extGcd(v, MOD, x, y); long long res = x % MOD; if(res < 0) { res += MOD; } return res; } long long calc(long long x, long long y) { x %= MOD; long long inv = inverse(y); return (x * inv) % MOD; } long long sumNewt(int n1, int n2, int diff) { long long res = 0; bin1[0] = 1; for(int i=1; i<=n1; i++) { bin1[i] = calc(bin1[i - 1] * (n1 - i + 1), i); } bin2[0] = 1; for(int i=1; i<=n2; i++) { bin2[i] = calc(bin2[i - 1] * (n2 - i + 1), i); } for(int i=0; i+diff<=n1 && i<=n2; i++) { res += (bin1[i + diff] * bin2[i]) % MOD; res %= MOD; } return res; } long long solve(int v) { cnt0 = cnt1 = lit0 = lit1 = sz = 0; bool biparite = DFS(v); if(!biparite) { return pow_2[sz - 1]; } if(lit0 < lit1) { swap(cnt0, cnt1); swap(lit0, lit1); } return sumNewt(cnt0, cnt1, lit0 - lit1); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; for(int i=1; i<=n; i++) { cin >> c[i]; } for(int i=1; i<=m; i++) { cin >> a >> b; V[a].push_back(b); V[b].push_back(a); } pow_2[0] = 1; for(int i=1; i<=n-1; i++) { pow_2[i] = (pow_2[i - 1] << 1) % MOD; } for(int i=1; i<=n; i++) { if(!visited[i]) { ans *= solve(i); ans %= MOD; } } 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 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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | #include <bits/stdc++.h> using namespace std; constexpr int maxN = 2e5; constexpr long long MOD = 1e9 + 7; int n, m; int c[maxN + 1]; int a, b; vector<int> V[maxN + 1]; bool visited[maxN + 1]; bool col[maxN + 1]; int cnt0, cnt1, lit0, lit1, sz; long long pow_2[maxN], bin1[maxN + 1], bin2[maxN + 1]; long long ans = 1; bool DFS(int v) { bool biparite = true; visited[v] = true; if(col[v]) { cnt1++; lit1 += c[v]; } else { cnt0++; lit0 += c[v]; } sz++; for(int u : V[v]) { if(!visited[u]) { col[u] = !col[v]; biparite &= DFS(u); } else { if(col[v] == col[u]) { biparite = false; } } } return biparite; } long long extGcd(long long a, long long b, long long &x, long long &y) { if(a == 0) { x = 0; y = 1; return b; } long long x1, y1; long long gcd = extGcd(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return gcd; } long long inverse(long long v) { long long x, y; long long g = extGcd(v, MOD, x, y); long long res = x % MOD; if(res < 0) { res += MOD; } return res; } long long calc(long long x, long long y) { x %= MOD; long long inv = inverse(y); return (x * inv) % MOD; } long long sumNewt(int n1, int n2, int diff) { long long res = 0; bin1[0] = 1; for(int i=1; i<=n1; i++) { bin1[i] = calc(bin1[i - 1] * (n1 - i + 1), i); } bin2[0] = 1; for(int i=1; i<=n2; i++) { bin2[i] = calc(bin2[i - 1] * (n2 - i + 1), i); } for(int i=0; i+diff<=n1 && i<=n2; i++) { res += (bin1[i + diff] * bin2[i]) % MOD; res %= MOD; } return res; } long long solve(int v) { cnt0 = cnt1 = lit0 = lit1 = sz = 0; bool biparite = DFS(v); if(!biparite) { return pow_2[sz - 1]; } if(lit0 < lit1) { swap(cnt0, cnt1); swap(lit0, lit1); } return sumNewt(cnt0, cnt1, lit0 - lit1); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; for(int i=1; i<=n; i++) { cin >> c[i]; } for(int i=1; i<=m; i++) { cin >> a >> b; V[a].push_back(b); V[b].push_back(a); } pow_2[0] = 1; for(int i=1; i<=n-1; i++) { pow_2[i] = (pow_2[i - 1] << 1) % MOD; } for(int i=1; i<=n; i++) { if(!visited[i]) { ans *= solve(i); ans %= MOD; } } cout << ans; } |