标签:find 节点 连接 include stream main algorithm mes sort
#include<cstdio> #include<iostream> #include<algorithm> #include<vector> #include<string> #include<queue> #include<map> #include<stack> using namespace std; const int maxn = 1e5 +10 ; struct EDGE { int u,v,w; }e[maxn]; int f[maxn]; int find(int x) { if(x != f[x]) f[x] = find(f[x]); return f[x]; } bool cmp(EDGE x,EDGE y) { return x.w<y.w; } void Kruskal(int m) { int ans = -100; for(int i=1;i<=m;i++) { int fu = find(e[i].u); //u的祖先节点 int fv = find(e[i].v); //v的祖先节点 if(fu != fv) { f[fu] = fv; //使2个祖先节点相连接 //f[e[i].u] = e[i].v; 错误点 //cout<<e[i].u<<" "<<e[i].v<<" "<<fu<<" "<<fv<<endl; if(ans < e[i].w) ans = e[i].w; } } cout<<ans; } int main() { int n,m,root; cin>>n>>m>>root; for(int i=1;i<=n;i++) { f[i] = i; } for(int i=1;i<=m;i++) { cin>>e[i].u>>e[i].v>>e[i].w; } sort(e+1,e+1+m,cmp); Kruskal(m); return 0; }
标签:find 节点 连接 include stream main algorithm mes sort
原文地址:https://www.cnblogs.com/fzuhyj/p/10529531.html