时间复杂度( O ( m l o g N ) O(mlogN) O(mlogN))
模板
int p[N];
struct Node{
int a, b, w;
bool operator< (const Node& t) const {
return w < t.w;
}
}e[M];
int find(int x) {
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
for (int i = 0; i < m; i ++ ) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
e[i] = {a, b, c};
}
sort(e, e + m);
for (int i = 0; i < m; i ++ ) {
int pa = find(e[i].a), pb = find(e[i].b);
if (pa != pb) p[pa] = pb;
}