#include<algorithm> #include<iostream> usingnamespace std; constint M = 200010;
// 存储每条边,重载小于号,按照权重比较大小 structEdge { int a, b, w; booloperator<(const Edge& W) const { return w < W.w; } } e[M];
int p[M]; int n, m;
// 并查集 find 函数 intfind(int x){ if (x != p[x]) p[x] = find(p[x]); return p[x]; } intmain(){ cin >> n >> m; for (int i = 0; i < m; i++) { int a, b, c; cin >> a >> b >> c; e[i] = {a, b, c}; } // 将边按照从小到大排序 sort(e, e + m); for (int i = 1; i <= n; i++) p[i] = i; int res = 0, cnt = 0; // 枚举每条边 for (int i = 0; i < m; i++) { int a = e[i].a, b = e[i].b, w = e[i].w; // 检查这条边的两个点是否连通 a = find(a), b = find(b); if (a != b) { p[a] = b; res += w; cnt++; } } if (cnt < n - 1) cout << "impossible" << endl; else cout << res << endl; return0; }
constint N = 510, M = 1e5 + 10; int n1, n2, m; int h[N], e[M], ne[M], idx; int match[N]; bool st[N]; voidadd(int a, int b){ e[idx] = b; ne[idx] = h[a]; h[a] = idx++; }