#include "swap.h"
#include <bits/stdc++.h>
#define FOR(i,x,n) for(int i=x; i<n; i++)
#define F0R(i,n) FOR(i,0,n)
#define ROF(i,x,n) for(int i=n-1; i>=x; i--)
#define R0F(i,n) ROF(i,0,n)
#define WTF cout << "WTF" << endl
#define IOS ios::sync_with_stdio(false); cin.tie(0)
#define F first
#define S second
#define pb push_back
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
const int N = 3e5 + 7;
const int ALPHA = 27;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int LOG = 22;
struct Edge {
int sp, ep, val;
inline bool operator < (const Edge &other) {
return val < other.val;
}
} eset[N];
int n, m;
int par[N], weight[N], height[N];
int deg[N], ans[N], up[N][LOG];
bool possible[N];
VI adj[N];
int grand(int x) {
if (par[x] < 0) return x;
return par[x] = grand(par[x]);
}
inline void merge(int a, int b, int x) {
deg[a]++; deg[b]++;
if (deg[a] >= 3 || deg[b] >= 3) possible[n] = 1;
a = grand(a); b = grand(b);
possible[n] |= possible[a] | possible[b];
adj[n].pb(a);
if (a != b) adj[n].pb(b);
else possible[n] = 1;
weight[n] = x;
par[a] = par[b] = n;
n++;
return;
}
void dfs(int now = n - 1, int best = INF, int h = 0) {
if (possible[now]) best = weight[now];
ans[now] = best;
height[now] = h;
for(int on : adj[now]) {
dfs(on, best, h + 1);
up[on][0] = now;
}
return;
}
void precalc() {
up[n - 1][0] = up[n][0] = 0;
FOR(k, 1, LOG) F0R(i, n + 1)
up[i][k] = up[ up[i][k - 1] ][k - 1];
return;
}
inline int lca(int a, int b) {
if (height[a] < height[b]) swap(a, b);
R0F(i, LOG) if (height[a] - (1 << i) >= height[b])
a = up[a][i];
if (a == b) return a;
R0F(i, LOG) if (up[a][i] != up[b][i]) {
a = up[a][i];
b = up[b][i];
}
return up[a][0];
}
void init(int p, int q, VI us, VI vs, VI ws) {
n = p; m = q;
F0R(i, m) {
eset[i].sp = us[i];
eset[i].ep = vs[i];
eset[i].val = ws[i];
}
fill(par, par + N, -1);
sort(eset, eset + m);
F0R(i, m) merge(eset[i].sp, eset[i].ep, eset[i].val);
dfs();
precalc();
}
int getMinimumFuelCapacity(int x, int y) {
int z = lca(x, y);
return (ans[z] == INF ? -1 : ans[z]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8532 KB |
Output is correct |
2 |
Correct |
4 ms |
8532 KB |
Output is correct |
3 |
Correct |
5 ms |
8532 KB |
Output is correct |
4 |
Correct |
5 ms |
8640 KB |
Output is correct |
5 |
Correct |
5 ms |
8796 KB |
Output is correct |
6 |
Correct |
7 ms |
8788 KB |
Output is correct |
7 |
Correct |
5 ms |
8796 KB |
Output is correct |
8 |
Correct |
5 ms |
8780 KB |
Output is correct |
9 |
Correct |
93 ms |
29776 KB |
Output is correct |
10 |
Correct |
115 ms |
34744 KB |
Output is correct |
11 |
Correct |
114 ms |
34200 KB |
Output is correct |
12 |
Correct |
125 ms |
35308 KB |
Output is correct |
13 |
Correct |
116 ms |
38732 KB |
Output is correct |
14 |
Correct |
99 ms |
29828 KB |
Output is correct |
15 |
Correct |
213 ms |
36528 KB |
Output is correct |
16 |
Correct |
224 ms |
36028 KB |
Output is correct |
17 |
Correct |
221 ms |
37368 KB |
Output is correct |
18 |
Correct |
287 ms |
40500 KB |
Output is correct |
19 |
Correct |
87 ms |
16724 KB |
Output is correct |
20 |
Correct |
210 ms |
37848 KB |
Output is correct |
21 |
Correct |
226 ms |
37212 KB |
Output is correct |
22 |
Correct |
232 ms |
38864 KB |
Output is correct |
23 |
Correct |
305 ms |
42048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8532 KB |
Output is correct |
2 |
Correct |
4 ms |
8532 KB |
Output is correct |
3 |
Correct |
276 ms |
46132 KB |
Output is correct |
4 |
Correct |
287 ms |
47736 KB |
Output is correct |
5 |
Correct |
305 ms |
46968 KB |
Output is correct |
6 |
Correct |
271 ms |
47556 KB |
Output is correct |
7 |
Correct |
299 ms |
47400 KB |
Output is correct |
8 |
Correct |
264 ms |
45996 KB |
Output is correct |
9 |
Correct |
269 ms |
47180 KB |
Output is correct |
10 |
Correct |
265 ms |
45716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8532 KB |
Output is correct |
2 |
Correct |
4 ms |
8532 KB |
Output is correct |
3 |
Correct |
5 ms |
8532 KB |
Output is correct |
4 |
Correct |
5 ms |
8640 KB |
Output is correct |
5 |
Correct |
5 ms |
8796 KB |
Output is correct |
6 |
Correct |
7 ms |
8788 KB |
Output is correct |
7 |
Correct |
5 ms |
8796 KB |
Output is correct |
8 |
Correct |
5 ms |
8780 KB |
Output is correct |
9 |
Correct |
4 ms |
8532 KB |
Output is correct |
10 |
Correct |
5 ms |
8780 KB |
Output is correct |
11 |
Correct |
5 ms |
8780 KB |
Output is correct |
12 |
Correct |
5 ms |
8784 KB |
Output is correct |
13 |
Correct |
5 ms |
8772 KB |
Output is correct |
14 |
Correct |
5 ms |
8712 KB |
Output is correct |
15 |
Correct |
5 ms |
8788 KB |
Output is correct |
16 |
Correct |
5 ms |
8788 KB |
Output is correct |
17 |
Correct |
5 ms |
8788 KB |
Output is correct |
18 |
Correct |
6 ms |
8776 KB |
Output is correct |
19 |
Correct |
5 ms |
8796 KB |
Output is correct |
20 |
Correct |
6 ms |
8780 KB |
Output is correct |
21 |
Correct |
5 ms |
8788 KB |
Output is correct |
22 |
Correct |
6 ms |
8916 KB |
Output is correct |
23 |
Correct |
5 ms |
8784 KB |
Output is correct |
24 |
Correct |
5 ms |
8916 KB |
Output is correct |
25 |
Correct |
7 ms |
9040 KB |
Output is correct |
26 |
Correct |
6 ms |
9032 KB |
Output is correct |
27 |
Correct |
5 ms |
8788 KB |
Output is correct |
28 |
Correct |
6 ms |
9044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8532 KB |
Output is correct |
2 |
Correct |
4 ms |
8532 KB |
Output is correct |
3 |
Correct |
4 ms |
8532 KB |
Output is correct |
4 |
Correct |
5 ms |
8532 KB |
Output is correct |
5 |
Correct |
5 ms |
8640 KB |
Output is correct |
6 |
Correct |
5 ms |
8796 KB |
Output is correct |
7 |
Correct |
7 ms |
8788 KB |
Output is correct |
8 |
Correct |
5 ms |
8796 KB |
Output is correct |
9 |
Correct |
5 ms |
8780 KB |
Output is correct |
10 |
Correct |
93 ms |
29776 KB |
Output is correct |
11 |
Correct |
115 ms |
34744 KB |
Output is correct |
12 |
Correct |
114 ms |
34200 KB |
Output is correct |
13 |
Correct |
125 ms |
35308 KB |
Output is correct |
14 |
Correct |
116 ms |
38732 KB |
Output is correct |
15 |
Correct |
5 ms |
8780 KB |
Output is correct |
16 |
Correct |
5 ms |
8780 KB |
Output is correct |
17 |
Correct |
5 ms |
8784 KB |
Output is correct |
18 |
Correct |
5 ms |
8772 KB |
Output is correct |
19 |
Correct |
5 ms |
8712 KB |
Output is correct |
20 |
Correct |
5 ms |
8788 KB |
Output is correct |
21 |
Correct |
5 ms |
8788 KB |
Output is correct |
22 |
Correct |
5 ms |
8788 KB |
Output is correct |
23 |
Correct |
6 ms |
8776 KB |
Output is correct |
24 |
Correct |
5 ms |
8796 KB |
Output is correct |
25 |
Correct |
6 ms |
8780 KB |
Output is correct |
26 |
Correct |
5 ms |
8788 KB |
Output is correct |
27 |
Correct |
6 ms |
8916 KB |
Output is correct |
28 |
Correct |
5 ms |
8784 KB |
Output is correct |
29 |
Correct |
5 ms |
8916 KB |
Output is correct |
30 |
Correct |
7 ms |
9040 KB |
Output is correct |
31 |
Correct |
6 ms |
9032 KB |
Output is correct |
32 |
Correct |
5 ms |
8788 KB |
Output is correct |
33 |
Correct |
6 ms |
9044 KB |
Output is correct |
34 |
Correct |
17 ms |
12372 KB |
Output is correct |
35 |
Correct |
122 ms |
36684 KB |
Output is correct |
36 |
Correct |
110 ms |
36708 KB |
Output is correct |
37 |
Correct |
116 ms |
36552 KB |
Output is correct |
38 |
Correct |
125 ms |
36232 KB |
Output is correct |
39 |
Correct |
110 ms |
36180 KB |
Output is correct |
40 |
Correct |
103 ms |
33876 KB |
Output is correct |
41 |
Correct |
128 ms |
37000 KB |
Output is correct |
42 |
Correct |
128 ms |
36728 KB |
Output is correct |
43 |
Correct |
108 ms |
40744 KB |
Output is correct |
44 |
Correct |
118 ms |
36940 KB |
Output is correct |
45 |
Correct |
158 ms |
49520 KB |
Output is correct |
46 |
Correct |
129 ms |
36736 KB |
Output is correct |
47 |
Correct |
119 ms |
36832 KB |
Output is correct |
48 |
Correct |
130 ms |
40828 KB |
Output is correct |
49 |
Correct |
123 ms |
47308 KB |
Output is correct |
50 |
Correct |
97 ms |
38716 KB |
Output is correct |
51 |
Correct |
145 ms |
43944 KB |
Output is correct |
52 |
Correct |
179 ms |
52252 KB |
Output is correct |
53 |
Correct |
209 ms |
55268 KB |
Output is correct |
54 |
Correct |
223 ms |
61632 KB |
Output is correct |
55 |
Correct |
114 ms |
40464 KB |
Output is correct |
56 |
Correct |
189 ms |
56848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8532 KB |
Output is correct |
2 |
Correct |
4 ms |
8532 KB |
Output is correct |
3 |
Correct |
5 ms |
8532 KB |
Output is correct |
4 |
Correct |
5 ms |
8640 KB |
Output is correct |
5 |
Correct |
5 ms |
8796 KB |
Output is correct |
6 |
Correct |
7 ms |
8788 KB |
Output is correct |
7 |
Correct |
5 ms |
8796 KB |
Output is correct |
8 |
Correct |
5 ms |
8780 KB |
Output is correct |
9 |
Correct |
93 ms |
29776 KB |
Output is correct |
10 |
Correct |
115 ms |
34744 KB |
Output is correct |
11 |
Correct |
114 ms |
34200 KB |
Output is correct |
12 |
Correct |
125 ms |
35308 KB |
Output is correct |
13 |
Correct |
116 ms |
38732 KB |
Output is correct |
14 |
Correct |
99 ms |
29828 KB |
Output is correct |
15 |
Correct |
213 ms |
36528 KB |
Output is correct |
16 |
Correct |
224 ms |
36028 KB |
Output is correct |
17 |
Correct |
221 ms |
37368 KB |
Output is correct |
18 |
Correct |
287 ms |
40500 KB |
Output is correct |
19 |
Correct |
276 ms |
46132 KB |
Output is correct |
20 |
Correct |
287 ms |
47736 KB |
Output is correct |
21 |
Correct |
305 ms |
46968 KB |
Output is correct |
22 |
Correct |
271 ms |
47556 KB |
Output is correct |
23 |
Correct |
299 ms |
47400 KB |
Output is correct |
24 |
Correct |
264 ms |
45996 KB |
Output is correct |
25 |
Correct |
269 ms |
47180 KB |
Output is correct |
26 |
Correct |
265 ms |
45716 KB |
Output is correct |
27 |
Correct |
5 ms |
8780 KB |
Output is correct |
28 |
Correct |
5 ms |
8780 KB |
Output is correct |
29 |
Correct |
5 ms |
8784 KB |
Output is correct |
30 |
Correct |
5 ms |
8772 KB |
Output is correct |
31 |
Correct |
5 ms |
8712 KB |
Output is correct |
32 |
Correct |
5 ms |
8788 KB |
Output is correct |
33 |
Correct |
5 ms |
8788 KB |
Output is correct |
34 |
Correct |
5 ms |
8788 KB |
Output is correct |
35 |
Correct |
6 ms |
8776 KB |
Output is correct |
36 |
Correct |
17 ms |
12372 KB |
Output is correct |
37 |
Correct |
122 ms |
36684 KB |
Output is correct |
38 |
Correct |
110 ms |
36708 KB |
Output is correct |
39 |
Correct |
116 ms |
36552 KB |
Output is correct |
40 |
Correct |
125 ms |
36232 KB |
Output is correct |
41 |
Correct |
110 ms |
36180 KB |
Output is correct |
42 |
Correct |
103 ms |
33876 KB |
Output is correct |
43 |
Correct |
128 ms |
37000 KB |
Output is correct |
44 |
Correct |
128 ms |
36728 KB |
Output is correct |
45 |
Correct |
108 ms |
40744 KB |
Output is correct |
46 |
Correct |
118 ms |
36940 KB |
Output is correct |
47 |
Correct |
22 ms |
12544 KB |
Output is correct |
48 |
Correct |
251 ms |
41472 KB |
Output is correct |
49 |
Correct |
239 ms |
41592 KB |
Output is correct |
50 |
Correct |
243 ms |
41392 KB |
Output is correct |
51 |
Correct |
235 ms |
41264 KB |
Output is correct |
52 |
Correct |
229 ms |
39676 KB |
Output is correct |
53 |
Correct |
186 ms |
33176 KB |
Output is correct |
54 |
Correct |
278 ms |
42384 KB |
Output is correct |
55 |
Correct |
254 ms |
41524 KB |
Output is correct |
56 |
Correct |
321 ms |
44844 KB |
Output is correct |
57 |
Correct |
263 ms |
42600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8532 KB |
Output is correct |
2 |
Correct |
4 ms |
8532 KB |
Output is correct |
3 |
Correct |
4 ms |
8532 KB |
Output is correct |
4 |
Correct |
5 ms |
8532 KB |
Output is correct |
5 |
Correct |
5 ms |
8640 KB |
Output is correct |
6 |
Correct |
5 ms |
8796 KB |
Output is correct |
7 |
Correct |
7 ms |
8788 KB |
Output is correct |
8 |
Correct |
5 ms |
8796 KB |
Output is correct |
9 |
Correct |
5 ms |
8780 KB |
Output is correct |
10 |
Correct |
93 ms |
29776 KB |
Output is correct |
11 |
Correct |
115 ms |
34744 KB |
Output is correct |
12 |
Correct |
114 ms |
34200 KB |
Output is correct |
13 |
Correct |
125 ms |
35308 KB |
Output is correct |
14 |
Correct |
116 ms |
38732 KB |
Output is correct |
15 |
Correct |
99 ms |
29828 KB |
Output is correct |
16 |
Correct |
213 ms |
36528 KB |
Output is correct |
17 |
Correct |
224 ms |
36028 KB |
Output is correct |
18 |
Correct |
221 ms |
37368 KB |
Output is correct |
19 |
Correct |
287 ms |
40500 KB |
Output is correct |
20 |
Correct |
276 ms |
46132 KB |
Output is correct |
21 |
Correct |
287 ms |
47736 KB |
Output is correct |
22 |
Correct |
305 ms |
46968 KB |
Output is correct |
23 |
Correct |
271 ms |
47556 KB |
Output is correct |
24 |
Correct |
299 ms |
47400 KB |
Output is correct |
25 |
Correct |
264 ms |
45996 KB |
Output is correct |
26 |
Correct |
269 ms |
47180 KB |
Output is correct |
27 |
Correct |
265 ms |
45716 KB |
Output is correct |
28 |
Correct |
5 ms |
8780 KB |
Output is correct |
29 |
Correct |
5 ms |
8780 KB |
Output is correct |
30 |
Correct |
5 ms |
8784 KB |
Output is correct |
31 |
Correct |
5 ms |
8772 KB |
Output is correct |
32 |
Correct |
5 ms |
8712 KB |
Output is correct |
33 |
Correct |
5 ms |
8788 KB |
Output is correct |
34 |
Correct |
5 ms |
8788 KB |
Output is correct |
35 |
Correct |
5 ms |
8788 KB |
Output is correct |
36 |
Correct |
6 ms |
8776 KB |
Output is correct |
37 |
Correct |
17 ms |
12372 KB |
Output is correct |
38 |
Correct |
122 ms |
36684 KB |
Output is correct |
39 |
Correct |
110 ms |
36708 KB |
Output is correct |
40 |
Correct |
116 ms |
36552 KB |
Output is correct |
41 |
Correct |
125 ms |
36232 KB |
Output is correct |
42 |
Correct |
110 ms |
36180 KB |
Output is correct |
43 |
Correct |
103 ms |
33876 KB |
Output is correct |
44 |
Correct |
128 ms |
37000 KB |
Output is correct |
45 |
Correct |
128 ms |
36728 KB |
Output is correct |
46 |
Correct |
108 ms |
40744 KB |
Output is correct |
47 |
Correct |
118 ms |
36940 KB |
Output is correct |
48 |
Correct |
22 ms |
12544 KB |
Output is correct |
49 |
Correct |
251 ms |
41472 KB |
Output is correct |
50 |
Correct |
239 ms |
41592 KB |
Output is correct |
51 |
Correct |
243 ms |
41392 KB |
Output is correct |
52 |
Correct |
235 ms |
41264 KB |
Output is correct |
53 |
Correct |
229 ms |
39676 KB |
Output is correct |
54 |
Correct |
186 ms |
33176 KB |
Output is correct |
55 |
Correct |
278 ms |
42384 KB |
Output is correct |
56 |
Correct |
254 ms |
41524 KB |
Output is correct |
57 |
Correct |
321 ms |
44844 KB |
Output is correct |
58 |
Correct |
263 ms |
42600 KB |
Output is correct |
59 |
Correct |
87 ms |
16724 KB |
Output is correct |
60 |
Correct |
210 ms |
37848 KB |
Output is correct |
61 |
Correct |
226 ms |
37212 KB |
Output is correct |
62 |
Correct |
232 ms |
38864 KB |
Output is correct |
63 |
Correct |
305 ms |
42048 KB |
Output is correct |
64 |
Correct |
5 ms |
8796 KB |
Output is correct |
65 |
Correct |
6 ms |
8780 KB |
Output is correct |
66 |
Correct |
5 ms |
8788 KB |
Output is correct |
67 |
Correct |
6 ms |
8916 KB |
Output is correct |
68 |
Correct |
5 ms |
8784 KB |
Output is correct |
69 |
Correct |
5 ms |
8916 KB |
Output is correct |
70 |
Correct |
7 ms |
9040 KB |
Output is correct |
71 |
Correct |
6 ms |
9032 KB |
Output is correct |
72 |
Correct |
5 ms |
8788 KB |
Output is correct |
73 |
Correct |
6 ms |
9044 KB |
Output is correct |
74 |
Correct |
158 ms |
49520 KB |
Output is correct |
75 |
Correct |
129 ms |
36736 KB |
Output is correct |
76 |
Correct |
119 ms |
36832 KB |
Output is correct |
77 |
Correct |
130 ms |
40828 KB |
Output is correct |
78 |
Correct |
123 ms |
47308 KB |
Output is correct |
79 |
Correct |
97 ms |
38716 KB |
Output is correct |
80 |
Correct |
145 ms |
43944 KB |
Output is correct |
81 |
Correct |
179 ms |
52252 KB |
Output is correct |
82 |
Correct |
209 ms |
55268 KB |
Output is correct |
83 |
Correct |
223 ms |
61632 KB |
Output is correct |
84 |
Correct |
114 ms |
40464 KB |
Output is correct |
85 |
Correct |
189 ms |
56848 KB |
Output is correct |
86 |
Correct |
78 ms |
23724 KB |
Output is correct |
87 |
Correct |
236 ms |
41544 KB |
Output is correct |
88 |
Correct |
238 ms |
41488 KB |
Output is correct |
89 |
Correct |
279 ms |
44208 KB |
Output is correct |
90 |
Correct |
237 ms |
53528 KB |
Output is correct |
91 |
Correct |
229 ms |
50364 KB |
Output is correct |
92 |
Correct |
294 ms |
47656 KB |
Output is correct |
93 |
Correct |
300 ms |
56524 KB |
Output is correct |
94 |
Correct |
359 ms |
59680 KB |
Output is correct |
95 |
Correct |
353 ms |
65352 KB |
Output is correct |
96 |
Correct |
320 ms |
45176 KB |
Output is correct |
97 |
Correct |
339 ms |
52472 KB |
Output is correct |