#include <bits/stdc++.h>
#define FOR(i,l,r) for(int i=(l); i<=(r); ++i)
#define REP(i,l,r) for(int i=(l); i<(r); ++i)
#define FORD(i,r,l) for(int i=(r); i>=(l); --i)
#define REPD(i,r,l) for(int i=(r)-1; i>=(l); --i)
#define getbit(x, i) (((x) >> (i)) & 1)
using namespace std;
const int N = 200005, logN = 19;
int n, depth[N], deg[N], dsu[N], isAny[N], ans[N];
int p[N][logN];
vector<int> S[N];
int DAD(int u) {
return dsu[u] < 0 ? u : dsu[u] = DAD(dsu[u]);
}
void Union(int u, int v, int w) {
int pu = DAD(u);
int pv = DAD(v);
if (pu == pv) {
if (!isAny[pu]) {
isAny[pu] = true;
ans[pu] = w;
}
return;
}
int p_uv = n++;
dsu[pu] = dsu[pv] = p_uv;
S[p_uv].push_back(pu);
S[p_uv].push_back(pv);
isAny[p_uv] = (isAny[pu] || isAny[pv] || deg[u] > 1 || deg[v] > 1);
if (isAny[p_uv]) ans[p_uv] = w;
++deg[u];
++deg[v];
}
void DFS(int u, int pa) {
if (!isAny[u] && isAny[pa]) {
isAny[u] = true;
ans[u] = ans[pa];
}
depth[u] = depth[pa] + 1;
p[u][0] = pa;
REP(i, 1, logN) p[u][i] = p[p[u][i - 1]][i - 1];
for(int v : S[u]) DFS(v, u);
}
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
n = N;
fill(dsu, dsu + n*2, -1);
vector<int> tmp;
REP(i, 0, M) tmp.push_back(i);
sort(tmp.begin(), tmp.end(), [&] (int x, int y) { return W[x] < W[y]; });
for(int id : tmp) {
Union(U[id], V[id], W[id]);
}
REP(i, 0, logN) p[n][i] = n;
DFS(n - 1, n);
}
int LCA(int u, int v) {
if (depth[u] > depth[v]) swap(u, v);
int diff = depth[v] - depth[u];
REPD(i, logN, 0) if (getbit(diff, i)) v = p[v][i];
REPD(i, logN, 0) if (p[u][i] != p[v][i]) u = p[u][i], v = p[v][i];
return u == v ? u : p[u][0];
}
int getMinimumFuelCapacity(int u, int v) {
int pa = LCA(u, v);
return isAny[pa] ? ans[pa] : -1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
5 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
4 ms |
5204 KB |
Output is correct |
6 |
Correct |
6 ms |
5204 KB |
Output is correct |
7 |
Correct |
5 ms |
5272 KB |
Output is correct |
8 |
Correct |
5 ms |
5204 KB |
Output is correct |
9 |
Correct |
108 ms |
24724 KB |
Output is correct |
10 |
Correct |
134 ms |
29148 KB |
Output is correct |
11 |
Correct |
129 ms |
28760 KB |
Output is correct |
12 |
Correct |
135 ms |
30096 KB |
Output is correct |
13 |
Correct |
128 ms |
31684 KB |
Output is correct |
14 |
Correct |
118 ms |
25100 KB |
Output is correct |
15 |
Correct |
251 ms |
33152 KB |
Output is correct |
16 |
Correct |
278 ms |
32476 KB |
Output is correct |
17 |
Correct |
281 ms |
33964 KB |
Output is correct |
18 |
Correct |
378 ms |
35628 KB |
Output is correct |
19 |
Correct |
95 ms |
14316 KB |
Output is correct |
20 |
Correct |
270 ms |
35360 KB |
Output is correct |
21 |
Correct |
295 ms |
34684 KB |
Output is correct |
22 |
Correct |
273 ms |
36448 KB |
Output is correct |
23 |
Correct |
295 ms |
37968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
5 ms |
4948 KB |
Output is correct |
3 |
Correct |
362 ms |
37320 KB |
Output is correct |
4 |
Correct |
262 ms |
38644 KB |
Output is correct |
5 |
Correct |
309 ms |
38136 KB |
Output is correct |
6 |
Correct |
290 ms |
38396 KB |
Output is correct |
7 |
Correct |
297 ms |
38352 KB |
Output is correct |
8 |
Correct |
259 ms |
37256 KB |
Output is correct |
9 |
Correct |
317 ms |
38048 KB |
Output is correct |
10 |
Correct |
300 ms |
37004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
5 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
4 ms |
5204 KB |
Output is correct |
6 |
Correct |
6 ms |
5204 KB |
Output is correct |
7 |
Correct |
5 ms |
5272 KB |
Output is correct |
8 |
Correct |
5 ms |
5204 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
4 ms |
5204 KB |
Output is correct |
11 |
Correct |
5 ms |
5276 KB |
Output is correct |
12 |
Correct |
4 ms |
5284 KB |
Output is correct |
13 |
Correct |
4 ms |
5144 KB |
Output is correct |
14 |
Correct |
3 ms |
5252 KB |
Output is correct |
15 |
Correct |
4 ms |
5280 KB |
Output is correct |
16 |
Correct |
4 ms |
5280 KB |
Output is correct |
17 |
Correct |
4 ms |
5204 KB |
Output is correct |
18 |
Correct |
5 ms |
5284 KB |
Output is correct |
19 |
Correct |
4 ms |
5204 KB |
Output is correct |
20 |
Correct |
6 ms |
5204 KB |
Output is correct |
21 |
Correct |
4 ms |
5204 KB |
Output is correct |
22 |
Correct |
4 ms |
5156 KB |
Output is correct |
23 |
Correct |
4 ms |
5204 KB |
Output is correct |
24 |
Correct |
5 ms |
5316 KB |
Output is correct |
25 |
Correct |
4 ms |
5332 KB |
Output is correct |
26 |
Correct |
4 ms |
5268 KB |
Output is correct |
27 |
Correct |
4 ms |
5204 KB |
Output is correct |
28 |
Correct |
4 ms |
5332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
5 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
4 ms |
5204 KB |
Output is correct |
7 |
Correct |
6 ms |
5204 KB |
Output is correct |
8 |
Correct |
5 ms |
5272 KB |
Output is correct |
9 |
Correct |
5 ms |
5204 KB |
Output is correct |
10 |
Correct |
108 ms |
24724 KB |
Output is correct |
11 |
Correct |
134 ms |
29148 KB |
Output is correct |
12 |
Correct |
129 ms |
28760 KB |
Output is correct |
13 |
Correct |
135 ms |
30096 KB |
Output is correct |
14 |
Correct |
128 ms |
31684 KB |
Output is correct |
15 |
Correct |
4 ms |
5204 KB |
Output is correct |
16 |
Correct |
5 ms |
5276 KB |
Output is correct |
17 |
Correct |
4 ms |
5284 KB |
Output is correct |
18 |
Correct |
4 ms |
5144 KB |
Output is correct |
19 |
Correct |
3 ms |
5252 KB |
Output is correct |
20 |
Correct |
4 ms |
5280 KB |
Output is correct |
21 |
Correct |
4 ms |
5280 KB |
Output is correct |
22 |
Correct |
4 ms |
5204 KB |
Output is correct |
23 |
Correct |
5 ms |
5284 KB |
Output is correct |
24 |
Correct |
4 ms |
5204 KB |
Output is correct |
25 |
Correct |
6 ms |
5204 KB |
Output is correct |
26 |
Correct |
4 ms |
5204 KB |
Output is correct |
27 |
Correct |
4 ms |
5156 KB |
Output is correct |
28 |
Correct |
4 ms |
5204 KB |
Output is correct |
29 |
Correct |
5 ms |
5316 KB |
Output is correct |
30 |
Correct |
4 ms |
5332 KB |
Output is correct |
31 |
Correct |
4 ms |
5268 KB |
Output is correct |
32 |
Correct |
4 ms |
5204 KB |
Output is correct |
33 |
Correct |
4 ms |
5332 KB |
Output is correct |
34 |
Correct |
14 ms |
8532 KB |
Output is correct |
35 |
Correct |
124 ms |
31000 KB |
Output is correct |
36 |
Correct |
130 ms |
31004 KB |
Output is correct |
37 |
Correct |
120 ms |
30976 KB |
Output is correct |
38 |
Correct |
124 ms |
30728 KB |
Output is correct |
39 |
Correct |
120 ms |
30592 KB |
Output is correct |
40 |
Correct |
114 ms |
28440 KB |
Output is correct |
41 |
Correct |
123 ms |
31252 KB |
Output is correct |
42 |
Correct |
121 ms |
30988 KB |
Output is correct |
43 |
Correct |
99 ms |
33032 KB |
Output is correct |
44 |
Correct |
137 ms |
31468 KB |
Output is correct |
45 |
Correct |
131 ms |
29760 KB |
Output is correct |
46 |
Correct |
119 ms |
31036 KB |
Output is correct |
47 |
Correct |
121 ms |
30948 KB |
Output is correct |
48 |
Correct |
130 ms |
31756 KB |
Output is correct |
49 |
Correct |
69 ms |
12592 KB |
Output is correct |
50 |
Correct |
59 ms |
12312 KB |
Output is correct |
51 |
Correct |
106 ms |
25540 KB |
Output is correct |
52 |
Correct |
152 ms |
34260 KB |
Output is correct |
53 |
Correct |
175 ms |
35496 KB |
Output is correct |
54 |
Correct |
178 ms |
35872 KB |
Output is correct |
55 |
Correct |
99 ms |
32960 KB |
Output is correct |
56 |
Correct |
167 ms |
35788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
5 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
4 ms |
5204 KB |
Output is correct |
6 |
Correct |
6 ms |
5204 KB |
Output is correct |
7 |
Correct |
5 ms |
5272 KB |
Output is correct |
8 |
Correct |
5 ms |
5204 KB |
Output is correct |
9 |
Correct |
108 ms |
24724 KB |
Output is correct |
10 |
Correct |
134 ms |
29148 KB |
Output is correct |
11 |
Correct |
129 ms |
28760 KB |
Output is correct |
12 |
Correct |
135 ms |
30096 KB |
Output is correct |
13 |
Correct |
128 ms |
31684 KB |
Output is correct |
14 |
Correct |
118 ms |
25100 KB |
Output is correct |
15 |
Correct |
251 ms |
33152 KB |
Output is correct |
16 |
Correct |
278 ms |
32476 KB |
Output is correct |
17 |
Correct |
281 ms |
33964 KB |
Output is correct |
18 |
Correct |
378 ms |
35628 KB |
Output is correct |
19 |
Correct |
362 ms |
37320 KB |
Output is correct |
20 |
Correct |
262 ms |
38644 KB |
Output is correct |
21 |
Correct |
309 ms |
38136 KB |
Output is correct |
22 |
Correct |
290 ms |
38396 KB |
Output is correct |
23 |
Correct |
297 ms |
38352 KB |
Output is correct |
24 |
Correct |
259 ms |
37256 KB |
Output is correct |
25 |
Correct |
317 ms |
38048 KB |
Output is correct |
26 |
Correct |
300 ms |
37004 KB |
Output is correct |
27 |
Correct |
4 ms |
5204 KB |
Output is correct |
28 |
Correct |
5 ms |
5276 KB |
Output is correct |
29 |
Correct |
4 ms |
5284 KB |
Output is correct |
30 |
Correct |
4 ms |
5144 KB |
Output is correct |
31 |
Correct |
3 ms |
5252 KB |
Output is correct |
32 |
Correct |
4 ms |
5280 KB |
Output is correct |
33 |
Correct |
4 ms |
5280 KB |
Output is correct |
34 |
Correct |
4 ms |
5204 KB |
Output is correct |
35 |
Correct |
5 ms |
5284 KB |
Output is correct |
36 |
Correct |
14 ms |
8532 KB |
Output is correct |
37 |
Correct |
124 ms |
31000 KB |
Output is correct |
38 |
Correct |
130 ms |
31004 KB |
Output is correct |
39 |
Correct |
120 ms |
30976 KB |
Output is correct |
40 |
Correct |
124 ms |
30728 KB |
Output is correct |
41 |
Correct |
120 ms |
30592 KB |
Output is correct |
42 |
Correct |
114 ms |
28440 KB |
Output is correct |
43 |
Correct |
123 ms |
31252 KB |
Output is correct |
44 |
Correct |
121 ms |
30988 KB |
Output is correct |
45 |
Correct |
99 ms |
33032 KB |
Output is correct |
46 |
Correct |
137 ms |
31468 KB |
Output is correct |
47 |
Correct |
24 ms |
8732 KB |
Output is correct |
48 |
Correct |
235 ms |
35572 KB |
Output is correct |
49 |
Correct |
284 ms |
35452 KB |
Output is correct |
50 |
Correct |
228 ms |
35488 KB |
Output is correct |
51 |
Correct |
235 ms |
35316 KB |
Output is correct |
52 |
Correct |
220 ms |
34056 KB |
Output is correct |
53 |
Correct |
202 ms |
28048 KB |
Output is correct |
54 |
Correct |
236 ms |
36456 KB |
Output is correct |
55 |
Correct |
236 ms |
35708 KB |
Output is correct |
56 |
Correct |
272 ms |
37216 KB |
Output is correct |
57 |
Correct |
257 ms |
36436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
5 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
4 ms |
5204 KB |
Output is correct |
7 |
Correct |
6 ms |
5204 KB |
Output is correct |
8 |
Correct |
5 ms |
5272 KB |
Output is correct |
9 |
Correct |
5 ms |
5204 KB |
Output is correct |
10 |
Correct |
108 ms |
24724 KB |
Output is correct |
11 |
Correct |
134 ms |
29148 KB |
Output is correct |
12 |
Correct |
129 ms |
28760 KB |
Output is correct |
13 |
Correct |
135 ms |
30096 KB |
Output is correct |
14 |
Correct |
128 ms |
31684 KB |
Output is correct |
15 |
Correct |
118 ms |
25100 KB |
Output is correct |
16 |
Correct |
251 ms |
33152 KB |
Output is correct |
17 |
Correct |
278 ms |
32476 KB |
Output is correct |
18 |
Correct |
281 ms |
33964 KB |
Output is correct |
19 |
Correct |
378 ms |
35628 KB |
Output is correct |
20 |
Correct |
362 ms |
37320 KB |
Output is correct |
21 |
Correct |
262 ms |
38644 KB |
Output is correct |
22 |
Correct |
309 ms |
38136 KB |
Output is correct |
23 |
Correct |
290 ms |
38396 KB |
Output is correct |
24 |
Correct |
297 ms |
38352 KB |
Output is correct |
25 |
Correct |
259 ms |
37256 KB |
Output is correct |
26 |
Correct |
317 ms |
38048 KB |
Output is correct |
27 |
Correct |
300 ms |
37004 KB |
Output is correct |
28 |
Correct |
4 ms |
5204 KB |
Output is correct |
29 |
Correct |
5 ms |
5276 KB |
Output is correct |
30 |
Correct |
4 ms |
5284 KB |
Output is correct |
31 |
Correct |
4 ms |
5144 KB |
Output is correct |
32 |
Correct |
3 ms |
5252 KB |
Output is correct |
33 |
Correct |
4 ms |
5280 KB |
Output is correct |
34 |
Correct |
4 ms |
5280 KB |
Output is correct |
35 |
Correct |
4 ms |
5204 KB |
Output is correct |
36 |
Correct |
5 ms |
5284 KB |
Output is correct |
37 |
Correct |
14 ms |
8532 KB |
Output is correct |
38 |
Correct |
124 ms |
31000 KB |
Output is correct |
39 |
Correct |
130 ms |
31004 KB |
Output is correct |
40 |
Correct |
120 ms |
30976 KB |
Output is correct |
41 |
Correct |
124 ms |
30728 KB |
Output is correct |
42 |
Correct |
120 ms |
30592 KB |
Output is correct |
43 |
Correct |
114 ms |
28440 KB |
Output is correct |
44 |
Correct |
123 ms |
31252 KB |
Output is correct |
45 |
Correct |
121 ms |
30988 KB |
Output is correct |
46 |
Correct |
99 ms |
33032 KB |
Output is correct |
47 |
Correct |
137 ms |
31468 KB |
Output is correct |
48 |
Correct |
24 ms |
8732 KB |
Output is correct |
49 |
Correct |
235 ms |
35572 KB |
Output is correct |
50 |
Correct |
284 ms |
35452 KB |
Output is correct |
51 |
Correct |
228 ms |
35488 KB |
Output is correct |
52 |
Correct |
235 ms |
35316 KB |
Output is correct |
53 |
Correct |
220 ms |
34056 KB |
Output is correct |
54 |
Correct |
202 ms |
28048 KB |
Output is correct |
55 |
Correct |
236 ms |
36456 KB |
Output is correct |
56 |
Correct |
236 ms |
35708 KB |
Output is correct |
57 |
Correct |
272 ms |
37216 KB |
Output is correct |
58 |
Correct |
257 ms |
36436 KB |
Output is correct |
59 |
Correct |
95 ms |
14316 KB |
Output is correct |
60 |
Correct |
270 ms |
35360 KB |
Output is correct |
61 |
Correct |
295 ms |
34684 KB |
Output is correct |
62 |
Correct |
273 ms |
36448 KB |
Output is correct |
63 |
Correct |
295 ms |
37968 KB |
Output is correct |
64 |
Correct |
4 ms |
5204 KB |
Output is correct |
65 |
Correct |
6 ms |
5204 KB |
Output is correct |
66 |
Correct |
4 ms |
5204 KB |
Output is correct |
67 |
Correct |
4 ms |
5156 KB |
Output is correct |
68 |
Correct |
4 ms |
5204 KB |
Output is correct |
69 |
Correct |
5 ms |
5316 KB |
Output is correct |
70 |
Correct |
4 ms |
5332 KB |
Output is correct |
71 |
Correct |
4 ms |
5268 KB |
Output is correct |
72 |
Correct |
4 ms |
5204 KB |
Output is correct |
73 |
Correct |
4 ms |
5332 KB |
Output is correct |
74 |
Correct |
131 ms |
29760 KB |
Output is correct |
75 |
Correct |
119 ms |
31036 KB |
Output is correct |
76 |
Correct |
121 ms |
30948 KB |
Output is correct |
77 |
Correct |
130 ms |
31756 KB |
Output is correct |
78 |
Correct |
69 ms |
12592 KB |
Output is correct |
79 |
Correct |
59 ms |
12312 KB |
Output is correct |
80 |
Correct |
106 ms |
25540 KB |
Output is correct |
81 |
Correct |
152 ms |
34260 KB |
Output is correct |
82 |
Correct |
175 ms |
35496 KB |
Output is correct |
83 |
Correct |
178 ms |
35872 KB |
Output is correct |
84 |
Correct |
99 ms |
32960 KB |
Output is correct |
85 |
Correct |
167 ms |
35788 KB |
Output is correct |
86 |
Correct |
67 ms |
15312 KB |
Output is correct |
87 |
Correct |
242 ms |
35572 KB |
Output is correct |
88 |
Correct |
219 ms |
35556 KB |
Output is correct |
89 |
Correct |
255 ms |
34544 KB |
Output is correct |
90 |
Correct |
158 ms |
16408 KB |
Output is correct |
91 |
Correct |
172 ms |
18376 KB |
Output is correct |
92 |
Correct |
218 ms |
30180 KB |
Output is correct |
93 |
Correct |
271 ms |
38312 KB |
Output is correct |
94 |
Correct |
286 ms |
39264 KB |
Output is correct |
95 |
Correct |
303 ms |
39840 KB |
Output is correct |
96 |
Correct |
310 ms |
37316 KB |
Output is correct |
97 |
Correct |
270 ms |
38428 KB |
Output is correct |