#include "swap.h"
#include <bits/stdc++.h>
#define PB push_back
using namespace std;
const int N = 300100;
const int M = 200100;
const int PW = 20;
vector<int> g[N];
int n, m, U[M], V[M], W[M], pre[N], lst, indx[N], deg[N], cost[N];
int ht[N], up[N][PW], tin[N], tout[N], tt = 0, id[N];
bool mrk[N], good[N];
bool cmp(int _x, int _y){
return (W[_x] < W[_y]);
}
int get(int x) { return (pre[x] == x ? x : pre[x] = get(pre[x])); }
void dfs(int v){
tin[v] = tt++;
for (int po = 1; po < PW; po++)
up[v][po] = up[up[v][po - 1]][po - 1];
for (int u : g[v]){
ht[u] = ht[v] + 1;
up[u][0] = v;
dfs(u);
}
tout[v] = tt++;
}
void init(int NN, int MM, std::vector<int> UU, std::vector<int> VV, std::vector<int> WW) {
n = NN; m = MM;
for (int i = 0; i < m; i++) {
U[i] = UU[i];
V[i] = VV[i];
W[i] = WW[i];
id[i] = i;
}
for (int i = 0; i < n; i++) {
pre[i] = i;
indx[i] = i;
good[i] = 0;
deg[i] = 0;
}
sort(id, id + m, cmp);
lst = n;
for (int it = 0; it < m; it++){
int i = id[it];
int x = get(U[i]);
int y = get(V[i]);
if (x == y){
if (!good[indx[x]]){
g[lst].PB(indx[x]);
good[lst] = 1;
indx[x] = lst;
cost[lst] = W[i];
lst++;
}
} else {
deg[U[i]]++;
deg[V[i]]++;
bool kok = (good[indx[x]] || good[indx[y]] || (deg[U[i]] > 2) || (deg[V[i]] > 2));
g[lst].PB(indx[x]);
g[lst].PB(indx[y]);
cost[lst] = W[i];
good[lst] = kok;
indx[x] = lst;
pre[y] = x;
lst++;
}
}
up[lst - 1][0] = lst - 1;
dfs(lst - 1);
}
bool upper(int a, int b){
return (tin[a] <= tin[b] && tout[b] <= tout[a]);
}
int lca(int a, int b){
if (upper(a, b)) return a;
if (upper(b, a)) return b;
for (int po = PW - 1; po >= 0; po--)
if (!upper(up[a][po], b))
a = up[a][po];
return up[a][0];
}
int getMinimumFuelCapacity(int X, int Y) {
if (!good[lst - 1]) return -1;
int lc = lca(X, Y);
if (good[lc])
return cost[lc];
else {
for (int po = PW - 1; po >= 0; po--){
if (ht[lc] < (1 << po)) continue;
if (!good[up[lc][po]])
lc = up[lc][po];
}
return cost[up[lc][0]];
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7404 KB |
Output is correct |
2 |
Correct |
5 ms |
7404 KB |
Output is correct |
3 |
Correct |
5 ms |
7404 KB |
Output is correct |
4 |
Correct |
5 ms |
7532 KB |
Output is correct |
5 |
Correct |
6 ms |
7788 KB |
Output is correct |
6 |
Correct |
6 ms |
7804 KB |
Output is correct |
7 |
Correct |
6 ms |
7660 KB |
Output is correct |
8 |
Correct |
6 ms |
7788 KB |
Output is correct |
9 |
Correct |
121 ms |
30308 KB |
Output is correct |
10 |
Correct |
147 ms |
35172 KB |
Output is correct |
11 |
Correct |
156 ms |
34920 KB |
Output is correct |
12 |
Correct |
158 ms |
36324 KB |
Output is correct |
13 |
Correct |
144 ms |
39648 KB |
Output is correct |
14 |
Correct |
131 ms |
30612 KB |
Output is correct |
15 |
Correct |
217 ms |
39684 KB |
Output is correct |
16 |
Correct |
210 ms |
38804 KB |
Output is correct |
17 |
Correct |
222 ms |
40396 KB |
Output is correct |
18 |
Correct |
210 ms |
43740 KB |
Output is correct |
19 |
Correct |
113 ms |
17164 KB |
Output is correct |
20 |
Correct |
269 ms |
40932 KB |
Output is correct |
21 |
Correct |
281 ms |
39964 KB |
Output is correct |
22 |
Correct |
290 ms |
42056 KB |
Output is correct |
23 |
Correct |
410 ms |
45004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7404 KB |
Output is correct |
2 |
Correct |
5 ms |
7404 KB |
Output is correct |
3 |
Correct |
315 ms |
45704 KB |
Output is correct |
4 |
Correct |
322 ms |
47180 KB |
Output is correct |
5 |
Correct |
330 ms |
46460 KB |
Output is correct |
6 |
Correct |
317 ms |
46924 KB |
Output is correct |
7 |
Correct |
337 ms |
46848 KB |
Output is correct |
8 |
Correct |
324 ms |
45332 KB |
Output is correct |
9 |
Correct |
330 ms |
46428 KB |
Output is correct |
10 |
Correct |
320 ms |
45128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7404 KB |
Output is correct |
2 |
Correct |
5 ms |
7404 KB |
Output is correct |
3 |
Correct |
5 ms |
7404 KB |
Output is correct |
4 |
Correct |
5 ms |
7404 KB |
Output is correct |
5 |
Correct |
5 ms |
7532 KB |
Output is correct |
6 |
Correct |
6 ms |
7788 KB |
Output is correct |
7 |
Correct |
6 ms |
7804 KB |
Output is correct |
8 |
Correct |
6 ms |
7660 KB |
Output is correct |
9 |
Correct |
6 ms |
7788 KB |
Output is correct |
10 |
Correct |
6 ms |
7660 KB |
Output is correct |
11 |
Correct |
6 ms |
7660 KB |
Output is correct |
12 |
Correct |
6 ms |
7660 KB |
Output is correct |
13 |
Correct |
6 ms |
7660 KB |
Output is correct |
14 |
Correct |
6 ms |
7660 KB |
Output is correct |
15 |
Correct |
6 ms |
7788 KB |
Output is correct |
16 |
Correct |
6 ms |
7660 KB |
Output is correct |
17 |
Correct |
6 ms |
7788 KB |
Output is correct |
18 |
Correct |
7 ms |
7660 KB |
Output is correct |
19 |
Correct |
7 ms |
7660 KB |
Output is correct |
20 |
Correct |
6 ms |
7808 KB |
Output is correct |
21 |
Correct |
6 ms |
7660 KB |
Output is correct |
22 |
Correct |
6 ms |
7532 KB |
Output is correct |
23 |
Correct |
6 ms |
7660 KB |
Output is correct |
24 |
Correct |
7 ms |
7788 KB |
Output is correct |
25 |
Correct |
7 ms |
7788 KB |
Output is correct |
26 |
Correct |
6 ms |
7788 KB |
Output is correct |
27 |
Correct |
6 ms |
7788 KB |
Output is correct |
28 |
Correct |
6 ms |
7788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7404 KB |
Output is correct |
2 |
Correct |
5 ms |
7404 KB |
Output is correct |
3 |
Correct |
5 ms |
7404 KB |
Output is correct |
4 |
Correct |
5 ms |
7404 KB |
Output is correct |
5 |
Correct |
5 ms |
7532 KB |
Output is correct |
6 |
Correct |
6 ms |
7788 KB |
Output is correct |
7 |
Correct |
6 ms |
7804 KB |
Output is correct |
8 |
Correct |
6 ms |
7660 KB |
Output is correct |
9 |
Correct |
6 ms |
7788 KB |
Output is correct |
10 |
Correct |
121 ms |
30308 KB |
Output is correct |
11 |
Correct |
147 ms |
35172 KB |
Output is correct |
12 |
Correct |
156 ms |
34920 KB |
Output is correct |
13 |
Correct |
158 ms |
36324 KB |
Output is correct |
14 |
Correct |
144 ms |
39648 KB |
Output is correct |
15 |
Correct |
6 ms |
7660 KB |
Output is correct |
16 |
Correct |
6 ms |
7660 KB |
Output is correct |
17 |
Correct |
6 ms |
7660 KB |
Output is correct |
18 |
Correct |
6 ms |
7660 KB |
Output is correct |
19 |
Correct |
6 ms |
7660 KB |
Output is correct |
20 |
Correct |
6 ms |
7788 KB |
Output is correct |
21 |
Correct |
6 ms |
7660 KB |
Output is correct |
22 |
Correct |
6 ms |
7788 KB |
Output is correct |
23 |
Correct |
7 ms |
7660 KB |
Output is correct |
24 |
Correct |
20 ms |
11372 KB |
Output is correct |
25 |
Correct |
168 ms |
36068 KB |
Output is correct |
26 |
Correct |
151 ms |
36196 KB |
Output is correct |
27 |
Correct |
151 ms |
36200 KB |
Output is correct |
28 |
Correct |
149 ms |
35956 KB |
Output is correct |
29 |
Correct |
153 ms |
35684 KB |
Output is correct |
30 |
Correct |
138 ms |
33356 KB |
Output is correct |
31 |
Correct |
158 ms |
36324 KB |
Output is correct |
32 |
Correct |
150 ms |
36196 KB |
Output is correct |
33 |
Correct |
138 ms |
40268 KB |
Output is correct |
34 |
Correct |
161 ms |
36452 KB |
Output is correct |
35 |
Correct |
7 ms |
7660 KB |
Output is correct |
36 |
Correct |
6 ms |
7808 KB |
Output is correct |
37 |
Correct |
6 ms |
7660 KB |
Output is correct |
38 |
Correct |
6 ms |
7532 KB |
Output is correct |
39 |
Correct |
6 ms |
7660 KB |
Output is correct |
40 |
Correct |
7 ms |
7788 KB |
Output is correct |
41 |
Correct |
7 ms |
7788 KB |
Output is correct |
42 |
Correct |
6 ms |
7788 KB |
Output is correct |
43 |
Correct |
6 ms |
7788 KB |
Output is correct |
44 |
Correct |
6 ms |
7788 KB |
Output is correct |
45 |
Correct |
166 ms |
35972 KB |
Output is correct |
46 |
Correct |
149 ms |
36068 KB |
Output is correct |
47 |
Correct |
152 ms |
36072 KB |
Output is correct |
48 |
Correct |
157 ms |
37744 KB |
Output is correct |
49 |
Correct |
86 ms |
16612 KB |
Output is correct |
50 |
Correct |
75 ms |
16484 KB |
Output is correct |
51 |
Correct |
136 ms |
31204 KB |
Output is correct |
52 |
Correct |
193 ms |
40036 KB |
Output is correct |
53 |
Correct |
231 ms |
46052 KB |
Output is correct |
54 |
Correct |
212 ms |
42212 KB |
Output is correct |
55 |
Correct |
142 ms |
40032 KB |
Output is correct |
56 |
Correct |
204 ms |
42596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7404 KB |
Output is correct |
2 |
Correct |
5 ms |
7404 KB |
Output is correct |
3 |
Correct |
5 ms |
7404 KB |
Output is correct |
4 |
Correct |
5 ms |
7532 KB |
Output is correct |
5 |
Correct |
6 ms |
7788 KB |
Output is correct |
6 |
Correct |
6 ms |
7804 KB |
Output is correct |
7 |
Correct |
6 ms |
7660 KB |
Output is correct |
8 |
Correct |
6 ms |
7788 KB |
Output is correct |
9 |
Correct |
121 ms |
30308 KB |
Output is correct |
10 |
Correct |
147 ms |
35172 KB |
Output is correct |
11 |
Correct |
156 ms |
34920 KB |
Output is correct |
12 |
Correct |
158 ms |
36324 KB |
Output is correct |
13 |
Correct |
144 ms |
39648 KB |
Output is correct |
14 |
Correct |
131 ms |
30612 KB |
Output is correct |
15 |
Correct |
217 ms |
39684 KB |
Output is correct |
16 |
Correct |
210 ms |
38804 KB |
Output is correct |
17 |
Correct |
222 ms |
40396 KB |
Output is correct |
18 |
Correct |
210 ms |
43740 KB |
Output is correct |
19 |
Correct |
315 ms |
45704 KB |
Output is correct |
20 |
Correct |
322 ms |
47180 KB |
Output is correct |
21 |
Correct |
330 ms |
46460 KB |
Output is correct |
22 |
Correct |
317 ms |
46924 KB |
Output is correct |
23 |
Correct |
337 ms |
46848 KB |
Output is correct |
24 |
Correct |
324 ms |
45332 KB |
Output is correct |
25 |
Correct |
330 ms |
46428 KB |
Output is correct |
26 |
Correct |
320 ms |
45128 KB |
Output is correct |
27 |
Correct |
6 ms |
7660 KB |
Output is correct |
28 |
Correct |
6 ms |
7660 KB |
Output is correct |
29 |
Correct |
6 ms |
7660 KB |
Output is correct |
30 |
Correct |
6 ms |
7660 KB |
Output is correct |
31 |
Correct |
6 ms |
7660 KB |
Output is correct |
32 |
Correct |
6 ms |
7788 KB |
Output is correct |
33 |
Correct |
6 ms |
7660 KB |
Output is correct |
34 |
Correct |
6 ms |
7788 KB |
Output is correct |
35 |
Correct |
7 ms |
7660 KB |
Output is correct |
36 |
Correct |
20 ms |
11372 KB |
Output is correct |
37 |
Correct |
168 ms |
36068 KB |
Output is correct |
38 |
Correct |
151 ms |
36196 KB |
Output is correct |
39 |
Correct |
151 ms |
36200 KB |
Output is correct |
40 |
Correct |
149 ms |
35956 KB |
Output is correct |
41 |
Correct |
153 ms |
35684 KB |
Output is correct |
42 |
Correct |
138 ms |
33356 KB |
Output is correct |
43 |
Correct |
158 ms |
36324 KB |
Output is correct |
44 |
Correct |
150 ms |
36196 KB |
Output is correct |
45 |
Correct |
138 ms |
40268 KB |
Output is correct |
46 |
Correct |
161 ms |
36452 KB |
Output is correct |
47 |
Correct |
29 ms |
11500 KB |
Output is correct |
48 |
Correct |
271 ms |
41204 KB |
Output is correct |
49 |
Correct |
270 ms |
41160 KB |
Output is correct |
50 |
Correct |
269 ms |
40904 KB |
Output is correct |
51 |
Correct |
276 ms |
40940 KB |
Output is correct |
52 |
Correct |
264 ms |
39312 KB |
Output is correct |
53 |
Correct |
224 ms |
32468 KB |
Output is correct |
54 |
Correct |
274 ms |
41800 KB |
Output is correct |
55 |
Correct |
274 ms |
41036 KB |
Output is correct |
56 |
Correct |
337 ms |
44232 KB |
Output is correct |
57 |
Correct |
290 ms |
41936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7404 KB |
Output is correct |
2 |
Correct |
5 ms |
7404 KB |
Output is correct |
3 |
Correct |
5 ms |
7404 KB |
Output is correct |
4 |
Correct |
5 ms |
7404 KB |
Output is correct |
5 |
Correct |
5 ms |
7532 KB |
Output is correct |
6 |
Correct |
6 ms |
7788 KB |
Output is correct |
7 |
Correct |
6 ms |
7804 KB |
Output is correct |
8 |
Correct |
6 ms |
7660 KB |
Output is correct |
9 |
Correct |
6 ms |
7788 KB |
Output is correct |
10 |
Correct |
121 ms |
30308 KB |
Output is correct |
11 |
Correct |
147 ms |
35172 KB |
Output is correct |
12 |
Correct |
156 ms |
34920 KB |
Output is correct |
13 |
Correct |
158 ms |
36324 KB |
Output is correct |
14 |
Correct |
144 ms |
39648 KB |
Output is correct |
15 |
Correct |
131 ms |
30612 KB |
Output is correct |
16 |
Correct |
217 ms |
39684 KB |
Output is correct |
17 |
Correct |
210 ms |
38804 KB |
Output is correct |
18 |
Correct |
222 ms |
40396 KB |
Output is correct |
19 |
Correct |
210 ms |
43740 KB |
Output is correct |
20 |
Correct |
315 ms |
45704 KB |
Output is correct |
21 |
Correct |
322 ms |
47180 KB |
Output is correct |
22 |
Correct |
330 ms |
46460 KB |
Output is correct |
23 |
Correct |
317 ms |
46924 KB |
Output is correct |
24 |
Correct |
337 ms |
46848 KB |
Output is correct |
25 |
Correct |
324 ms |
45332 KB |
Output is correct |
26 |
Correct |
330 ms |
46428 KB |
Output is correct |
27 |
Correct |
320 ms |
45128 KB |
Output is correct |
28 |
Correct |
6 ms |
7660 KB |
Output is correct |
29 |
Correct |
6 ms |
7660 KB |
Output is correct |
30 |
Correct |
6 ms |
7660 KB |
Output is correct |
31 |
Correct |
6 ms |
7660 KB |
Output is correct |
32 |
Correct |
6 ms |
7660 KB |
Output is correct |
33 |
Correct |
6 ms |
7788 KB |
Output is correct |
34 |
Correct |
6 ms |
7660 KB |
Output is correct |
35 |
Correct |
6 ms |
7788 KB |
Output is correct |
36 |
Correct |
7 ms |
7660 KB |
Output is correct |
37 |
Correct |
20 ms |
11372 KB |
Output is correct |
38 |
Correct |
168 ms |
36068 KB |
Output is correct |
39 |
Correct |
151 ms |
36196 KB |
Output is correct |
40 |
Correct |
151 ms |
36200 KB |
Output is correct |
41 |
Correct |
149 ms |
35956 KB |
Output is correct |
42 |
Correct |
153 ms |
35684 KB |
Output is correct |
43 |
Correct |
138 ms |
33356 KB |
Output is correct |
44 |
Correct |
158 ms |
36324 KB |
Output is correct |
45 |
Correct |
150 ms |
36196 KB |
Output is correct |
46 |
Correct |
138 ms |
40268 KB |
Output is correct |
47 |
Correct |
161 ms |
36452 KB |
Output is correct |
48 |
Correct |
29 ms |
11500 KB |
Output is correct |
49 |
Correct |
271 ms |
41204 KB |
Output is correct |
50 |
Correct |
270 ms |
41160 KB |
Output is correct |
51 |
Correct |
269 ms |
40904 KB |
Output is correct |
52 |
Correct |
276 ms |
40940 KB |
Output is correct |
53 |
Correct |
264 ms |
39312 KB |
Output is correct |
54 |
Correct |
224 ms |
32468 KB |
Output is correct |
55 |
Correct |
274 ms |
41800 KB |
Output is correct |
56 |
Correct |
274 ms |
41036 KB |
Output is correct |
57 |
Correct |
337 ms |
44232 KB |
Output is correct |
58 |
Correct |
290 ms |
41936 KB |
Output is correct |
59 |
Correct |
113 ms |
17164 KB |
Output is correct |
60 |
Correct |
269 ms |
40932 KB |
Output is correct |
61 |
Correct |
281 ms |
39964 KB |
Output is correct |
62 |
Correct |
290 ms |
42056 KB |
Output is correct |
63 |
Correct |
410 ms |
45004 KB |
Output is correct |
64 |
Correct |
7 ms |
7660 KB |
Output is correct |
65 |
Correct |
6 ms |
7808 KB |
Output is correct |
66 |
Correct |
6 ms |
7660 KB |
Output is correct |
67 |
Correct |
6 ms |
7532 KB |
Output is correct |
68 |
Correct |
6 ms |
7660 KB |
Output is correct |
69 |
Correct |
7 ms |
7788 KB |
Output is correct |
70 |
Correct |
7 ms |
7788 KB |
Output is correct |
71 |
Correct |
6 ms |
7788 KB |
Output is correct |
72 |
Correct |
6 ms |
7788 KB |
Output is correct |
73 |
Correct |
6 ms |
7788 KB |
Output is correct |
74 |
Correct |
166 ms |
35972 KB |
Output is correct |
75 |
Correct |
149 ms |
36068 KB |
Output is correct |
76 |
Correct |
152 ms |
36072 KB |
Output is correct |
77 |
Correct |
157 ms |
37744 KB |
Output is correct |
78 |
Correct |
86 ms |
16612 KB |
Output is correct |
79 |
Correct |
75 ms |
16484 KB |
Output is correct |
80 |
Correct |
136 ms |
31204 KB |
Output is correct |
81 |
Correct |
193 ms |
40036 KB |
Output is correct |
82 |
Correct |
231 ms |
46052 KB |
Output is correct |
83 |
Correct |
212 ms |
42212 KB |
Output is correct |
84 |
Correct |
142 ms |
40032 KB |
Output is correct |
85 |
Correct |
204 ms |
42596 KB |
Output is correct |
86 |
Correct |
91 ms |
19220 KB |
Output is correct |
87 |
Correct |
280 ms |
41032 KB |
Output is correct |
88 |
Correct |
270 ms |
40904 KB |
Output is correct |
89 |
Correct |
336 ms |
40836 KB |
Output is correct |
90 |
Correct |
193 ms |
21436 KB |
Output is correct |
91 |
Correct |
204 ms |
23432 KB |
Output is correct |
92 |
Correct |
301 ms |
35908 KB |
Output is correct |
93 |
Correct |
321 ms |
44828 KB |
Output is correct |
94 |
Correct |
403 ms |
49400 KB |
Output is correct |
95 |
Correct |
337 ms |
45924 KB |
Output is correct |
96 |
Correct |
341 ms |
44744 KB |
Output is correct |
97 |
Correct |
370 ms |
45220 KB |
Output is correct |