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

BZOJ 1083: [SCOI2005]繁忙的都市

时间:2017-12-28 23:25:16      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:span   std   最小   tar   find   inline   getchar   max   cout   

二次联通门 : 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;
}

 

BZOJ 1083: [SCOI2005]繁忙的都市

标签:span   std   最小   tar   find   inline   getchar   max   cout   

原文地址:https://www.cnblogs.com/ZlycerQan/p/8137591.html

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