-
Notifications
You must be signed in to change notification settings - Fork 0
/
11085_union_find.cpp
51 lines (50 loc) · 1015 Bytes
/
11085_union_find.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// 210322 #11085 ±º»ç À̵¿ Gold II
// I think it's Gold IV ~ Gold III
// like mst, but not mst. (reverse mst) union-find
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int MAX = 1001;
int N, M, S, E, p[MAX], ans;
vector <pair<int, pair<int, int>>> v;
int find(int a) {
if (a == p[a])
return a;
return p[a] = find(p[a]);
}
bool merge(int a, int b) {
a = find(a);
b = find(b);
if (a == b)
return false;
p[b] = a;
return true;
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
cin >> N >> M >> S >> E;
for (int i = 0; i < N; i++) {
p[i] = i;
}
for (int i = 0; i < M; i++) {
int a, b, cost;
cin >> a >> b >> cost;
v.push_back({ cost, {a,b} });
}
sort(v.begin(), v.end());
for (int i = v.size() - 1; i >= 0; i--) {
int node1 = v[i].second.first;
int node2 = v[i].second.second;
int cost = v[i].first;
merge(node1, node2);
if (find(S) == find(E)) {
ans = cost;
break;
}
}
cout << ans << "\n";
}