-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwormsort.cpp
More file actions
103 lines (98 loc) · 2.46 KB
/
wormsort.cpp
File metadata and controls
103 lines (98 loc) · 2.46 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
template <typename T1, typename T2>
ostream &operator <<(ostream &os, pair<T1, T2> p){os << p.first << " " << p.second; return os;}
template <typename T>
ostream &operator <<(ostream &os, vector<T> &v){for(T i : v)os << i << ", "; return os;}
template <typename T>
ostream &operator <<(ostream &os, set<T> s){for(T i : s) os << i << ", "; return os;}
template <typename T1, typename T2>
ostream &operator <<(ostream &os, map<T1, T2> m){for(pair<T1, T2> i : m) os << i << endl; return os;}
int n, m;
int p[100000];
pair<int, pii> wh[100000];
bool works(int x){
if(x < 0){
return false;
}
vi graph[n];
for(int i = 0; i < x; i++){
int a = wh[i].second.first;
int b = wh[i].second.second;
graph[a].push_back(b);
graph[b].push_back(a);
}
int group[n];
fill(group, group+n, -1);
for(int i = 0; i < n; i++){
if(group[i] != -1){
continue;
}
queue<int> bfs;
bfs.push(i);
while(!bfs.empty()){
int idx = bfs.front();
bfs.pop();
if(group[idx] != -1){
continue;
}
group[idx] = i;
for(int j : graph[idx]){
bfs.push(j);
}
}
}
for(int i = 0; i < n; i++){
if(group[p[i]] != group[i]){
return false;
}
}
return true;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
freopen("wormsort.in", "r", stdin);
freopen("wormsort.out", "w", stdout);
cin >> n >> m;
for(int i = 0; i < n; i++){
cin >> p[i];
p[i]--;
}
for(int i = 0; i < m; i++){
cin >> wh[i].second.first >> wh[i].second.second >> wh[i].first;
wh[i].second.first--;
wh[i].second.second--;
}
sort(wh, wh+m, greater<pair<int, pii>>());
int low = 0;
int high = m;
int mid;
while(low < high){
mid = (low + high) / 2;
bool ok = works(mid);
bool ok1 = works(mid-1);
if(ok && !ok1){
break;
}else if(ok){
high = mid-1;
}else{
low = mid+1;
}
}
while(works(mid) && mid > 0){
mid--;
}
while(!works(mid)){
mid++;
}
if(mid == 0){
cout << -1 << endl;
}else{
cout << wh[mid-1].first << endl;
}
return 0;
}