1693758898 题解分享 · 2025/4/10
最小生成树 - 题解
板子题` include using namespace std; using ll = long long; const int N = 1e6 + 9; struct node{ ll u,v,w; bool operator es; signed main(){ ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll n,m;cin>>n>>m; for(int i=1;i >u>>v>>w; es.push_back({u,v,w}); } ll ans = 0; sort(es.begin(),es.end()); for(int i=1;i<=n;i++) fa[i] = i; ``` for(auto &y : es){ if(root(y.u)==root(y.v)) continue; ans += y.w; fa[root(y.v)] = root(y.u); } ``` for(int i = 1; i < n; i++) if(root(i) != root(i + 1)) ans = -1; if(ans!=-1) cout << ans << "\n"; else cout<<"impossible"; } ``` ```
查看全文
0 0 0 5