#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
#define MAX_N 20000
struct Node {
int a, b, c;
};
set<int> s[MAX_N + 5];
int fa[MAX_N + 5], n, m;
int get(int x) {
return fa[x] = (fa[x] == x ? x : get(fa[x]));
}
int main() {
cin >> n >> m;
vector<Node> arr(m);
for (int i = 0; i <= n; i++) fa[i] = i;
for (int i = 0, a, b, c; i < m; i++) {
cin >> arr[i].a >> arr[i].b >> arr[i].c;
}
sort(arr.begin(), arr.end(), [&](Node i, Node j) -> bool {return i.c > j.c;
});
for (int i = 0; i < m; i++) {
int a = arr[i].a, b = arr[i].b;
if (get(a) == get(b)) {
cout << arr[i].c;
return 0;
}
if (!s[a].empty()) {
fa[get(*s[a].begin())] = get(b);
}
if (!s[b].empty()) {
fa[get(*s[b].begin())] = get(a);
}
s[a].insert(b);
s[b].insert(a);
}
cout << 0;
return 0;
}
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
#define MAX_N 20000
class UnionSet {
public:
UnionSet(int n) : fa(n + 1), val(n + 1) {
for (int i = 0; i <= n; i++) {
fa[i] = i;
val[i] = 0;
}
}
int get(int x) {
if (fa[x] == x) return x;
int root = get(fa[x]);
val[x] = (val[x] + val[fa[x]]) % 2;
return fa[x] = root;
}
void merge(int a, int b, int t) {
int aa = get(a), bb = get(b);
if (aa == bb) return;
val[aa] = (t - val[a] + val[b] + 2) % 2;
fa[aa] = bb;
return;
}
vector<int> fa, val;
};
struct Data {
int a, b, c;
};
int main() {
int n, m;
cin >> n >> m;
UnionSet u(n);
vector<Data> arr(m);
for (int i = 0; i < m; i++) {
cin >> arr[i].a >> arr[i].b >> arr[i].c;
}
sort(arr.begin(), arr.end(), [&](const Data i, const Data j)->bool {
return i.c > j.c;
});
for (int i = 0; i < m; i++) {
int a = arr[i].a, b = arr[i].b;
if (u.get(a) == u.get(b)) {
if ((u.val[a] + u.val[b]) % 2 == 0) {
cout << arr[i].c;
return 0;
}
}
else {
u.merge(a, b, 1);
}
}
cout << 0;
return 0;
}
HZOJ-327:关押罪犯
云儿乱飘2023-10-20 17:51
相关推荐
Greedy Alg9 小时前
LeetCode 239. 滑动窗口最大值空白到白9 小时前
机器学习-KNN算法闪电麦坤9510 小时前
数据结构:排序算法的评判标准(Criteria Used For Analysing Sorts)爱coding的橙子10 小时前
每日算法刷题Day65:8.27:leetcode dfs11道题,用时2h30min不懂机器人10 小时前
linux网络编程-----TCP服务端并发模型(epoll)Mercury_Lc11 小时前
【链表 - LeetCode】25. K 个一组翻转链表地平线开发者11 小时前
理想汽车智驾方案介绍 3|MoE+Sparse Attention 高效结构解析小O的算法实验室12 小时前
2025年KBS SCI1区TOP,矩阵差分进化算法+移动网络视觉覆盖无人机轨迹优化,深度解析+性能实测艾莉丝努力练剑13 小时前
【C语言16天强化训练】从基础入门到进阶:Day 11浩少70215 小时前
LeetCode-22day:多维动态规划