voidsolve(){ cin >> n >> m; for (int i = 0; i < m; i++) { int a, b, w; cin >> a >> b >> w; edge[i] = {a, b, w}; } sort(edge, edge + m, cmp); for (int i = 1; i <= n; i++) p[i] = i; int ans = 0, cnt = 0; for (int i = 0; i < m; i++) { int a = edge[i].a, b = edge[i].b, w = edge[i].w; a = find(a); b = find(b); if (a != b) { p[a] = b; ans += w; cnt++; } } if (cnt < n - 1) { cout << "impossible" << endl; } else { cout << ans << endl; } }
signedmain(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; // cin >> t; while (t--) { solve(); } return0; }
voidsolve(){ cin >> n >> m >> k; for (int i = 0; i < m; i++) { int x, y, l; cin >> x >> y >> l; edge[i] = {x, y, l}; } for (int i = 1; i <= n; i++) { p[i] = i; } sort(edge, edge + m, cmp); int cnt = 0, ans = 0; for (int i = 0; i < m; i++) { int x = edge[i].x, y = edge[i].y, l = edge[i].l; x = find(x); y = find(y); if (x != y) { p[x] = y; cnt++; ans += l; } if (cnt >= n - k) break; } // 棉花糖可以理解为连通块 if (cnt >= n - k) { cout << ans << endl; } else { cout << "No Answer" << endl; } }
signedmain(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; // cin >> t; while (t--) { solve(); } return0; }