#include <iostream> #include <queue> #include <vector> using namespace std; constexpr long long MOD = 1'000'000'007; int inv(int x) { if (x <= 1) { return x; } return MOD - MOD / x * inv(MOD % x) % MOD; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<int> bulbs, colors; vector<vector<int>> switches; bulbs.resize(n + 1); switches.resize(n + 1); colors.resize(n + 1); for (int i = 1; i <= n; ++i) { cin >> bulbs[i]; } for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; switches[a].push_back(b); switches[b].push_back(a); } long long res = 1; for (int i = 1; i <= n; ++i) { if (colors[i] != 0) { continue; } long long allCount{}, oneCount{}; bool oddCycle = false; colors[i] = 1; oneCount = bulbs[i] == 1; allCount = 1; queue<int> bfs; bfs.push(i); while (!bfs.empty()) { int bulb = bfs.front(); bfs.pop(); for (auto next : switches[bulb]) { if (colors[next] == colors[bulb]) { oddCycle = true; } if (colors[next] != 0) continue; bfs.push(next); colors[next] = 3 - colors[bulb]; ++allCount; oneCount += (colors[next] == 2) ^ (bulbs[next] == 1); } } long long roundRes = 1; if (oddCycle) { for (int k = 1; k < allCount; ++k) { roundRes *= 2; roundRes %= MOD; } } else { oneCount = min(oneCount, allCount - oneCount); for (int k = allCount; k > allCount - oneCount; --k) { roundRes *= k; roundRes %= MOD; } for (int k = 2; k <= oneCount; ++k) { roundRes *= inv(k); roundRes %= MOD; } } res *= roundRes; res %= MOD; } cout << res << '\n'; }
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 | #include <iostream> #include <queue> #include <vector> using namespace std; constexpr long long MOD = 1'000'000'007; int inv(int x) { if (x <= 1) { return x; } return MOD - MOD / x * inv(MOD % x) % MOD; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<int> bulbs, colors; vector<vector<int>> switches; bulbs.resize(n + 1); switches.resize(n + 1); colors.resize(n + 1); for (int i = 1; i <= n; ++i) { cin >> bulbs[i]; } for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; switches[a].push_back(b); switches[b].push_back(a); } long long res = 1; for (int i = 1; i <= n; ++i) { if (colors[i] != 0) { continue; } long long allCount{}, oneCount{}; bool oddCycle = false; colors[i] = 1; oneCount = bulbs[i] == 1; allCount = 1; queue<int> bfs; bfs.push(i); while (!bfs.empty()) { int bulb = bfs.front(); bfs.pop(); for (auto next : switches[bulb]) { if (colors[next] == colors[bulb]) { oddCycle = true; } if (colors[next] != 0) continue; bfs.push(next); colors[next] = 3 - colors[bulb]; ++allCount; oneCount += (colors[next] == 2) ^ (bulbs[next] == 1); } } long long roundRes = 1; if (oddCycle) { for (int k = 1; k < allCount; ++k) { roundRes *= 2; roundRes %= MOD; } } else { oneCount = min(oneCount, allCount - oneCount); for (int k = allCount; k > allCount - oneCount; --k) { roundRes *= k; roundRes %= MOD; } for (int k = 2; k <= oneCount; ++k) { roundRes *= inv(k); roundRes %= MOD; } } res *= roundRes; res %= MOD; } cout << res << '\n'; } |