【SCOI2005】【BZOJ1083】繁忙的都市(MST模板)

problem

给定一张图,求其最小生成树中的边数和权值最大的边

solution

裸的Kruskal

codes

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 10010;

struct Edge{int u, v, w;}e[maxn];
bool cmp(Edge a, Edge b){return a.w<b.w;}

int fa[maxn];
int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
int init(int n){for(int i = 1; i <= n; i++)fa[i]=i;}

int main(){
    int n, m;  cin>>n>>m;
    for(int i = 1; i <= m; i++)
        cin>>e[i].u>>e[i].v>>e[i].w;
    sort(e+1,e+m+1,cmp);
    init(n);
    int ans1=0, ans2=-1;
    for(int i = 1; i <= m; i++){
        if(find(e[i].u)!=find(e[i].v)){
            fa[find(e[i].u)] = find(e[i].v);
            ans1++;
            ans2 = max(ans2, e[i].w);
        }
    }
    cout<<ans1<<' '<<ans2<<'\n';
    return 0;
}
posted @ 2018-05-28 20:35  gwj1139177410  阅读(48)  评论(0编辑  收藏
选择