#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9;
array<int, 3> Edge[200005];
vector<int> adj[200005];
int cnt = 0;
int ans[200005];
int N, M;
struct UnionFind {
vector<int> root;
vector<int> L, R, V;
UnionFind(int N) {
root.resize(N);
L.resize(N);
R.resize(N);
V.resize(N);
for(int i = 0; i<N;i++) {
root[i] = -1;
L[i] = R[i] = V[i] = i;
}
}
int Find(int n) {
if(root[n]<0) return n;
int r = Find(root[n]);
root[n] = r;
return r;
}
void Merge(int a, int b, int d) {
int x = Find(a), y = Find(b);
if(x==y) {
ans[V[x]] = min(ans[V[x]], d);
return;
}
if(root[x]>root[y]) {
swap(x, y);
swap(a, b);
}
root[x] += root[y];
root[y] = x;
bool isLine = true;
if(a==L[x]) {
int k = L[x];
L[x] = R[x];
R[x] = k;
}
if(a != R[x]) isLine = false;
if(b == R[y]) {
int k = L[y];
L[y] = R[y];
R[y] = k;
}
if(b != L[y]) isLine = false;
if(!isLine) {
ans[V[x]] = min(ans[V[x]], d);
ans[V[y]] = min(ans[V[y]], d);
}
ans[cnt] = min(ans[V[x]], ans[V[y]]);
ans[cnt] = max(ans[cnt], d);
if(ans[cnt]!=INF) isLine = false;
//assert(isLine);
/*cout << a << ' ' << b << ' ' << d << " Merging\n";
cout << x << ' ' << y <<'\n';
cout << L[x] << ' ' << R[x] << ' ' << L[y] << ' ' << R[y] << '\n';
cout << "isLine : " << isLine << '\n';*/
R[x] = R[y];
adj[cnt].push_back(V[x]);
adj[cnt].push_back(V[y]);
V[x] = cnt;
cnt++;
}
};
int par[200005][20];
int dep[200005];
void dfs(int c, int p, int d, int k) {
k = min(k, ans[c]);
ans[c] = k;
par[c][0] = p;
dep[c] = d;
for(int n : adj[c]) {
dfs(n, c, d+1, k);
}
}
int LCA(int x, int y) {
if(dep[x]<dep[y]) swap(x, y);
int d = dep[x] - dep[y];
int i;
for(i=19;i>=0;i--) {
if(d&(1<<i)) x = par[x][i];
}
if(x==y) return x;
for(i=19;i>=0;i--) {
if(par[x][i]!=par[y][i]) {
x = par[x][i], y = par[y][i];
}
}
assert(par[x][0]==par[y][0]);
return par[x][0];
}
void init(int _N, int _M,vector<int> _U, vector<int> _V, vector<int> _W) {
N = _N, M = _M;
int i, j;
for(i=0;i<M;i++) Edge[i] = {_W[i], _U[i], _V[i]};
cnt = N;
for(i=0;i<=2*N;i++) ans[i] = INF;
sort(Edge, Edge+M);
UnionFind UF(N);
for(i=0;i<M;i++) {
UF.Merge(Edge[i][1], Edge[i][2], Edge[i][0]);
}
dfs(cnt-1, cnt, 0, INF);
par[cnt][0] = cnt;
/*for(i=0;i<=cnt;i++) {
cout << par[i][0] << ' ';
}
cout << '\n';
for(i=0;i<=cnt;i++) {
cout << ans[i] << ' ';
}
cout << '\n';*/
for(j=1;j<20;j++) {
for(i=0;i<=cnt;i++) {
par[i][j] = par[par[i][j-1]][j-1];
}
}
}
int getMinimumFuelCapacity(int X, int Y) {
int v = ans[LCA(X, Y)];
//cout << X << ' ' << Y << ' ' << LCA(X, Y) << '\n';
return (v==INF?-1:v);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
4 ms |
5204 KB |
Output is correct |
9 |
Correct |
82 ms |
26528 KB |
Output is correct |
10 |
Correct |
109 ms |
31396 KB |
Output is correct |
11 |
Correct |
121 ms |
31044 KB |
Output is correct |
12 |
Correct |
113 ms |
32544 KB |
Output is correct |
13 |
Correct |
95 ms |
34836 KB |
Output is correct |
14 |
Correct |
89 ms |
26912 KB |
Output is correct |
15 |
Correct |
207 ms |
35436 KB |
Output is correct |
16 |
Correct |
210 ms |
34776 KB |
Output is correct |
17 |
Correct |
211 ms |
36384 KB |
Output is correct |
18 |
Correct |
274 ms |
38776 KB |
Output is correct |
19 |
Correct |
75 ms |
14472 KB |
Output is correct |
20 |
Correct |
206 ms |
35452 KB |
Output is correct |
21 |
Correct |
207 ms |
34756 KB |
Output is correct |
22 |
Correct |
211 ms |
36436 KB |
Output is correct |
23 |
Correct |
290 ms |
38804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
230 ms |
35020 KB |
Output is correct |
4 |
Correct |
238 ms |
36556 KB |
Output is correct |
5 |
Correct |
270 ms |
35596 KB |
Output is correct |
6 |
Correct |
253 ms |
36428 KB |
Output is correct |
7 |
Correct |
281 ms |
36000 KB |
Output is correct |
8 |
Correct |
228 ms |
34772 KB |
Output is correct |
9 |
Correct |
230 ms |
35796 KB |
Output is correct |
10 |
Correct |
223 ms |
34504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
4 ms |
5204 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5204 KB |
Output is correct |
11 |
Correct |
4 ms |
5204 KB |
Output is correct |
12 |
Correct |
3 ms |
5276 KB |
Output is correct |
13 |
Correct |
4 ms |
5204 KB |
Output is correct |
14 |
Correct |
4 ms |
5204 KB |
Output is correct |
15 |
Correct |
4 ms |
5244 KB |
Output is correct |
16 |
Correct |
3 ms |
5208 KB |
Output is correct |
17 |
Correct |
3 ms |
5284 KB |
Output is correct |
18 |
Correct |
3 ms |
5204 KB |
Output is correct |
19 |
Correct |
3 ms |
5152 KB |
Output is correct |
20 |
Correct |
3 ms |
5204 KB |
Output is correct |
21 |
Correct |
3 ms |
5204 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5204 KB |
Output is correct |
24 |
Correct |
5 ms |
5276 KB |
Output is correct |
25 |
Correct |
4 ms |
5320 KB |
Output is correct |
26 |
Correct |
4 ms |
5332 KB |
Output is correct |
27 |
Correct |
3 ms |
5204 KB |
Output is correct |
28 |
Correct |
4 ms |
5332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
4 ms |
5204 KB |
Output is correct |
10 |
Correct |
82 ms |
26528 KB |
Output is correct |
11 |
Correct |
109 ms |
31396 KB |
Output is correct |
12 |
Correct |
121 ms |
31044 KB |
Output is correct |
13 |
Correct |
113 ms |
32544 KB |
Output is correct |
14 |
Correct |
95 ms |
34836 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
4 ms |
5204 KB |
Output is correct |
17 |
Correct |
3 ms |
5276 KB |
Output is correct |
18 |
Correct |
4 ms |
5204 KB |
Output is correct |
19 |
Correct |
4 ms |
5204 KB |
Output is correct |
20 |
Correct |
4 ms |
5244 KB |
Output is correct |
21 |
Correct |
3 ms |
5208 KB |
Output is correct |
22 |
Correct |
3 ms |
5284 KB |
Output is correct |
23 |
Correct |
3 ms |
5204 KB |
Output is correct |
24 |
Correct |
3 ms |
5152 KB |
Output is correct |
25 |
Correct |
3 ms |
5204 KB |
Output is correct |
26 |
Correct |
3 ms |
5204 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
3 ms |
5204 KB |
Output is correct |
29 |
Correct |
5 ms |
5276 KB |
Output is correct |
30 |
Correct |
4 ms |
5320 KB |
Output is correct |
31 |
Correct |
4 ms |
5332 KB |
Output is correct |
32 |
Correct |
3 ms |
5204 KB |
Output is correct |
33 |
Correct |
4 ms |
5332 KB |
Output is correct |
34 |
Correct |
11 ms |
8660 KB |
Output is correct |
35 |
Correct |
103 ms |
32272 KB |
Output is correct |
36 |
Correct |
106 ms |
32372 KB |
Output is correct |
37 |
Correct |
104 ms |
32204 KB |
Output is correct |
38 |
Correct |
105 ms |
31876 KB |
Output is correct |
39 |
Correct |
101 ms |
31748 KB |
Output is correct |
40 |
Correct |
99 ms |
29484 KB |
Output is correct |
41 |
Correct |
110 ms |
32556 KB |
Output is correct |
42 |
Correct |
102 ms |
32160 KB |
Output is correct |
43 |
Correct |
99 ms |
35368 KB |
Output is correct |
44 |
Correct |
107 ms |
32596 KB |
Output is correct |
45 |
Correct |
111 ms |
31672 KB |
Output is correct |
46 |
Correct |
108 ms |
32200 KB |
Output is correct |
47 |
Correct |
122 ms |
32240 KB |
Output is correct |
48 |
Correct |
122 ms |
33356 KB |
Output is correct |
49 |
Correct |
63 ms |
13516 KB |
Output is correct |
50 |
Correct |
54 ms |
13308 KB |
Output is correct |
51 |
Correct |
119 ms |
27180 KB |
Output is correct |
52 |
Correct |
213 ms |
36028 KB |
Output is correct |
53 |
Correct |
141 ms |
37580 KB |
Output is correct |
54 |
Correct |
155 ms |
37888 KB |
Output is correct |
55 |
Correct |
104 ms |
35020 KB |
Output is correct |
56 |
Correct |
135 ms |
37980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
4 ms |
5204 KB |
Output is correct |
9 |
Correct |
82 ms |
26528 KB |
Output is correct |
10 |
Correct |
109 ms |
31396 KB |
Output is correct |
11 |
Correct |
121 ms |
31044 KB |
Output is correct |
12 |
Correct |
113 ms |
32544 KB |
Output is correct |
13 |
Correct |
95 ms |
34836 KB |
Output is correct |
14 |
Correct |
89 ms |
26912 KB |
Output is correct |
15 |
Correct |
207 ms |
35436 KB |
Output is correct |
16 |
Correct |
210 ms |
34776 KB |
Output is correct |
17 |
Correct |
211 ms |
36384 KB |
Output is correct |
18 |
Correct |
274 ms |
38776 KB |
Output is correct |
19 |
Correct |
230 ms |
35020 KB |
Output is correct |
20 |
Correct |
238 ms |
36556 KB |
Output is correct |
21 |
Correct |
270 ms |
35596 KB |
Output is correct |
22 |
Correct |
253 ms |
36428 KB |
Output is correct |
23 |
Correct |
281 ms |
36000 KB |
Output is correct |
24 |
Correct |
228 ms |
34772 KB |
Output is correct |
25 |
Correct |
230 ms |
35796 KB |
Output is correct |
26 |
Correct |
223 ms |
34504 KB |
Output is correct |
27 |
Correct |
3 ms |
5204 KB |
Output is correct |
28 |
Correct |
4 ms |
5204 KB |
Output is correct |
29 |
Correct |
3 ms |
5276 KB |
Output is correct |
30 |
Correct |
4 ms |
5204 KB |
Output is correct |
31 |
Correct |
4 ms |
5204 KB |
Output is correct |
32 |
Correct |
4 ms |
5244 KB |
Output is correct |
33 |
Correct |
3 ms |
5208 KB |
Output is correct |
34 |
Correct |
3 ms |
5284 KB |
Output is correct |
35 |
Correct |
3 ms |
5204 KB |
Output is correct |
36 |
Correct |
11 ms |
8660 KB |
Output is correct |
37 |
Correct |
103 ms |
32272 KB |
Output is correct |
38 |
Correct |
106 ms |
32372 KB |
Output is correct |
39 |
Correct |
104 ms |
32204 KB |
Output is correct |
40 |
Correct |
105 ms |
31876 KB |
Output is correct |
41 |
Correct |
101 ms |
31748 KB |
Output is correct |
42 |
Correct |
99 ms |
29484 KB |
Output is correct |
43 |
Correct |
110 ms |
32556 KB |
Output is correct |
44 |
Correct |
102 ms |
32160 KB |
Output is correct |
45 |
Correct |
99 ms |
35368 KB |
Output is correct |
46 |
Correct |
107 ms |
32596 KB |
Output is correct |
47 |
Correct |
19 ms |
8828 KB |
Output is correct |
48 |
Correct |
241 ms |
36080 KB |
Output is correct |
49 |
Correct |
202 ms |
36068 KB |
Output is correct |
50 |
Correct |
206 ms |
36124 KB |
Output is correct |
51 |
Correct |
218 ms |
35932 KB |
Output is correct |
52 |
Correct |
189 ms |
34256 KB |
Output is correct |
53 |
Correct |
169 ms |
28048 KB |
Output is correct |
54 |
Correct |
244 ms |
36428 KB |
Output is correct |
55 |
Correct |
213 ms |
36120 KB |
Output is correct |
56 |
Correct |
253 ms |
38556 KB |
Output is correct |
57 |
Correct |
204 ms |
36488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
4 ms |
5204 KB |
Output is correct |
10 |
Correct |
82 ms |
26528 KB |
Output is correct |
11 |
Correct |
109 ms |
31396 KB |
Output is correct |
12 |
Correct |
121 ms |
31044 KB |
Output is correct |
13 |
Correct |
113 ms |
32544 KB |
Output is correct |
14 |
Correct |
95 ms |
34836 KB |
Output is correct |
15 |
Correct |
89 ms |
26912 KB |
Output is correct |
16 |
Correct |
207 ms |
35436 KB |
Output is correct |
17 |
Correct |
210 ms |
34776 KB |
Output is correct |
18 |
Correct |
211 ms |
36384 KB |
Output is correct |
19 |
Correct |
274 ms |
38776 KB |
Output is correct |
20 |
Correct |
230 ms |
35020 KB |
Output is correct |
21 |
Correct |
238 ms |
36556 KB |
Output is correct |
22 |
Correct |
270 ms |
35596 KB |
Output is correct |
23 |
Correct |
253 ms |
36428 KB |
Output is correct |
24 |
Correct |
281 ms |
36000 KB |
Output is correct |
25 |
Correct |
228 ms |
34772 KB |
Output is correct |
26 |
Correct |
230 ms |
35796 KB |
Output is correct |
27 |
Correct |
223 ms |
34504 KB |
Output is correct |
28 |
Correct |
3 ms |
5204 KB |
Output is correct |
29 |
Correct |
4 ms |
5204 KB |
Output is correct |
30 |
Correct |
3 ms |
5276 KB |
Output is correct |
31 |
Correct |
4 ms |
5204 KB |
Output is correct |
32 |
Correct |
4 ms |
5204 KB |
Output is correct |
33 |
Correct |
4 ms |
5244 KB |
Output is correct |
34 |
Correct |
3 ms |
5208 KB |
Output is correct |
35 |
Correct |
3 ms |
5284 KB |
Output is correct |
36 |
Correct |
3 ms |
5204 KB |
Output is correct |
37 |
Correct |
11 ms |
8660 KB |
Output is correct |
38 |
Correct |
103 ms |
32272 KB |
Output is correct |
39 |
Correct |
106 ms |
32372 KB |
Output is correct |
40 |
Correct |
104 ms |
32204 KB |
Output is correct |
41 |
Correct |
105 ms |
31876 KB |
Output is correct |
42 |
Correct |
101 ms |
31748 KB |
Output is correct |
43 |
Correct |
99 ms |
29484 KB |
Output is correct |
44 |
Correct |
110 ms |
32556 KB |
Output is correct |
45 |
Correct |
102 ms |
32160 KB |
Output is correct |
46 |
Correct |
99 ms |
35368 KB |
Output is correct |
47 |
Correct |
107 ms |
32596 KB |
Output is correct |
48 |
Correct |
19 ms |
8828 KB |
Output is correct |
49 |
Correct |
241 ms |
36080 KB |
Output is correct |
50 |
Correct |
202 ms |
36068 KB |
Output is correct |
51 |
Correct |
206 ms |
36124 KB |
Output is correct |
52 |
Correct |
218 ms |
35932 KB |
Output is correct |
53 |
Correct |
189 ms |
34256 KB |
Output is correct |
54 |
Correct |
169 ms |
28048 KB |
Output is correct |
55 |
Correct |
244 ms |
36428 KB |
Output is correct |
56 |
Correct |
213 ms |
36120 KB |
Output is correct |
57 |
Correct |
253 ms |
38556 KB |
Output is correct |
58 |
Correct |
204 ms |
36488 KB |
Output is correct |
59 |
Correct |
75 ms |
14472 KB |
Output is correct |
60 |
Correct |
206 ms |
35452 KB |
Output is correct |
61 |
Correct |
207 ms |
34756 KB |
Output is correct |
62 |
Correct |
211 ms |
36436 KB |
Output is correct |
63 |
Correct |
290 ms |
38804 KB |
Output is correct |
64 |
Correct |
3 ms |
5152 KB |
Output is correct |
65 |
Correct |
3 ms |
5204 KB |
Output is correct |
66 |
Correct |
3 ms |
5204 KB |
Output is correct |
67 |
Correct |
4 ms |
5076 KB |
Output is correct |
68 |
Correct |
3 ms |
5204 KB |
Output is correct |
69 |
Correct |
5 ms |
5276 KB |
Output is correct |
70 |
Correct |
4 ms |
5320 KB |
Output is correct |
71 |
Correct |
4 ms |
5332 KB |
Output is correct |
72 |
Correct |
3 ms |
5204 KB |
Output is correct |
73 |
Correct |
4 ms |
5332 KB |
Output is correct |
74 |
Correct |
111 ms |
31672 KB |
Output is correct |
75 |
Correct |
108 ms |
32200 KB |
Output is correct |
76 |
Correct |
122 ms |
32240 KB |
Output is correct |
77 |
Correct |
122 ms |
33356 KB |
Output is correct |
78 |
Correct |
63 ms |
13516 KB |
Output is correct |
79 |
Correct |
54 ms |
13308 KB |
Output is correct |
80 |
Correct |
119 ms |
27180 KB |
Output is correct |
81 |
Correct |
213 ms |
36028 KB |
Output is correct |
82 |
Correct |
141 ms |
37580 KB |
Output is correct |
83 |
Correct |
155 ms |
37888 KB |
Output is correct |
84 |
Correct |
104 ms |
35020 KB |
Output is correct |
85 |
Correct |
135 ms |
37980 KB |
Output is correct |
86 |
Correct |
56 ms |
16028 KB |
Output is correct |
87 |
Correct |
205 ms |
36128 KB |
Output is correct |
88 |
Correct |
210 ms |
36020 KB |
Output is correct |
89 |
Correct |
205 ms |
35732 KB |
Output is correct |
90 |
Correct |
148 ms |
18120 KB |
Output is correct |
91 |
Correct |
149 ms |
20128 KB |
Output is correct |
92 |
Correct |
189 ms |
31008 KB |
Output is correct |
93 |
Correct |
244 ms |
39624 KB |
Output is correct |
94 |
Correct |
251 ms |
41268 KB |
Output is correct |
95 |
Correct |
253 ms |
41768 KB |
Output is correct |
96 |
Correct |
262 ms |
38812 KB |
Output is correct |
97 |
Correct |
238 ms |
39584 KB |
Output is correct |