53 lines
1.3 KiB
C++
53 lines
1.3 KiB
C++
#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;
|
||
} |