-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp6.cpp
More file actions
61 lines (45 loc) · 954 Bytes
/
Copy pathp6.cpp
File metadata and controls
61 lines (45 loc) · 954 Bytes
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
52
53
54
55
56
57
58
59
60
61
#include<iostream>
#include<vector>
#include<algorithm>
#include<array>
#include<numeric>
using namespace std;
using int3=array<int,3>;
#define all(x) x.begin(),x.end()
class DSU{
public:
DSU(int n):fa(n+1){
iota(all(fa),0);
}
int getf(int x){
return x==fa[x]?x:fa[x]=getf(fa[x]);
}
bool conn(int x,int y){
return getf(x)==getf(y);
}
void merge(int x,int y){
fa[getf(x)]=getf(y);
}
private:
vector<int>fa;
};
int main(){
int n,m; cin>>n>>m;
vector<int3>edge(m);
for(auto &[u,v,w]:edge)
cin>>u>>v>>w;
sort(edge.begin(),edge.end(),[](const int3 &a,const int3 &b){
return a[2]<b[2];
});
DSU dsu(n);
for(auto [u,v,w]:edge){
if(!dsu.conn(u,v))
dsu.merge(u,v);
if(dsu.conn(1,n)){
cout<<w<<endl;
return 0;
}
}
cout<<-1<<endl;
return 0;
}