题目
#include <bits/stdc++.h>
using namespace std;
int father[1020], n, m;
struct node {
int x, y, t;
} a[100020];
bool cmp(node a, node b) {
return a.t < b.t;
}
int find(int x) {
while (x != father[x])
x = father[x];
return x;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
father[i] = i;
for (int i = 1; i <= m; i++)
cin >> a[i].x >> a[i].y >> a[i].t;
sort(a + 1, a + n + 1, cmp);
for (int i = 1; i <= m; i++) {
int findx = find(a[i].x), findy = find(a[i].y);
if (findx != findy) {
father[findy] = findx;
n--;
}
if (n == 1) {
cout << a[i].t;
return 0;
}
}
cout << -1;
}