#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<climits>
#include"swap.h"
#define ff first
#define ss second
using namespace std;
int n, m;
vector<pair<int, pair<int, int>>> edges;
vector<vector<int>> binlift;
vector<int> depths;
vector<int> swapp;
int getp(int x, vector<int>& dsu) {
if (dsu[x] == x)return x;
return dsu[x] = getp(dsu[x], dsu);
}
void merge(int a, int b, int x, vector<int>& dsu) {
a = getp(a, dsu);
b = getp(b, dsu);
dsu[a] = x;
dsu[b] = x;
}
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
n = N;
m = M;
for (int i = 0; i < m; i++) {
edges.push_back({ W[i], {U[i], V[i]} });
}
sort(edges.begin(), edges.end());
vector<int> dsu(n + m);
for (int i = 0; i < n + m; i++)dsu[i] = i;
swapp.assign(n + m, INT_MAX);
vector<vector<int>>child(n + m);
vector<int>deg(n, 0);
binlift.assign(n + m, vector<int>(20, -1));
for (int i = 0; i < m; i++) {
int a = edges[i].ss.ff;
int b = edges[i].ss.ss;
int oa = a;
int ob = b;
a = getp(a, dsu);
b = getp(b, dsu);
if (a != b) {
merge(a, b, i + n, dsu);
binlift[a][0] = i + n;
binlift[b][0] = i + n;
if (swapp[a] != INT_MAX || swapp[b] != INT_MAX || (deg[oa] >= 2 || deg[ob] >= 2)) {
swapp[i + n] = edges[i].ff;
}
child[i + n].push_back(a);
child[i + n].push_back(b);
}
else {
swapp[a] = min(swapp[a], edges[i].ff);
}
deg[oa]++;
deg[ob]++;
}
depths.assign(n + m, 0);
for (int i = n + m - 1; i >= 0; i--) {
for (int j = 0; j < child[i].size(); j++) {
depths[child[i][j]] = depths[i] + 1;
swapp[child[i][j]] = min(swapp[child[i][j]], swapp[i]);
}
}
for (int i = n + m - 1; i >= 0; i--) {
int j = 0;
while (binlift[i][j] != -1) {
binlift[i][j + 1] = binlift[binlift[i][j]][j];
j++;
}
}
}
int getMinimumFuelCapacity(int X, int Y) {
if (depths[X] < depths[Y]) {
int tmp = X;
X = Y;
Y = tmp;
}
while (depths[X] != depths[Y]) {
X = binlift[X][floor(log2(depths[X] - depths[Y]))];
}
while (X != Y) {
int i = 19;
while (binlift[X][i] == binlift[Y][i] && i > 0)i--;
X = binlift[X][i];
Y = binlift[Y][i];
}
return swapp[X] == INT_MAX ? -1 : swapp[X];
}
/*signed main() {
int N, M;
cin >> N >> M;
vector<int> U(M), V(M), W(M);
for (int i = 0; i < M; i++)cin >> U[i] >> V[i] >> W[i];
init(N, M, U, V, W);
int Q;
cin >> Q;
while (Q--) {
int a, b;
cin >> a >> b;
cout << getMinimumFuelCapacity(a, b) << "\n";
}
}*/
Compilation message
swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:64:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for (int j = 0; j < child[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
99 ms |
31448 KB |
Output is correct |
10 |
Correct |
114 ms |
38304 KB |
Output is correct |
11 |
Correct |
112 ms |
37740 KB |
Output is correct |
12 |
Correct |
117 ms |
39964 KB |
Output is correct |
13 |
Correct |
159 ms |
40648 KB |
Output is correct |
14 |
Correct |
103 ms |
31652 KB |
Output is correct |
15 |
Correct |
279 ms |
42536 KB |
Output is correct |
16 |
Correct |
290 ms |
41408 KB |
Output is correct |
17 |
Correct |
252 ms |
43840 KB |
Output is correct |
18 |
Correct |
497 ms |
44444 KB |
Output is correct |
19 |
Correct |
111 ms |
10892 KB |
Output is correct |
20 |
Correct |
256 ms |
42548 KB |
Output is correct |
21 |
Correct |
246 ms |
41248 KB |
Output is correct |
22 |
Correct |
267 ms |
43920 KB |
Output is correct |
23 |
Correct |
597 ms |
44632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
420 ms |
41188 KB |
Output is correct |
4 |
Correct |
492 ms |
43364 KB |
Output is correct |
5 |
Correct |
465 ms |
41844 KB |
Output is correct |
6 |
Correct |
463 ms |
42844 KB |
Output is correct |
7 |
Correct |
461 ms |
42508 KB |
Output is correct |
8 |
Correct |
423 ms |
40788 KB |
Output is correct |
9 |
Correct |
441 ms |
42244 KB |
Output is correct |
10 |
Correct |
428 ms |
40508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
564 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
564 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
2 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
732 KB |
Output is correct |
25 |
Correct |
1 ms |
852 KB |
Output is correct |
26 |
Correct |
2 ms |
852 KB |
Output is correct |
27 |
Correct |
1 ms |
568 KB |
Output is correct |
28 |
Correct |
2 ms |
820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
99 ms |
31448 KB |
Output is correct |
11 |
Correct |
114 ms |
38304 KB |
Output is correct |
12 |
Correct |
112 ms |
37740 KB |
Output is correct |
13 |
Correct |
117 ms |
39964 KB |
Output is correct |
14 |
Correct |
159 ms |
40648 KB |
Output is correct |
15 |
Correct |
1 ms |
564 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
564 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
2 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
732 KB |
Output is correct |
30 |
Correct |
1 ms |
852 KB |
Output is correct |
31 |
Correct |
2 ms |
852 KB |
Output is correct |
32 |
Correct |
1 ms |
568 KB |
Output is correct |
33 |
Correct |
2 ms |
820 KB |
Output is correct |
34 |
Correct |
13 ms |
5652 KB |
Output is correct |
35 |
Correct |
123 ms |
39596 KB |
Output is correct |
36 |
Correct |
117 ms |
39708 KB |
Output is correct |
37 |
Correct |
113 ms |
39668 KB |
Output is correct |
38 |
Correct |
125 ms |
39100 KB |
Output is correct |
39 |
Correct |
121 ms |
38932 KB |
Output is correct |
40 |
Correct |
123 ms |
35668 KB |
Output is correct |
41 |
Correct |
110 ms |
39968 KB |
Output is correct |
42 |
Correct |
118 ms |
39720 KB |
Output is correct |
43 |
Correct |
150 ms |
39616 KB |
Output is correct |
44 |
Correct |
137 ms |
40004 KB |
Output is correct |
45 |
Correct |
169 ms |
45680 KB |
Output is correct |
46 |
Correct |
112 ms |
39804 KB |
Output is correct |
47 |
Correct |
135 ms |
39636 KB |
Output is correct |
48 |
Correct |
205 ms |
41468 KB |
Output is correct |
49 |
Correct |
76 ms |
32884 KB |
Output is correct |
50 |
Correct |
63 ms |
26576 KB |
Output is correct |
51 |
Correct |
134 ms |
38976 KB |
Output is correct |
52 |
Correct |
152 ms |
52736 KB |
Output is correct |
53 |
Correct |
215 ms |
56360 KB |
Output is correct |
54 |
Correct |
168 ms |
60604 KB |
Output is correct |
55 |
Correct |
162 ms |
39640 KB |
Output is correct |
56 |
Correct |
223 ms |
54892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
99 ms |
31448 KB |
Output is correct |
10 |
Correct |
114 ms |
38304 KB |
Output is correct |
11 |
Correct |
112 ms |
37740 KB |
Output is correct |
12 |
Correct |
117 ms |
39964 KB |
Output is correct |
13 |
Correct |
159 ms |
40648 KB |
Output is correct |
14 |
Correct |
103 ms |
31652 KB |
Output is correct |
15 |
Correct |
279 ms |
42536 KB |
Output is correct |
16 |
Correct |
290 ms |
41408 KB |
Output is correct |
17 |
Correct |
252 ms |
43840 KB |
Output is correct |
18 |
Correct |
497 ms |
44444 KB |
Output is correct |
19 |
Correct |
420 ms |
41188 KB |
Output is correct |
20 |
Correct |
492 ms |
43364 KB |
Output is correct |
21 |
Correct |
465 ms |
41844 KB |
Output is correct |
22 |
Correct |
463 ms |
42844 KB |
Output is correct |
23 |
Correct |
461 ms |
42508 KB |
Output is correct |
24 |
Correct |
423 ms |
40788 KB |
Output is correct |
25 |
Correct |
441 ms |
42244 KB |
Output is correct |
26 |
Correct |
428 ms |
40508 KB |
Output is correct |
27 |
Correct |
1 ms |
564 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
1 ms |
596 KB |
Output is correct |
35 |
Correct |
1 ms |
564 KB |
Output is correct |
36 |
Correct |
13 ms |
5652 KB |
Output is correct |
37 |
Correct |
123 ms |
39596 KB |
Output is correct |
38 |
Correct |
117 ms |
39708 KB |
Output is correct |
39 |
Correct |
113 ms |
39668 KB |
Output is correct |
40 |
Correct |
125 ms |
39100 KB |
Output is correct |
41 |
Correct |
121 ms |
38932 KB |
Output is correct |
42 |
Correct |
123 ms |
35668 KB |
Output is correct |
43 |
Correct |
110 ms |
39968 KB |
Output is correct |
44 |
Correct |
118 ms |
39720 KB |
Output is correct |
45 |
Correct |
150 ms |
39616 KB |
Output is correct |
46 |
Correct |
137 ms |
40004 KB |
Output is correct |
47 |
Correct |
21 ms |
5524 KB |
Output is correct |
48 |
Correct |
278 ms |
43536 KB |
Output is correct |
49 |
Correct |
269 ms |
43644 KB |
Output is correct |
50 |
Correct |
266 ms |
43448 KB |
Output is correct |
51 |
Correct |
254 ms |
43276 KB |
Output is correct |
52 |
Correct |
251 ms |
40924 KB |
Output is correct |
53 |
Correct |
238 ms |
30368 KB |
Output is correct |
54 |
Correct |
272 ms |
43860 KB |
Output is correct |
55 |
Correct |
265 ms |
43668 KB |
Output is correct |
56 |
Correct |
465 ms |
43576 KB |
Output is correct |
57 |
Correct |
308 ms |
43804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
99 ms |
31448 KB |
Output is correct |
11 |
Correct |
114 ms |
38304 KB |
Output is correct |
12 |
Correct |
112 ms |
37740 KB |
Output is correct |
13 |
Correct |
117 ms |
39964 KB |
Output is correct |
14 |
Correct |
159 ms |
40648 KB |
Output is correct |
15 |
Correct |
103 ms |
31652 KB |
Output is correct |
16 |
Correct |
279 ms |
42536 KB |
Output is correct |
17 |
Correct |
290 ms |
41408 KB |
Output is correct |
18 |
Correct |
252 ms |
43840 KB |
Output is correct |
19 |
Correct |
497 ms |
44444 KB |
Output is correct |
20 |
Correct |
420 ms |
41188 KB |
Output is correct |
21 |
Correct |
492 ms |
43364 KB |
Output is correct |
22 |
Correct |
465 ms |
41844 KB |
Output is correct |
23 |
Correct |
463 ms |
42844 KB |
Output is correct |
24 |
Correct |
461 ms |
42508 KB |
Output is correct |
25 |
Correct |
423 ms |
40788 KB |
Output is correct |
26 |
Correct |
441 ms |
42244 KB |
Output is correct |
27 |
Correct |
428 ms |
40508 KB |
Output is correct |
28 |
Correct |
1 ms |
564 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
1 ms |
596 KB |
Output is correct |
35 |
Correct |
1 ms |
596 KB |
Output is correct |
36 |
Correct |
1 ms |
564 KB |
Output is correct |
37 |
Correct |
13 ms |
5652 KB |
Output is correct |
38 |
Correct |
123 ms |
39596 KB |
Output is correct |
39 |
Correct |
117 ms |
39708 KB |
Output is correct |
40 |
Correct |
113 ms |
39668 KB |
Output is correct |
41 |
Correct |
125 ms |
39100 KB |
Output is correct |
42 |
Correct |
121 ms |
38932 KB |
Output is correct |
43 |
Correct |
123 ms |
35668 KB |
Output is correct |
44 |
Correct |
110 ms |
39968 KB |
Output is correct |
45 |
Correct |
118 ms |
39720 KB |
Output is correct |
46 |
Correct |
150 ms |
39616 KB |
Output is correct |
47 |
Correct |
137 ms |
40004 KB |
Output is correct |
48 |
Correct |
21 ms |
5524 KB |
Output is correct |
49 |
Correct |
278 ms |
43536 KB |
Output is correct |
50 |
Correct |
269 ms |
43644 KB |
Output is correct |
51 |
Correct |
266 ms |
43448 KB |
Output is correct |
52 |
Correct |
254 ms |
43276 KB |
Output is correct |
53 |
Correct |
251 ms |
40924 KB |
Output is correct |
54 |
Correct |
238 ms |
30368 KB |
Output is correct |
55 |
Correct |
272 ms |
43860 KB |
Output is correct |
56 |
Correct |
265 ms |
43668 KB |
Output is correct |
57 |
Correct |
465 ms |
43576 KB |
Output is correct |
58 |
Correct |
308 ms |
43804 KB |
Output is correct |
59 |
Correct |
111 ms |
10892 KB |
Output is correct |
60 |
Correct |
256 ms |
42548 KB |
Output is correct |
61 |
Correct |
246 ms |
41248 KB |
Output is correct |
62 |
Correct |
267 ms |
43920 KB |
Output is correct |
63 |
Correct |
597 ms |
44632 KB |
Output is correct |
64 |
Correct |
1 ms |
596 KB |
Output is correct |
65 |
Correct |
1 ms |
596 KB |
Output is correct |
66 |
Correct |
1 ms |
596 KB |
Output is correct |
67 |
Correct |
2 ms |
596 KB |
Output is correct |
68 |
Correct |
1 ms |
596 KB |
Output is correct |
69 |
Correct |
1 ms |
732 KB |
Output is correct |
70 |
Correct |
1 ms |
852 KB |
Output is correct |
71 |
Correct |
2 ms |
852 KB |
Output is correct |
72 |
Correct |
1 ms |
568 KB |
Output is correct |
73 |
Correct |
2 ms |
820 KB |
Output is correct |
74 |
Correct |
169 ms |
45680 KB |
Output is correct |
75 |
Correct |
112 ms |
39804 KB |
Output is correct |
76 |
Correct |
135 ms |
39636 KB |
Output is correct |
77 |
Correct |
205 ms |
41468 KB |
Output is correct |
78 |
Correct |
76 ms |
32884 KB |
Output is correct |
79 |
Correct |
63 ms |
26576 KB |
Output is correct |
80 |
Correct |
134 ms |
38976 KB |
Output is correct |
81 |
Correct |
152 ms |
52736 KB |
Output is correct |
82 |
Correct |
215 ms |
56360 KB |
Output is correct |
83 |
Correct |
168 ms |
60604 KB |
Output is correct |
84 |
Correct |
162 ms |
39640 KB |
Output is correct |
85 |
Correct |
223 ms |
54892 KB |
Output is correct |
86 |
Correct |
93 ms |
17496 KB |
Output is correct |
87 |
Correct |
250 ms |
43492 KB |
Output is correct |
88 |
Correct |
260 ms |
43820 KB |
Output is correct |
89 |
Correct |
441 ms |
43260 KB |
Output is correct |
90 |
Correct |
179 ms |
38312 KB |
Output is correct |
91 |
Correct |
206 ms |
36796 KB |
Output is correct |
92 |
Correct |
392 ms |
41664 KB |
Output is correct |
93 |
Correct |
335 ms |
56144 KB |
Output is correct |
94 |
Correct |
492 ms |
59956 KB |
Output is correct |
95 |
Correct |
309 ms |
64496 KB |
Output is correct |
96 |
Correct |
464 ms |
43588 KB |
Output is correct |
97 |
Correct |
468 ms |
51304 KB |
Output is correct |