BZOJ 1083: [SCOI2005]繁忙的都市

二次联通门 : BZOJ 1083: [SCOI2005]繁忙的都市

 

 

 

 

/*
    BZOJ 1083: [SCOI2005]繁忙的都市

    
    最小生成树

*/
#include <cstdio>
#include <iostream>
#define rg register
#include <algorithm>
inline void read (int &n)
{
    rg char c = getchar ();
    for (n = 0; !isdigit (c); c = getchar ());
    for (; isdigit (c); n = n * 10 + c - '0', c = getchar ());
}
#define Max 1000005
struct D 
{
    int u, v, d;
    D (int a = 0, int b = 0, int c = 0) : u (a), v (b), d (c) { }
    bool operator < (const D &rhs) const
    { return d < rhs.d; }
} a[Max];
int f[Max];
inline int Find (int x) { return x == f[x] ? x : f[x] = Find (f[x]); }
inline void cmax (int &a, int b) { if (b > a) a = b; }
int main (int argc, char *argv[])
{
    int N, M; read (N), read (M); rg int i, j;
    for (i = 1; i <= M; ++ i)
        read (a[i].u), read (a[i].v), read (a[i].d);

    std :: sort (a + 1, a + 1 + M);
    for (i = 1; i <= N; ++ i) f[i] = i;
    int res = 0, x, y, C = 0;
    for (i = 1; i <= M; ++ i)
    {
        x = Find (a[i].u), y = Find (a[i].v);

        if (x != y)
        {
            ++ C, f[x] = y, cmax (res, a[i].d);
            if (C == N - 1) break;
        }
    }
    std :: cout << N - 1 << " " << res;

    return 0;
}

 

posted @ 2017-12-28 20:13  ZlycerQan  阅读(119)  评论(0编辑  收藏