-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLuogu_P_1547.cpp
35 lines (34 loc) · 952 Bytes
/
Luogu_P_1547.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
#include <bits/extc++.h>
using namespace std;
namespace pbds = __gnu_pbds;
using ui = unsigned int;
struct edge {
size_t u, v;
ui w;
friend bool operator<(edge const& x, edge const& y) { return x.w < y.w; }
};
class dsds {
vector<size_t> fa;
size_t find(size_t a) { return fa[a] == a ? a : fa[a] = find(fa[a]); }
public:
dsds(size_t n): fa(n) { iota(fa.begin(), fa.end(), 0); }
void merge(size_t x, size_t y) { fa[find(x)] = find(y); }
bool in_one_set(size_t x, size_t y) { return find(x) == find(y); }
};
int main(void) {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
size_t n, m;
cin >> n >> m;
vector<edge> e(m);
for (edge& i : e) cin >> i.u >> i.v >> i.w, i.u--, i.v--;
sort(e.begin(), e.end());
dsds ds(n);
size_t cnt = 0;
for (edge const& i : e)
if (!ds.in_one_set(i.u, i.v)) {
ds.merge(i.u, i.v);
if (++cnt == n - 1) return cout << i.w, 0;
}
cout << -1;
return 0;
}