蒟蒻求助一道水题,大佬帮忙看看。
查看原帖
蒟蒻求助一道水题,大佬帮忙看看。
112109
Alphaban楼主2020/8/14 00:04

Wa #2

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int n, m, f[20005], e[20005];
struct Node{
    int x, y, res;
}a[100005];
int find(int x) {
    if (f[x] == x)
        return x;
     else return f[x] = find(f[x]);   
}
bool cmp(Node a, Node b) {
    return a.res > b.res;
}
void Update(int x, int y) {
    int A = find(x), B = find(y);
    if (A == B)
        return;
    f[A] = B;   
}
int main() {
    ios::sync_with_stdio(0);
    cin >> n >> m;
    for(int i = 1; i <= m; ++i)
        cin >> a[i].x >> a[i].y >> a[i].res;
    for(int i = 1; i <= n; ++i)
        f[i] = i;    
    sort(a + 1, a + m + 1, cmp);    
    for(int i = 1; i <= m; ++i) {
        int A = find(a[i].x), B = find(a[i].y);
        if (A == B) {
            cout << a[i].res << endl;
            return 0;
        }
        else {
            if (e[a[i].x] == 0)
                e[a[i].x] = a[i].y;
            else {
                Update(e[a[i].x], a[i].y);
                e[a[i].x] = 0;
            }    
            if (e[a[i].y] == 0)
                e[a[i].y] == a[i].x;
            else {
                Update(e[a[i].y], a[i].x);
                e[a[i].y] = 0;
            }        
        }
    }
    cout << 0 << endl;
    return 0;
}
2020/8/14 00:04
加载中...