您的位置:首页 > 其它

bzoj1083[SCOI2005]繁忙的都市

2016-08-26 10:01 387 查看
刷水题~

一道裸的最小生成树。。(不解释了)

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
int x,y,c;
}a[110000];
int cmp(const void *xx,const void *yy)
{
node n1=*(node *)xx;
node n2=*(node *)yy;
if(n1.c>n2.c)return 1;
else if(n1.c<n2.c)return -1;
return 0;
}
int fa[110000];
int findfa(int x)
{
if(x==fa[x])return x;
return findfa(fa[x]);
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].c);
}
qsort(a+1,m,sizeof(node),cmp);
for(int i=1;i<=n;i++)fa[i]=i;
int ans=0,d;
for(int i=1;i<=m;i++)
{
int fx=findfa(a[i].x),fy=findfa(a[i].y);
if(fx!=fy)
{
fa[fx]=fy;
ans++;if(ans==n-1)d=a[i].c;
}
}
printf("%d %d",ans,d);
return 0;
}


by_yzh
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: