#include <iostream> #include <vector> #include <queue> #include <math.h> using namespace std; const long long MOD = 1000000007; queue<int> q; queue<int> q2; int counter=0; bool dwud = 1; bool odw[200007]; long long two[400008]; vector<int> v[200007]; vector< pair<int, int> > graph[400007]; void bfs(int pp, int a) { odw[pp] = true; graph[a].push_back({pp, pp}); q2.push(pp); while (!q2.empty()) { int x = q2.front(); q2.pop(); for (int i = 0; i < v[x].size(); i ++) if(odw[v[x][i]] == false) { odw[v[x][i]] = true; graph[a].push_back({x, v[x][i]}); q2.push(v[x][i]); } } } bool isBipartite(int pp, vector<int>& color) { color[pp] = 0; queue<int> q; q.push(pp); while (!q.empty()) { int x = q.front(); q.pop(); for (int i = 0; i < v[x].size(); i++) { if (color[v[x][i]] == -1) { color[v[x][i]] = 1 - color[x]; q.push(v[x][i]); } else if (color[v[x][i]] == color[x]) { return false; } } } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m; cin >> n >> m; vector<int> initial_state(n+1); for (int i = 1; i <= n; ++i) { cin >> initial_state[i]; } for (int i = 0; i < m; ++i) { int ai, bi; cin >> ai >> bi; v[ai].push_back(bi); v[bi].push_back(ai); } int odwa=0; for(int i=1;i<=n;i++){ if(!odw[i]){ bfs(i, counter); } odwa+=graph[counter].size(); counter++; if(odwa == n){ break; } } long long answer=1; two[0]=1; for(int i=1;i<400002;i++){ answer *= 2LL; answer %= MOD; two[i] = answer; } answer = 1; for(int i=0;i<counter;i++){ bool flag = false; if(graph[i].size()>1){ vector<int> col(n+1, -1); dwud = isBipartite(graph[i][0].first, col); } for(int j=1;j<graph[i].size();j++){ if(initial_state[graph[i][j].first] == initial_state[graph[i][j].second]){ flag = true; break; } } if(flag){ if(graph[i].size() == 2){ answer *= 2; } else if(dwud){ answer *= (graph[i].size()*graph[i].size() - 2*graph[i].size()) % MOD; } else { answer *= two[graph[i].size()-1] % MOD; } } } cout<<answer<<endl; 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 | #include <iostream> #include <vector> #include <queue> #include <math.h> using namespace std; const long long MOD = 1000000007; queue<int> q; queue<int> q2; int counter=0; bool dwud = 1; bool odw[200007]; long long two[400008]; vector<int> v[200007]; vector< pair<int, int> > graph[400007]; void bfs(int pp, int a) { odw[pp] = true; graph[a].push_back({pp, pp}); q2.push(pp); while (!q2.empty()) { int x = q2.front(); q2.pop(); for (int i = 0; i < v[x].size(); i ++) if(odw[v[x][i]] == false) { odw[v[x][i]] = true; graph[a].push_back({x, v[x][i]}); q2.push(v[x][i]); } } } bool isBipartite(int pp, vector<int>& color) { color[pp] = 0; queue<int> q; q.push(pp); while (!q.empty()) { int x = q.front(); q.pop(); for (int i = 0; i < v[x].size(); i++) { if (color[v[x][i]] == -1) { color[v[x][i]] = 1 - color[x]; q.push(v[x][i]); } else if (color[v[x][i]] == color[x]) { return false; } } } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m; cin >> n >> m; vector<int> initial_state(n+1); for (int i = 1; i <= n; ++i) { cin >> initial_state[i]; } for (int i = 0; i < m; ++i) { int ai, bi; cin >> ai >> bi; v[ai].push_back(bi); v[bi].push_back(ai); } int odwa=0; for(int i=1;i<=n;i++){ if(!odw[i]){ bfs(i, counter); } odwa+=graph[counter].size(); counter++; if(odwa == n){ break; } } long long answer=1; two[0]=1; for(int i=1;i<400002;i++){ answer *= 2LL; answer %= MOD; two[i] = answer; } answer = 1; for(int i=0;i<counter;i++){ bool flag = false; if(graph[i].size()>1){ vector<int> col(n+1, -1); dwud = isBipartite(graph[i][0].first, col); } for(int j=1;j<graph[i].size();j++){ if(initial_state[graph[i][j].first] == initial_state[graph[i][j].second]){ flag = true; break; } } if(flag){ if(graph[i].size() == 2){ answer *= 2; } else if(dwud){ answer *= (graph[i].size()*graph[i].size() - 2*graph[i].size()) % MOD; } else { answer *= two[graph[i].size()-1] % MOD; } } } cout<<answer<<endl; return 0; } |