比赛 防止颓废的小练习v0.2 评测结果 AAAAAAAAAA
题目名称 关押罪犯 最终得分 100
用户昵称 KZNS 运行时间 0.122 s
代码语言 C++ 内存使用 1.38 MiB
提交时间 2016-10-18 10:04:39
显示代码纯文本
//KZNS
#include <cstdio>
#include <algorithm>
using namespace std;
#define Nmax 20003
#define Mmax 100003
class poi {
    public:
        int a, b, c;
};
bool operator < (const poi &a, const poi &b) {
    return a.c > b.c;
}
int N, M;
poi ls[Mmax];
int fa[Nmax];
bool fd[Nmax] = {0};
void rin() {
    scanf("%d %d", &N, &M);
    for (int i = 0; i < M; i++)
        scanf("%d %d %d", &ls[i].a, &ls[i].b, &ls[i].c);
    for (int i = 1; i <= N; i++)
        fa[i] = i;
}
int FA(int x) {
    if (fa[x] == x)
        return x;
    int f = fa[x];
    fa[x] = FA(fa[x]);
    fd[x] ^= fd[f];
    return fa[x];
}
void work() {
    sort(ls, ls+M);
    int ans = 0;
    int a, b, c;
    for (int i = 0; i < M; i++) {
        a = ls[i].a;
        b = ls[i].b;
        c = ls[i].c;
        if (FA(a) == FA(b)) {
            if (fd[a] == fd[b]) {
                ans = c;
                break;
            }
        }
        else {
            fd[fa[a]] = !(fd[a] ^ fd[b]);
            fa[fa[a]] = fa[b];         
        }
    }
    printf("%d\n", ans);
}
int main() {
    freopen("prison1.in", "r", stdin);
    freopen("prison1.out", "w", stdout);
    rin();
    work();
    return 0;
}
//UBWH