码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ 1083 [SCOI2005]繁忙的都市

时间:2017-09-18 10:52:52      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:生成树   div   for   long   log   main   algorithm   pen   splay   

最小生成树模板。

然后写并查集写了个fa[x]==find(fa[x]),RE。。。

十分难受

技术分享
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
typedef long long LL; 
const int maxn=40000+299;
using namespace std;
int n,m,u,v,w,fa[maxn],tot,ans;
struct edge {
    int u,v,w;
   friend bool operator <(const edge&a,const edge &b) {
        return a.w<b.w;
    }
}e[maxn];
int find(int x) {return x==fa[x]?x:fa[x]=find(fa[x]); }
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++) 
    scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
    sort(e+1,e+m+1);
    for(int i=1;i<=n;i++) fa[i]=i;
    for(int i=1;i<=m;i++) {
        u=e[i].u; v=e[i].v;
        int fau=find(u),fav=find(v);
        if(fau!=fav) {
            tot++;
            fa[fau]=fav;
            if(tot==n-1) {ans=e[i].w;break;}
        }
    }
    printf("%d %d\n",tot,ans);
    return 0;
}
View Code

 

BZOJ 1083 [SCOI2005]繁忙的都市

标签:生成树   div   for   long   log   main   algorithm   pen   splay   

原文地址:http://www.cnblogs.com/Achenchen/p/7541328.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!