#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
相关推荐
Ada's5 小时前
【计算机基础系列】003:Python数据结构Nil2086 小时前
leetcode 118杨辉三角wanderist.8 小时前
线性筛法详解:从筛质数到欧拉函数、Möbius 函数与约数函数All for pursuit.8 小时前
【设计-1】208.实现Trie (前缀树)weixin_307779138 小时前
C++代码实现MATLAB中的crossval函数功能青少儿编程课堂9 小时前
背包问题(0/1 背包与完全背包)解题精讲——动态规划入门光影少年9 小时前
为什么 JavaScript 中 0.1 + 0.2 !== 0.3,如何让其相等?HEJOO99 小时前
Linux 设备驱动系列——使用全局工作队列Code_Solitude10 小时前
c语言:递归程序员清风10 小时前
哈希表:从键值映射到高效查找