#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
相关推荐
归去_来兮10 小时前
拉格朗日插值算法原理及简单示例千寻girling16 小时前
Python 是用来做 AI 人工智能 的 , 不适合开发 Web 网站 | 《Web框架》颜酱19 小时前
一步步实现字符串计算器:从「转整数」到「带括号与优化」CoovallyAIHub2 天前
语音AI Agent编排框架!Pipecat斩获10K+ Star,60+集成开箱即用,亚秒级对话延迟接近真人反应速度!NineData2 天前
数据库管理工具NineData,一年进化成为数万+开发者的首选数据库工具?木心月转码ing2 天前
Hot100-Day14-T33搜索旋转排序数组会员源码网2 天前
内存泄漏(如未关闭流、缓存无限增长)颜酱2 天前
从0到1实现LFU缓存:思路拆解+代码落地颜酱2 天前
从0到1实现LRU缓存:思路拆解+代码落地