题解:
简单最小生成树
代码:
#include<bits/stdc++.h> using namespace std; #define y1 ____y1 const int N=10005; int z[N],f[N],x[N],y[N],n,m,fa[N]; int find(int x) { if (x==fa[x])return x; fa[x]=find(fa[x]); return fa[x]; } int cmp(int x,int y) { return z[x]<z[y]; } int main() { scanf("%d%d",&n,&m); for (int i=1;i<=m;i++) { scanf("%d%d%d",&x[i],&y[i],&z[i]); f[i]=i; } for (int i=1;i<=n;i++)fa[i]=i; sort(f+1,f+m+1,cmp); int tot=0,ans=0; for (int i=1;i<=m;i++) { int x1=find(x[f[i]]),y1=find(y[f[i]]); if (x1!=y1) { fa[x1]=fa[y1]; tot++; ans=z[f[i]]; } } printf("%d %d",tot,ans); }