【题解】luogu p1111 修复公路
并查集
总结:
1.并查集板子掌握不熟练
#include<bits/stdc++.h> using namespace std; int n, m, fa[1005], t, sum[1005]; struct node{ int u, v, t; }a[100005]; bool cmp(node x, node y) { return x.t < y.t; } int find(int x) { if(fa[x] == x) return x; else return fa[x] = find(fa[x]); } void hb(int x, int y, int z) { if(find(x) != find(y)) { fa[find(x)] = find(y); sum[find(y)] += sum[find(x)]; } if(sum[find(y)] == n) { cout << a[z].t; exit(0); } } int main() { cin >> n >> m; for(int i = 1; i <= m; i++) cin >> a[i].u >> a[i].v >> a[i].t; sort(a+1, a+1+m, cmp); for(int i = 1; i <= n; i++) fa[i] = i, sum[i] = 1; for(int i = 1; i <= m; i++) hb(a[i].u, a[i].v, i); cout << "-1"; return 0; }