BZOJ 1083: [SCOI2005]繁忙的都市 裸的最小生成树

题目链接:

http://www.lydsy.com/JudgeOnline/problem.php?id=1083

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

const int maxn = 333;
const int maxm = maxn*maxn;

struct Edge {
    int u, v, w;
    bool operator < (const Edge& tmp) const {
        return w < tmp.w;
    }
}egs[maxm];

int n, m;

int fat[maxn];
int Find(int x) {
    return fat[x] = fat[x] == x ? x : Find(fat[x]);
}

int main() {
    while (scanf("%d%d", &n, &m) == 2) {
        for (int i = 0; i <= n; i++) fat[i] = i;
        for (int i = 0; i < m; i++) {
            scanf("%d%d%d", &egs[i].u, &egs[i].v,&egs[i].w);
        }
        sort(egs, egs + m);
        int ma = -1;
        for (int i = 0; i < m; i++) {
            int pu = Find(egs[i].u);
            int pv = Find(egs[i].v);
            if (pu != pv) {
                ma = max(ma, egs[i].w);
                fat[pv] = pu;
            }
        }
        printf("%d %d\n", n - 1, ma);
    }
    return 0;
}

 

posted @ 2016-05-31 11:43  fenicnn  阅读(211)  评论(0编辑  收藏  举报