【并查集】关押罪犯
#include <bits/stdc++.h>
using namespace std;
const int maxn=20010;
const int maxm=100010;
struct node {
int x,y,w;
bool operator<(const node &b)const {
return w>b.w;
}
}a[maxm];
int f[maxn*2];
int n,m;
int getfa(int x) {
return x == f[x] ? x : f[x] = getfa(f[x]);
}
void merge(int x,int y) {
int xx = getfa(x), yy = getfa(y);
if (xx != yy) f[xx] = y;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n * 2; i++) {
f[i] = i;
}
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].w);
}
sort(a + 1, a + 1 + m);
for (int i = 1; i <= m; i++) {
if (getfa(a[i].x) == getfa(a[i].y)) {
printf("%d\n", a[i].w);
return 0;
}
merge(a[i].x + n, a[i].y);
merge(a[i].y + n, a[i].x);
}
printf("0\n");
}