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
| #include <bits/stdc++.h> using namespace std; const int N = 2010, P = 1e9 + 7; struct Edge { int x, y, z; bool operator<(Edge b) { return z < b.z; } } e[N]; int n, ans, cur, ok, f[N], d[N][N]; vector<int> v[N]; queue<pair<int, int>> q; int findr(int x) { return (f[x] == x ? x : f[x] = findr(f[x])); } int main() { scanf("%d", &n); for (int i = 1; i <= n - 1; i++) scanf("%d%d%d", &e[i].x, &e[i].y, &e[i].z);
for (int i = 1; i <= n; i++) f[i] = i, v[i].push_back(i); sort(e + 1, e + n); ans = ok = cur = 1; for (int i = 1; i <= n * (n - 1) / 2; i++) { if (cur <= n - 1 && e[cur].z == i) { d[e[cur].x][e[cur].y] = d[e[cur].y][e[cur].x] = i; int fx = findr(e[cur].x), fy = findr(e[cur].y); f[fx] = fy; int lenx = v[fx].size(), leny = v[fy].size(); for (int i = 0; i < lenx; i++) { for (int j = 0; j < leny; j++) { if (v[fx][i] != e[cur].x || v[fy][j] != e[cur].y) q.push(make_pair(v[fx][i], v[fy][j])); } } for (int i = 0; i < lenx; i++) v[fy].push_back(v[fx][i]); cur++; } else { if (q.empty()) { ans = ok = 0; break; } ans = (1ll * ans * (long long)(q.size())) % P; d[q.front().first][q.front().second] = d[q.front().second][q.front().first] = i; q.pop(); } } printf("%d\n", ans); if (ok) { for(int i = 1; i <= n; i++,puts("")) for(int j = 1; j <= n; j++) printf("%d ", d[i][j]); } return 0; }
|