社区讨论
60分求条
P1525[NOIP 2010 提高组] 关押罪犯参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @mlj1e01k
- 此快照首次捕获于
- 2026/02/12 13:47 上周
- 此快照最后确认于
- 2026/02/14 21:35 5 天前
CPP
#include <bits/stdc++.h>
using namespace std;
#define int long long
// #define int __int128
// #define int unsigned long long
const int N = 2e4 + 10, M = 1e5 + 10;
struct ren {
int u, v, w;
} fan[M];
int fa[N];
int n, m;
bool cmp (ren x, ren y) {
return x.w > y.w;
}
int find (int u) {
if (fa[u] == u) return u;
else return fa[u] = find (fa[u]);
}
void merge (int u, int v) {
int fx = find (u), fy = find (v);
if (fx != fy) {
fa[fx] = fy;
}
}
void query () {
for (int i = 1; i <= n; i++) fa[i] = i;
sort (fan + 1, fan + 1 + m, cmp);
for (int i = 1; i <= m; i++) {
int fx = find (fan[i].u), fy = find (fan[i].v);
if (fx == fy) {
cout << fan[i].w << "\n";
return;
} else {
merge (fx, fy);
}
}
cout << "0\n";
}
signed main () {
ios::sync_with_stdio (false);
cin.tie (0), cout.tie (0);
cin >> n >> m;
for (int i = 1; i <= m; i++) cin >> fan[i].u >> fan[i].v >> fan[i].w;
query ();
return 0;
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...