[NOIP2010 提高组] 关押罪犯 - 洛谷https://www.luogu.com.cn/problem/P1525
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
#include <cstdlib>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 2000010;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false)
int n, m;
struct node {
int x, y, cost;
} v[MAXN];
inline bool cmp(node &a, node &b) {
return a.cost > b.cost;
}
int pre[MAXN];
inline void init(int n) {
for (int i = 1; i <= n; i++) {
pre[i] = i;
}
}
int find(int x) {
if (pre[x] == x) {
return x;
}
return pre[x] = find(pre[x]);
}
inline void unite(int x, int y) {
x = find(x);
y = find(y);
pre[x] = y;
}
inline bool same(int x, int y) {
return find(x) == find(y);
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d %d %d", &v[i].x, &v[i].y, &v[i].cost);
}
init(n * 2);
sort(v + 1, v + m + 1, cmp);
for (int i = 1; i <= m; i++) {
if (same(v[i].x, v[i].y) || same(v[i].x + n, v[i].y + n)) {
printf("%d", v[i].cost);
return 0;
} else {
unite(v[i].x, v[i].y + n);
unite(v[i].x + n, v[i].y);
}
}
printf("0");
return 0;
}