就是用并查集做的,也没看出来和题解里的ac代码有什么本质区别
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int p = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
p = p * 10 + c - '0';
c = getchar();
}
return f * p;
}
struct A {
int b, e, w;
} p[100010];
int q[10010];
int r[10010];
bool cmp(A a, A b) {
if (a.w > b.w)
return true;
else
return false;
}
int find(int x) {
if (q[x] == x)
return x;
else {
q[x] = find(q[x]);
return q[x];
}
}
int main () {
int n, m;
cin >> n >> m;
memset(r, 0, sizeof(r));
int a, b;
for (int i = 1; i <= m; i++) {
p[i].b = read();
p[i].e = read();
p[i].w = read();
}
sort(p + 1, p + m + 1, cmp);
for (int i = 1; i < n; i++) {
q[i] = i;
}
for (int i = 1; i <= m; i++) {
int t1 = find(p[i].b);
int t2 = find(p[i].e);
if (t1 == t2) {
cout << p[i].w;
return 0;
}
if (!r[p[i].b])
r[p[i].b] = p[i].e;
else
q[find(r[p[i].b])] = p[i].e;
if (!r[p[i].e])
r[p[i].e] = p[i].b;
else
q[find(r[p[i].e])] = p[i].b;
}
cout << "0";
}