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.

53 lines
1.3 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, M = 100010;
int n, m, x, y;
// p[i]表示i所属的集合用p[i+n]表示i所属集合的补集实现的很巧妙可以当成一个使用并查集的巧妙应用
int p[N << 1]; //并查集数组
int find(int x) {
if (x == p[x]) return x;
return p[x] = find(p[x]);
}
//合并集合
bool join(int a, int b) {
if (find(a) == find(b)) return false;
p[find(a)] = find(b);
return true;
}
//关系
struct Node {
int x, y, v; //冲突值
//排序函数,按冲突值大小排序,大的在前
const bool operator<(const Node &t) const {
return v > t.v;
}
} a[M];
int main() {
//读入
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; i++) scanf("%d %d %d", &a[i].x, &a[i].y, &a[i].v);
//排序,按冲突值由大到小排序
sort(a + 1, a + m + 1);
//初始化扩展域并查集,每个人都是自己的祖先
for (int i = 1; i <= 2 * n; i++) p[i] = i;
//处理m组关系
for (int i = 1; i <= m; i++) {
int px = find(a[i].x), py = find(a[i].y);
if (px == py) {
printf("%d\n", a[i].v);
exit(0);
} else
join(a[i].y + n, px), join(a[i].x + n, py);
}
printf("%d\n", 0);
return 0;
}