for (int i = 1; i <= n; i++) p[i] = i; sort(edge, edge + m); 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) continue; else { p[a] = b; cnt++; res += w; } } if (cnt == n - 1) //cnt记录边数, n 个点对应n - 1条边 cout << res; else puts("impossible");