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

最小生成树 : 最大边

时间:2021-02-17 14:57:50      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:blank   syn   false   ios   sort   amp   res   www   inline   

https://www.acwing.com/problem/content/1144/

#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define x first
#define y second
#define inf 0x3f3f3f3f
const int N = 310, M = 10010;
int n, m;
int p[N];

struct Edge {
    int a, b, w;
    bool operator< (const Edge &t) const {
        return w < t.w;
    }
}e[M];

int find(int x) {
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}

int main() {
    IO;
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) p[i] = i;
    for (int i = 0; i < m; ++i) {
        int a, b, w;
        cin >> a >> b >> w;
        e[i] = {a, b, w};
    }
    sort(e, e + m);
    int res = 0;
    for (int i = 0; i < m; ++i) { // kruskal
        int a = find(e[i].a), b = find(e[i].b), w = e[i].w;
        if (a != b) {
            p[a] = b;
            res = w;
        }
    }
    cout << n - 1 << " " << res << ‘\n‘;
    return 0;
}

最小生成树 : 最大边

标签:blank   syn   false   ios   sort   amp   res   www   inline   

原文地址:https://www.cnblogs.com/phr2000/p/14402944.html

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