201812-4数据中心

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

const int INF = 0X3f3f3f3f;
const int maxn = 200010;

struct Edge
{
    int from, to, weight;
    void input()
    {
       cin>>from>>to>>weight;
    }
    bool operator < (const Edge & t) const
    {
        return weight < t.weight;
    }
};

int fa[maxn];
Edge edges[maxn];

int n, m;

void init()
{
    for(int i = 1; i <= n; i++)
    {
        fa[i] = i;
    }
}

int findset(int u)
{
    return fa[u] == u ? u : fa[u] = findset(fa[u]);
}

bool checkset(int x, int y)
{
    return findset(x) == findset(y);
}

void unionset(int x, int y)
{
    int p1 = findset(x), p2 = findset(y);
    if(p1 == p2)
    {
        return;
    }
    fa[p1] = p2;
}

int Kruskal()
{
    sort(edges, edges + m);
    int maxn=0;
    for(int i = 0; i < m; i++)
    {
        if(!checkset(edges[i].from, edges[i].to))
        {
            unionset(edges[i].from, edges[i].to);
            if(maxn<edges[i].weight) maxn=edges[i].weight;
        }
    }return maxn;

}

int main()
{
    cin>>n>>m;
    int root;
    cin>>root;
    init();
    for(int i = 0; i < m; i++)
    {
        edges[i].input();
    }
  cout<<Kruskal();
    return 0;
}

发布了38 篇原创文章 · 获赞 7 · 访问量 2771

猜你喜欢

转载自blog.csdn.net/zzyzzylalala/article/details/100824664
今日推荐