voidunionn(int x, int y) { int a = find(x); int b = find(y); if (a != b) { ufs[a] = b; } }
constint maxm = 100010;
structEdge { int a, b, w; bool select; }edge[maxm];
boolcmp(Edge a, Edge b) { if (a.w != b.w) return a.w < b.w; if (a.a != b.a) return a.a < b.a; return a.b < b.b; }
voidkruskal() { for (int i = 1; i <= n; ++i) { ufs[i] = i; } int k = 0, x, y; sort(edge + 1, edge + 1 + m, cmp); for (int i = 1; i <= m; ++i) { if (k == n - 1) break; x = find(edge[i].a); y = find(edge[i].b); if (x != y) { unionn(x, y); k++; edge[i].select = true; } } }