|
| 1 | +#include <bits/stdc++.h> |
| 2 | +using namespace std; |
| 3 | + |
| 4 | +typedef long long int64; |
| 5 | + |
| 6 | +const int64 MOD = 1000000007; |
| 7 | + |
| 8 | +const int |
| 9 | + MAXV = 1 << 17; |
| 10 | + |
| 11 | +int V, E; |
| 12 | +int data[MAXV]; |
| 13 | +int components; |
| 14 | +vector<int> hate[MAXV]; |
| 15 | +vector<int> love[MAXV]; |
| 16 | + |
| 17 | +int findSet(int x) { |
| 18 | + return data[x] < 0 ? x : data[x] = findSet(data[x]); |
| 19 | +} |
| 20 | + |
| 21 | +int getSize(int x) { |
| 22 | + return -data[findSet(x)]; |
| 23 | +} |
| 24 | + |
| 25 | +void join(int u, int v) { |
| 26 | + u = findSet(u); |
| 27 | + v = findSet(v); |
| 28 | + if (u == v) |
| 29 | + return ; |
| 30 | + |
| 31 | + |
| 32 | + components--; |
| 33 | + |
| 34 | + if (getSize(u) > getSize(v)) |
| 35 | + swap(u, v); |
| 36 | + |
| 37 | + data[u] += data[v]; |
| 38 | + data[v] = u; |
| 39 | +} |
| 40 | + |
| 41 | +int mark[MAXV][2]; |
| 42 | +int curMark; |
| 43 | + |
| 44 | +void dfs(int u, int x) { |
| 45 | + if (mark[u][1 - x] == curMark) { |
| 46 | + cout << 0 << endl; |
| 47 | + exit(0); |
| 48 | + } |
| 49 | + if (mark[u][x] == curMark) |
| 50 | + return ; |
| 51 | + mark[u][x] = curMark; |
| 52 | + for (int v : love[u]) dfs(v, x); |
| 53 | + for (int v : hate[u]) dfs(v, 1 - x); |
| 54 | +} |
| 55 | + |
| 56 | +int main() { |
| 57 | + ios_base::sync_with_stdio(0); |
| 58 | + |
| 59 | + memset(data, -1, sizeof(data)); |
| 60 | + |
| 61 | + cin >> V >> E; |
| 62 | + |
| 63 | + components = V; |
| 64 | + |
| 65 | + for (int i = 0; i < E; ++i) { |
| 66 | + int u, v, w; |
| 67 | + cin >> u >> v >> w; |
| 68 | + --u; |
| 69 | + --v; |
| 70 | + if (w == 0) { |
| 71 | + hate[u].push_back(v); |
| 72 | + hate[v].push_back(u); |
| 73 | + } else { |
| 74 | + love[u].push_back(v); |
| 75 | + love[v].push_back(u); |
| 76 | + } |
| 77 | + join(u, v); |
| 78 | + } |
| 79 | + |
| 80 | + // search for cycles with odd number of hate relations |
| 81 | + for (int i = 0; i < V; ++i) if (data[i] < 0) { |
| 82 | + curMark++; |
| 83 | + dfs(i, 0); |
| 84 | + } |
| 85 | + |
| 86 | + int64 result = 1; |
| 87 | + for (int i = 0; i < components - 1; ++i) |
| 88 | + result = 2 * result % MOD; |
| 89 | + |
| 90 | + cout << result << endl; |
| 91 | + |
| 92 | + return 0; |
| 93 | +} |
0 commit comments