You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
const int N = 20010;
const int M = 100010;
int n, m, x, y;
//fa[i]表示i所属的集合用fa[i+n]表示i所属集合的补集实现的很巧妙可以当成一个使用并查集的巧妙应用
int fa[N << 1]; //并查集数组,有时也写成 N << 1 ,从左到右读作N左移1位就是 2*N
//要深入理解这个递归并压缩的过程
int find(int x) {
if (fa[x] != x) fa[x] = find(fa[x]);
return fa[x];
}
//加入家族集合中
void join(int c1, int c2) {
int f1 = find(c1), f2 = find(c2);
if (f1 != f2)fa[f1] = f2;
}
//关系
struct node {
int x; //第一个人
int y; //第二个人
int v; //冲突值
} a[M];
//排序函数,按冲突值大小排序,大的在前
bool cmp(const node &a, const node &b) {
return a.v > b.v;
}
int main() {
//读入
cin >> n >> m;
for (int i = 1; i <= m; i++) cin >> a[i].x >> a[i].y >> a[i].v;
//排序
sort(a + 1, a + m + 1, cmp);
//初始化并查集,每个人都是自己的祖先
for (int i = 1; i <= 2 * n; i++)fa[i] = i;//注意这里是2*n
//处理m组关系
for (int i = 1; i <= m; i++) {
x = find(a[i].x);
y = find(a[i].y);
if (x == y) {
cout << a[i].v << endl;
return 0;
} else
join(a[i].y + n, x), join(a[i].x + n, y);
}
cout << 0 << endl;
return 0;
}