[SCOI2005]繁忙的都市 (最小生成树)
题目链接
Solution
裸的最小生成树.
Code
#include<bits/stdc++.h>
using namespace std;
const int maxn=500008;
struct sj{int to,fr,w;}a[maxn];
bool cmp(sj x,sj y)
{return x.w<y.w;}
int n,fa[maxn],ans,m;
int find(int x)
{if(x==fa[x])return x;else return fa[x]=find(fa[x]);}
void join(int x,int y)
{x=find(x); y=find(y);if(x!=y)fa[x]=y;}
int read()
{
char ch=getchar(); int f=1,w=0;
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){w=w*10+ch-'0';ch=getchar();}
return f*w;
}
int main()
{
n=read(); m=read();
for(int i=1;i<=n;i++)
fa[i]=i;
for(int i=1;i<=m;i++)
{a[i].fr=read();a[i].to=read();a[i].w=read();}
sort(a+1,a+m+1,cmp);
for(int i=1;i<=m;i++)
{
if(find(a[i].fr)!=find(a[i].to))
join(a[i].fr,a[i].to),ans=max(ans,a[i].w);
}
cout<<n-1<<' '<<ans<<endl;
}