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.
46 lines
1.6 KiB
46 lines
1.6 KiB
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
const int N = 100010, M = 20000; // 这两个数字的顺序有点反人类
|
|
|
|
// 结构体
|
|
struct Node {
|
|
int x, y, w;
|
|
const bool operator<(const Node &t) const {
|
|
return w > t.w;
|
|
}
|
|
} a[N];
|
|
|
|
// 带权并查集模板
|
|
int n, m, p[M], d[M];
|
|
int find(int x) {
|
|
if (x == p[x]) return x; // 递归出口,自己是老大
|
|
int px = find(p[x]); // 他爸是哪家的,他就是哪家的
|
|
d[x] += d[p[x]]; // 插入广告:把我爸爸的路径长度都加给我,我好知道我和祖先的权值关系
|
|
return p[x] = px; // 路径压缩记录家庭
|
|
}
|
|
|
|
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].w);
|
|
// 按自定义的仇恨值由大到小排序
|
|
sort(a + 1, a + m + 1);
|
|
|
|
for (int i = 1; i <= n; i++) p[i] = i; // 并查集初始化
|
|
|
|
for (int i = 1; i <= m; i++) {
|
|
// 由于是排好序的,所以仇恨值由大到小,现在枚举到的,肯定都是仇恨值高的,
|
|
// 这样的应该向不同的监狱分配人员,如果最后分不下去了,就是找到了最小的仇恨值
|
|
int x = a[i].x, y = a[i].y;
|
|
int px = find(x), py = find(y); // 每个人自己的家庭
|
|
if (px != py) {
|
|
d[px] = d[y] - d[x] + 1; // 更新距离
|
|
p[px] = py; // 合并并查集
|
|
} else if ((d[y] - d[x]) % 2 == 0) { // 发现了冲突
|
|
printf("%d\n", a[i].w);
|
|
return 0;
|
|
}
|
|
}
|
|
printf("%d\n", 0);
|
|
return 0;
|
|
} |