#include<bits/stdc++.h>
using namespace std;
vector<int> group[100010];
vector<pair<int, int> > pa[100010];
vector<pair<int, pair<int, int> > > mv;
int p[100010], cycle[100010], outd[100010], sizep[100010], cnt[100010];
int find(int x){
if(p[x] == x) return x;
return find(p[x]);
}
void init(int n, int m, vector<int> u, vector<int> v, vector<int> w){
for(int i = 0;i < n;i++){
sizep[i] = 1; p[i] = i;
group[i].push_back(i);
pa[i].push_back({0, i});
}
for(int i = 0;i < m;i++){
mv.push_back({w[i], {u[i], v[i]}});
}
sort(mv.begin(), mv.end());
/*for(int i = 0;i < m;i++){
printf("%d %d %d\n", mv[i].first, mv[i].second.first, mv[i].second.second);
}*/
for(int i = 0;i < m;i++){
int u1 = find(mv[i].second.first);
int u2 = find(mv[i].second.second);
cnt[mv[i].second.first]++; cnt[mv[i].second.second]++;
if(u1 == u2){
if(cycle[u1] == 0) cycle[u1] = mv[i].first;
if(cnt[mv[i].second.first] >= 3 || cnt[mv[i].second.second] >= 3){
if(outd[u1] == 0) outd[u1] = mv[i].first;
}
}
else{
if(sizep[u1] < sizep[u2]) swap(u1, u2);
for(int j = 0;j < group[u2].size();j++){
pa[group[u2][j]].push_back({mv[i].first, u1});
group[u1].push_back(group[u2][j]);
p[group[u2][j]] = u1;
}
sizep[u1] += sizep[u2];
if(cycle[u1] || cycle[u2]){
if(cycle[u2] == 0) cycle[u2] = mv[i].first;
if(cycle[u1] == 0) cycle[u1] = mv[i].first;
}
if(outd[u1] || outd[u2] || cnt[mv[i].second.first] >= 3 || cnt[mv[i].second.second] >= 3){
if(outd[u1] == 0) outd[u1] = mv[i].first;
if(outd[u2] == 0) outd[u2] = mv[i].first;
}
}
}
}
int getMinimumFuelCapacity(int X, int Y){
int x = X, y = Y, ans = 1000000001;
int s1 = 0, e1 = pa[x].size() - 1;
while(s1 < e1){
int mid = (s1+e1)/2;
if(cycle[pa[x][mid].second]||outd[pa[x][mid].second]) e1 = mid;
else s1 = mid+1;
}
int s2 = 0, e2 = pa[y].size() - 1;
while(s2 < e2){
int mid = (s2+e2)/2;
if(cycle[pa[y][mid].second]||outd[pa[y][mid].second]) e2 = mid;
else s2 = mid+1;
}
if(cycle[pa[x][s1].second]) ans = min(ans, cycle[pa[x][s1].second]);
if(outd[pa[y][s2].second]) ans = min(ans, outd[pa[y][s2].second]);
if(outd[pa[x][s1].second]) ans = min(ans, outd[pa[x][s1].second]);
if(cycle[pa[y][s2].second]) ans = min(ans, cycle[pa[y][s2].second]);
if(ans == 1000000001) return -1;
int s = 0, e = 1000000001;
while(s < e){
//printf("%d %d\n", s, e);
int mid = (s+e)/2;
s1 = 0, e1 = pa[x].size() - 1;
while(s1 < e1){
int mid1 = (s1+e1+1)/2;
if(pa[x][mid1].first > mid) e1 = mid1-1;
else s1 = mid1;
}
s2 = 0, e2 = pa[y].size() - 1;
while(s2 < e2){
int mid2 = (s2+e2+1)/2;
if(pa[y][mid2].first > mid) e2 = mid2-1;
else s2 = mid2;
}
if(pa[x][s1].second != pa[y][s2].second) s = mid+1;
else e = mid;
}
if(e == 1000000001) return -1;
else return max(ans, s);
}
Compilation message
swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:38:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int j = 0;j < group[u2].size();j++){
| ~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
5120 KB |
Output is correct |
5 |
Correct |
5 ms |
5248 KB |
Output is correct |
6 |
Correct |
4 ms |
5248 KB |
Output is correct |
7 |
Correct |
4 ms |
5248 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
179 ms |
21984 KB |
Output is correct |
10 |
Correct |
225 ms |
25452 KB |
Output is correct |
11 |
Correct |
224 ms |
25068 KB |
Output is correct |
12 |
Correct |
239 ms |
26348 KB |
Output is correct |
13 |
Correct |
137 ms |
18924 KB |
Output is correct |
14 |
Correct |
188 ms |
21996 KB |
Output is correct |
15 |
Correct |
339 ms |
27640 KB |
Output is correct |
16 |
Correct |
330 ms |
27164 KB |
Output is correct |
17 |
Correct |
351 ms |
28404 KB |
Output is correct |
18 |
Correct |
235 ms |
20820 KB |
Output is correct |
19 |
Correct |
194 ms |
11704 KB |
Output is correct |
20 |
Correct |
475 ms |
28904 KB |
Output is correct |
21 |
Correct |
478 ms |
28456 KB |
Output is correct |
22 |
Correct |
499 ms |
29780 KB |
Output is correct |
23 |
Correct |
338 ms |
22228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
288 ms |
19724 KB |
Output is correct |
4 |
Correct |
292 ms |
20052 KB |
Output is correct |
5 |
Correct |
293 ms |
20096 KB |
Output is correct |
6 |
Correct |
285 ms |
20168 KB |
Output is correct |
7 |
Correct |
291 ms |
20204 KB |
Output is correct |
8 |
Correct |
282 ms |
19612 KB |
Output is correct |
9 |
Correct |
287 ms |
19944 KB |
Output is correct |
10 |
Correct |
286 ms |
19624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
5120 KB |
Output is correct |
6 |
Correct |
5 ms |
5248 KB |
Output is correct |
7 |
Correct |
4 ms |
5248 KB |
Output is correct |
8 |
Correct |
4 ms |
5248 KB |
Output is correct |
9 |
Correct |
4 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
5 ms |
5248 KB |
Output is correct |
12 |
Correct |
5 ms |
5248 KB |
Output is correct |
13 |
Correct |
4 ms |
5120 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
4 ms |
5248 KB |
Output is correct |
16 |
Correct |
4 ms |
5248 KB |
Output is correct |
17 |
Correct |
4 ms |
5248 KB |
Output is correct |
18 |
Correct |
4 ms |
5248 KB |
Output is correct |
19 |
Correct |
4 ms |
5248 KB |
Output is correct |
20 |
Correct |
5 ms |
5248 KB |
Output is correct |
21 |
Correct |
5 ms |
5248 KB |
Output is correct |
22 |
Correct |
5 ms |
5124 KB |
Output is correct |
23 |
Correct |
4 ms |
5120 KB |
Output is correct |
24 |
Correct |
5 ms |
5248 KB |
Output is correct |
25 |
Correct |
5 ms |
5248 KB |
Output is correct |
26 |
Correct |
6 ms |
5248 KB |
Output is correct |
27 |
Correct |
4 ms |
5248 KB |
Output is correct |
28 |
Correct |
5 ms |
5248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
5120 KB |
Output is correct |
6 |
Correct |
5 ms |
5248 KB |
Output is correct |
7 |
Correct |
4 ms |
5248 KB |
Output is correct |
8 |
Correct |
4 ms |
5248 KB |
Output is correct |
9 |
Correct |
4 ms |
5120 KB |
Output is correct |
10 |
Correct |
179 ms |
21984 KB |
Output is correct |
11 |
Correct |
225 ms |
25452 KB |
Output is correct |
12 |
Correct |
224 ms |
25068 KB |
Output is correct |
13 |
Correct |
239 ms |
26348 KB |
Output is correct |
14 |
Correct |
137 ms |
18924 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
5 ms |
5248 KB |
Output is correct |
17 |
Correct |
5 ms |
5248 KB |
Output is correct |
18 |
Correct |
4 ms |
5120 KB |
Output is correct |
19 |
Correct |
4 ms |
5120 KB |
Output is correct |
20 |
Correct |
4 ms |
5248 KB |
Output is correct |
21 |
Correct |
4 ms |
5248 KB |
Output is correct |
22 |
Correct |
4 ms |
5248 KB |
Output is correct |
23 |
Correct |
4 ms |
5248 KB |
Output is correct |
24 |
Correct |
26 ms |
7864 KB |
Output is correct |
25 |
Correct |
239 ms |
28140 KB |
Output is correct |
26 |
Correct |
242 ms |
28152 KB |
Output is correct |
27 |
Correct |
241 ms |
27756 KB |
Output is correct |
28 |
Correct |
220 ms |
26732 KB |
Output is correct |
29 |
Correct |
218 ms |
26476 KB |
Output is correct |
30 |
Correct |
188 ms |
24428 KB |
Output is correct |
31 |
Correct |
239 ms |
28776 KB |
Output is correct |
32 |
Correct |
252 ms |
28524 KB |
Output is correct |
33 |
Correct |
131 ms |
20460 KB |
Output is correct |
34 |
Correct |
215 ms |
26476 KB |
Output is correct |
35 |
Correct |
4 ms |
5248 KB |
Output is correct |
36 |
Correct |
5 ms |
5248 KB |
Output is correct |
37 |
Correct |
5 ms |
5248 KB |
Output is correct |
38 |
Correct |
5 ms |
5124 KB |
Output is correct |
39 |
Correct |
4 ms |
5120 KB |
Output is correct |
40 |
Correct |
5 ms |
5248 KB |
Output is correct |
41 |
Correct |
5 ms |
5248 KB |
Output is correct |
42 |
Correct |
6 ms |
5248 KB |
Output is correct |
43 |
Correct |
4 ms |
5248 KB |
Output is correct |
44 |
Correct |
5 ms |
5248 KB |
Output is correct |
45 |
Correct |
172 ms |
22644 KB |
Output is correct |
46 |
Correct |
232 ms |
28392 KB |
Output is correct |
47 |
Correct |
207 ms |
25068 KB |
Output is correct |
48 |
Correct |
175 ms |
22380 KB |
Output is correct |
49 |
Correct |
91 ms |
14848 KB |
Output is correct |
50 |
Correct |
75 ms |
12524 KB |
Output is correct |
51 |
Correct |
138 ms |
19692 KB |
Output is correct |
52 |
Correct |
268 ms |
31260 KB |
Output is correct |
53 |
Correct |
237 ms |
28004 KB |
Output is correct |
54 |
Correct |
299 ms |
34024 KB |
Output is correct |
55 |
Correct |
138 ms |
20972 KB |
Output is correct |
56 |
Correct |
214 ms |
26724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
5120 KB |
Output is correct |
5 |
Correct |
5 ms |
5248 KB |
Output is correct |
6 |
Correct |
4 ms |
5248 KB |
Output is correct |
7 |
Correct |
4 ms |
5248 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
179 ms |
21984 KB |
Output is correct |
10 |
Correct |
225 ms |
25452 KB |
Output is correct |
11 |
Correct |
224 ms |
25068 KB |
Output is correct |
12 |
Correct |
239 ms |
26348 KB |
Output is correct |
13 |
Correct |
137 ms |
18924 KB |
Output is correct |
14 |
Correct |
188 ms |
21996 KB |
Output is correct |
15 |
Correct |
339 ms |
27640 KB |
Output is correct |
16 |
Correct |
330 ms |
27164 KB |
Output is correct |
17 |
Correct |
351 ms |
28404 KB |
Output is correct |
18 |
Correct |
235 ms |
20820 KB |
Output is correct |
19 |
Correct |
288 ms |
19724 KB |
Output is correct |
20 |
Correct |
292 ms |
20052 KB |
Output is correct |
21 |
Correct |
293 ms |
20096 KB |
Output is correct |
22 |
Correct |
285 ms |
20168 KB |
Output is correct |
23 |
Correct |
291 ms |
20204 KB |
Output is correct |
24 |
Correct |
282 ms |
19612 KB |
Output is correct |
25 |
Correct |
287 ms |
19944 KB |
Output is correct |
26 |
Correct |
286 ms |
19624 KB |
Output is correct |
27 |
Correct |
4 ms |
5120 KB |
Output is correct |
28 |
Correct |
5 ms |
5248 KB |
Output is correct |
29 |
Correct |
5 ms |
5248 KB |
Output is correct |
30 |
Correct |
4 ms |
5120 KB |
Output is correct |
31 |
Correct |
4 ms |
5120 KB |
Output is correct |
32 |
Correct |
4 ms |
5248 KB |
Output is correct |
33 |
Correct |
4 ms |
5248 KB |
Output is correct |
34 |
Correct |
4 ms |
5248 KB |
Output is correct |
35 |
Correct |
4 ms |
5248 KB |
Output is correct |
36 |
Correct |
26 ms |
7864 KB |
Output is correct |
37 |
Correct |
239 ms |
28140 KB |
Output is correct |
38 |
Correct |
242 ms |
28152 KB |
Output is correct |
39 |
Correct |
241 ms |
27756 KB |
Output is correct |
40 |
Correct |
220 ms |
26732 KB |
Output is correct |
41 |
Correct |
218 ms |
26476 KB |
Output is correct |
42 |
Correct |
188 ms |
24428 KB |
Output is correct |
43 |
Correct |
239 ms |
28776 KB |
Output is correct |
44 |
Correct |
252 ms |
28524 KB |
Output is correct |
45 |
Correct |
131 ms |
20460 KB |
Output is correct |
46 |
Correct |
215 ms |
26476 KB |
Output is correct |
47 |
Correct |
41 ms |
8248 KB |
Output is correct |
48 |
Correct |
470 ms |
33268 KB |
Output is correct |
49 |
Correct |
446 ms |
32864 KB |
Output is correct |
50 |
Correct |
454 ms |
32724 KB |
Output is correct |
51 |
Correct |
451 ms |
32096 KB |
Output is correct |
52 |
Correct |
416 ms |
30332 KB |
Output is correct |
53 |
Correct |
354 ms |
25056 KB |
Output is correct |
54 |
Correct |
475 ms |
33676 KB |
Output is correct |
55 |
Correct |
457 ms |
33748 KB |
Output is correct |
56 |
Correct |
310 ms |
25940 KB |
Output is correct |
57 |
Correct |
453 ms |
31572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
5120 KB |
Output is correct |
6 |
Correct |
5 ms |
5248 KB |
Output is correct |
7 |
Correct |
4 ms |
5248 KB |
Output is correct |
8 |
Correct |
4 ms |
5248 KB |
Output is correct |
9 |
Correct |
4 ms |
5120 KB |
Output is correct |
10 |
Correct |
179 ms |
21984 KB |
Output is correct |
11 |
Correct |
225 ms |
25452 KB |
Output is correct |
12 |
Correct |
224 ms |
25068 KB |
Output is correct |
13 |
Correct |
239 ms |
26348 KB |
Output is correct |
14 |
Correct |
137 ms |
18924 KB |
Output is correct |
15 |
Correct |
188 ms |
21996 KB |
Output is correct |
16 |
Correct |
339 ms |
27640 KB |
Output is correct |
17 |
Correct |
330 ms |
27164 KB |
Output is correct |
18 |
Correct |
351 ms |
28404 KB |
Output is correct |
19 |
Correct |
235 ms |
20820 KB |
Output is correct |
20 |
Correct |
288 ms |
19724 KB |
Output is correct |
21 |
Correct |
292 ms |
20052 KB |
Output is correct |
22 |
Correct |
293 ms |
20096 KB |
Output is correct |
23 |
Correct |
285 ms |
20168 KB |
Output is correct |
24 |
Correct |
291 ms |
20204 KB |
Output is correct |
25 |
Correct |
282 ms |
19612 KB |
Output is correct |
26 |
Correct |
287 ms |
19944 KB |
Output is correct |
27 |
Correct |
286 ms |
19624 KB |
Output is correct |
28 |
Correct |
4 ms |
5120 KB |
Output is correct |
29 |
Correct |
5 ms |
5248 KB |
Output is correct |
30 |
Correct |
5 ms |
5248 KB |
Output is correct |
31 |
Correct |
4 ms |
5120 KB |
Output is correct |
32 |
Correct |
4 ms |
5120 KB |
Output is correct |
33 |
Correct |
4 ms |
5248 KB |
Output is correct |
34 |
Correct |
4 ms |
5248 KB |
Output is correct |
35 |
Correct |
4 ms |
5248 KB |
Output is correct |
36 |
Correct |
4 ms |
5248 KB |
Output is correct |
37 |
Correct |
26 ms |
7864 KB |
Output is correct |
38 |
Correct |
239 ms |
28140 KB |
Output is correct |
39 |
Correct |
242 ms |
28152 KB |
Output is correct |
40 |
Correct |
241 ms |
27756 KB |
Output is correct |
41 |
Correct |
220 ms |
26732 KB |
Output is correct |
42 |
Correct |
218 ms |
26476 KB |
Output is correct |
43 |
Correct |
188 ms |
24428 KB |
Output is correct |
44 |
Correct |
239 ms |
28776 KB |
Output is correct |
45 |
Correct |
252 ms |
28524 KB |
Output is correct |
46 |
Correct |
131 ms |
20460 KB |
Output is correct |
47 |
Correct |
215 ms |
26476 KB |
Output is correct |
48 |
Correct |
41 ms |
8248 KB |
Output is correct |
49 |
Correct |
470 ms |
33268 KB |
Output is correct |
50 |
Correct |
446 ms |
32864 KB |
Output is correct |
51 |
Correct |
454 ms |
32724 KB |
Output is correct |
52 |
Correct |
451 ms |
32096 KB |
Output is correct |
53 |
Correct |
416 ms |
30332 KB |
Output is correct |
54 |
Correct |
354 ms |
25056 KB |
Output is correct |
55 |
Correct |
475 ms |
33676 KB |
Output is correct |
56 |
Correct |
457 ms |
33748 KB |
Output is correct |
57 |
Correct |
310 ms |
25940 KB |
Output is correct |
58 |
Correct |
453 ms |
31572 KB |
Output is correct |
59 |
Correct |
194 ms |
11704 KB |
Output is correct |
60 |
Correct |
475 ms |
28904 KB |
Output is correct |
61 |
Correct |
478 ms |
28456 KB |
Output is correct |
62 |
Correct |
499 ms |
29780 KB |
Output is correct |
63 |
Correct |
338 ms |
22228 KB |
Output is correct |
64 |
Correct |
4 ms |
5248 KB |
Output is correct |
65 |
Correct |
5 ms |
5248 KB |
Output is correct |
66 |
Correct |
5 ms |
5248 KB |
Output is correct |
67 |
Correct |
5 ms |
5124 KB |
Output is correct |
68 |
Correct |
4 ms |
5120 KB |
Output is correct |
69 |
Correct |
5 ms |
5248 KB |
Output is correct |
70 |
Correct |
5 ms |
5248 KB |
Output is correct |
71 |
Correct |
6 ms |
5248 KB |
Output is correct |
72 |
Correct |
4 ms |
5248 KB |
Output is correct |
73 |
Correct |
5 ms |
5248 KB |
Output is correct |
74 |
Correct |
172 ms |
22644 KB |
Output is correct |
75 |
Correct |
232 ms |
28392 KB |
Output is correct |
76 |
Correct |
207 ms |
25068 KB |
Output is correct |
77 |
Correct |
175 ms |
22380 KB |
Output is correct |
78 |
Correct |
91 ms |
14848 KB |
Output is correct |
79 |
Correct |
75 ms |
12524 KB |
Output is correct |
80 |
Correct |
138 ms |
19692 KB |
Output is correct |
81 |
Correct |
268 ms |
31260 KB |
Output is correct |
82 |
Correct |
237 ms |
28004 KB |
Output is correct |
83 |
Correct |
299 ms |
34024 KB |
Output is correct |
84 |
Correct |
138 ms |
20972 KB |
Output is correct |
85 |
Correct |
214 ms |
26724 KB |
Output is correct |
86 |
Correct |
103 ms |
12760 KB |
Output is correct |
87 |
Correct |
445 ms |
32500 KB |
Output is correct |
88 |
Correct |
465 ms |
32852 KB |
Output is correct |
89 |
Correct |
369 ms |
26280 KB |
Output is correct |
90 |
Correct |
253 ms |
18028 KB |
Output is correct |
91 |
Correct |
268 ms |
18444 KB |
Output is correct |
92 |
Correct |
346 ms |
24116 KB |
Output is correct |
93 |
Correct |
482 ms |
33952 KB |
Output is correct |
94 |
Correct |
461 ms |
31048 KB |
Output is correct |
95 |
Correct |
527 ms |
36456 KB |
Output is correct |
96 |
Correct |
313 ms |
26196 KB |
Output is correct |
97 |
Correct |
419 ms |
29092 KB |
Output is correct |