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
| #include <bits/stdc++.h>
inline int rd() { int a = 1, b = 0; char c = getchar(); while (!isdigit(c)) a = c == '-' ? 0 : 1, c = getchar(); while (isdigit(c)) b = b * 10 + c - '0', c = getchar(); return a ? b : -b; }
const int N = 1e3 + 233, P = 31011;
int n, m, ans, fa[N], vis[N], st[N], pick[N], tot;
struct Edge { int x, y, c; } e[N];
bool operator<(const Edge &a, const Edge &b) { return a.c < b.c; }
int find(int x) { if (x == fa[x]) return x; return find(fa[x]); }
int dfs(int s, int d, int p) { if (d == st[s + 1]) { if (p == pick[s]) return 1; return 0; } int x = find(e[d].x), y = find(e[d].y), ret = 0; if (x != y) { fa[x] = y; ret += dfs(s, d + 1, p + 1); fa[x] = x; } return ret + dfs(s, d + 1, p); }
int main() { n = rd(), m = rd(); for (int i = 1; i <= m; ++i) e[i].x = rd(), e[i].y = rd(), e[i].c = rd(); std::sort(e + 1, e + m + 1); for (int i = 1; i <= n; ++i) fa[i] = i; for (int i = 1; i <= m; ++i) { int x = find(e[i].x), y = find(e[i].y); if (x != y) { fa[x] = y; vis[i] = 1; ++ans; } } if (ans != n - 1) { puts("0"); return 0; } for (int i = 1; i <= m; ++i) if (e[i].c != e[i - 1].c) st[++tot] = i; st[tot + 1] = m + 1; for (int i = 1; i <= tot; ++i) for (int j = st[i]; j < st[i + 1]; ++j) if (vis[j]) ++pick[i]; for (int i = 1; i <= n; ++i) fa[i] = i; ans = 1; for (int s = 1; s <= tot; ++s) { ans = ans * dfs(s, st[s], 0) % P; for (int i = st[s]; i < st[s + 1]; ++i) { int x = find(e[i].x), y = find(e[i].y); if (x != y) fa[x] = y; } } printf("%d\n", ans); return 0; }
|