#include<bits/stdc++.h>
#ifndef LOCAL
#include "swap.h"
#endif // LOCAL
using namespace std;
#define pb push_back
#define ld long double
#define ll long long
mt19937 rnd(51);
const int N = 3e5 + 10;
vector<int> g[N];
bool good[N];
int par[N], sz[N], d[N], po[20][N], val[N];
int find_set(int v) {
if (v == par[v]) return v;
else return par[v] = find_set(par[v]);
}
void union_set(int a, int b) {
if (sz[a] < sz[b]) {
swap(a, b);
}
par[b] = a;
sz[a] += sz[b];
good[a] |= good[b];
}
void dfs(int v, int p) {
po[0][v] = p;
for (auto to : g[v]) {
if (to != p) {
d[to] = d[v] + 1;
dfs(to, v);
}
}
}
void init(int n, int m, vector<int> u, vector<int> v, vector<int> w) {
for (int i = 0; i < N; i++) {
par[i] = i;
sz[i] = 1;
}
vector<int> ord(m), ind(n), cnt(n);
iota(ind.begin(), ind.end(), 0);
iota(ord.begin(), ord.end(), 0);
sort(ord.begin(), ord.end(), [&](int i, int j){return w[i] < w[j];});
int now = n;
for (auto i : ord) {
int a = find_set(u[i]), b = find_set(v[i]);
val[now] = w[i];
if (a == b) {
po[0][ind[a]] = now;
g[now].pb(ind[a]);
good[a] = good[now] = 1;
ind[a] = now++;
} else {
po[0][ind[a]] = now;
po[0][ind[b]] = now;
g[now].pb(ind[a]);
g[now].pb(ind[b]);
union_set(a, b);
cnt[u[i]]++, cnt[v[i]]++;
a = find_set(a);
good[a] |= max(cnt[u[i]], cnt[v[i]]) >= 3;
good[now] = good[a];
ind[a] = now++;
}
}
dfs(now - 1, now - 1);
for (int i = 1; i < 20; i++) {
for (int j = 0; j < now; j++) {
po[i][j] = po[i - 1][po[i - 1][j]];
}
}
}
int lca(int a, int b) {
if (d[a] < d[b]) swap(a, b);
for (int i = 19; i >= 0; i--) {
if (d[po[i][a]] >= d[b]) {
a = po[i][a];
}
}
if (a == b) return a;
for (int i = 19; i >= 0; i--) {
if (po[i][a] != po[i][b]) {
a = po[i][a];
b = po[i][b];
}
}
return po[0][a];
}
int getMinimumFuelCapacity(int a, int b) {
int l = lca(a, b);
if (!good[l]) {
for (int i = 19; i >= 0; i--) {
if (!good[po[i][l]]) {
l = po[i][l];
}
}
if (d[l] > 0) {
l = po[0][l];
}
if (good[l]) {
return val[l];
} else {
return -1;
}
} else {
return val[l];
}
}
#ifdef LOCAL
signed main() {
int N, M;
assert(2 == scanf("%d %d", &N, &M));
std::vector<int> U(M), V(M), W(M);
for (int i = 0; i < M; ++i) {
assert(3 == scanf("%d %d %d", &U[i], &V[i], &W[i]));
}
int Q;
assert(1 == scanf("%d", &Q));
std::vector<int> X(Q), Y(Q);
for (int i = 0; i < Q; ++i) {
assert(2 == scanf("%d %d", &X[i], &Y[i]));
}
init(N, M, U, V, W);
std::vector<int> minimum_fuel_capacities(Q);
for (int i = 0; i < Q; ++i) {
minimum_fuel_capacities[i] = getMinimumFuelCapacity(X[i], Y[i]);
}
for (int i = 0; i < Q; ++i) {
printf("%d\n", minimum_fuel_capacities[i]);
}
return 0;
}
#endif
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9812 KB |
Output is correct |
2 |
Correct |
6 ms |
9812 KB |
Output is correct |
3 |
Correct |
6 ms |
9812 KB |
Output is correct |
4 |
Correct |
6 ms |
9812 KB |
Output is correct |
5 |
Correct |
6 ms |
10024 KB |
Output is correct |
6 |
Correct |
7 ms |
9940 KB |
Output is correct |
7 |
Correct |
6 ms |
10068 KB |
Output is correct |
8 |
Correct |
6 ms |
10064 KB |
Output is correct |
9 |
Correct |
72 ms |
30028 KB |
Output is correct |
10 |
Correct |
107 ms |
34440 KB |
Output is correct |
11 |
Correct |
107 ms |
34148 KB |
Output is correct |
12 |
Correct |
85 ms |
35532 KB |
Output is correct |
13 |
Correct |
82 ms |
38596 KB |
Output is correct |
14 |
Correct |
108 ms |
30260 KB |
Output is correct |
15 |
Correct |
492 ms |
38556 KB |
Output is correct |
16 |
Correct |
515 ms |
37756 KB |
Output is correct |
17 |
Correct |
492 ms |
39372 KB |
Output is correct |
18 |
Correct |
479 ms |
42440 KB |
Output is correct |
19 |
Correct |
146 ms |
18892 KB |
Output is correct |
20 |
Correct |
454 ms |
38744 KB |
Output is correct |
21 |
Correct |
486 ms |
38192 KB |
Output is correct |
22 |
Correct |
507 ms |
39836 KB |
Output is correct |
23 |
Correct |
564 ms |
42864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9812 KB |
Output is correct |
2 |
Correct |
6 ms |
9812 KB |
Output is correct |
3 |
Correct |
458 ms |
43664 KB |
Output is correct |
4 |
Correct |
490 ms |
45192 KB |
Output is correct |
5 |
Correct |
496 ms |
44484 KB |
Output is correct |
6 |
Correct |
445 ms |
44900 KB |
Output is correct |
7 |
Correct |
463 ms |
44952 KB |
Output is correct |
8 |
Correct |
488 ms |
43688 KB |
Output is correct |
9 |
Correct |
483 ms |
44684 KB |
Output is correct |
10 |
Correct |
433 ms |
43368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9812 KB |
Output is correct |
2 |
Correct |
6 ms |
9812 KB |
Output is correct |
3 |
Correct |
6 ms |
9812 KB |
Output is correct |
4 |
Correct |
6 ms |
9812 KB |
Output is correct |
5 |
Correct |
6 ms |
10024 KB |
Output is correct |
6 |
Correct |
7 ms |
9940 KB |
Output is correct |
7 |
Correct |
6 ms |
10068 KB |
Output is correct |
8 |
Correct |
6 ms |
10064 KB |
Output is correct |
9 |
Correct |
5 ms |
9812 KB |
Output is correct |
10 |
Correct |
6 ms |
10068 KB |
Output is correct |
11 |
Correct |
7 ms |
10060 KB |
Output is correct |
12 |
Correct |
8 ms |
10068 KB |
Output is correct |
13 |
Correct |
6 ms |
9924 KB |
Output is correct |
14 |
Correct |
6 ms |
9940 KB |
Output is correct |
15 |
Correct |
6 ms |
9976 KB |
Output is correct |
16 |
Correct |
7 ms |
10060 KB |
Output is correct |
17 |
Correct |
6 ms |
10068 KB |
Output is correct |
18 |
Correct |
6 ms |
10064 KB |
Output is correct |
19 |
Correct |
6 ms |
10044 KB |
Output is correct |
20 |
Correct |
6 ms |
10052 KB |
Output is correct |
21 |
Correct |
6 ms |
10068 KB |
Output is correct |
22 |
Correct |
7 ms |
10220 KB |
Output is correct |
23 |
Correct |
7 ms |
9940 KB |
Output is correct |
24 |
Correct |
6 ms |
10196 KB |
Output is correct |
25 |
Correct |
7 ms |
10196 KB |
Output is correct |
26 |
Correct |
7 ms |
10196 KB |
Output is correct |
27 |
Correct |
8 ms |
10132 KB |
Output is correct |
28 |
Correct |
7 ms |
10188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Correct |
6 ms |
9812 KB |
Output is correct |
3 |
Correct |
6 ms |
9812 KB |
Output is correct |
4 |
Correct |
6 ms |
9812 KB |
Output is correct |
5 |
Correct |
6 ms |
9812 KB |
Output is correct |
6 |
Correct |
6 ms |
10024 KB |
Output is correct |
7 |
Correct |
7 ms |
9940 KB |
Output is correct |
8 |
Correct |
6 ms |
10068 KB |
Output is correct |
9 |
Correct |
6 ms |
10064 KB |
Output is correct |
10 |
Correct |
72 ms |
30028 KB |
Output is correct |
11 |
Correct |
107 ms |
34440 KB |
Output is correct |
12 |
Correct |
107 ms |
34148 KB |
Output is correct |
13 |
Correct |
85 ms |
35532 KB |
Output is correct |
14 |
Correct |
82 ms |
38596 KB |
Output is correct |
15 |
Correct |
6 ms |
10068 KB |
Output is correct |
16 |
Correct |
7 ms |
10060 KB |
Output is correct |
17 |
Correct |
8 ms |
10068 KB |
Output is correct |
18 |
Correct |
6 ms |
9924 KB |
Output is correct |
19 |
Correct |
6 ms |
9940 KB |
Output is correct |
20 |
Correct |
6 ms |
9976 KB |
Output is correct |
21 |
Correct |
7 ms |
10060 KB |
Output is correct |
22 |
Correct |
6 ms |
10068 KB |
Output is correct |
23 |
Correct |
6 ms |
10064 KB |
Output is correct |
24 |
Correct |
6 ms |
10044 KB |
Output is correct |
25 |
Correct |
6 ms |
10052 KB |
Output is correct |
26 |
Correct |
6 ms |
10068 KB |
Output is correct |
27 |
Correct |
7 ms |
10220 KB |
Output is correct |
28 |
Correct |
7 ms |
9940 KB |
Output is correct |
29 |
Correct |
6 ms |
10196 KB |
Output is correct |
30 |
Correct |
7 ms |
10196 KB |
Output is correct |
31 |
Correct |
7 ms |
10196 KB |
Output is correct |
32 |
Correct |
8 ms |
10132 KB |
Output is correct |
33 |
Correct |
7 ms |
10188 KB |
Output is correct |
34 |
Correct |
16 ms |
13260 KB |
Output is correct |
35 |
Correct |
95 ms |
35276 KB |
Output is correct |
36 |
Correct |
85 ms |
35272 KB |
Output is correct |
37 |
Correct |
98 ms |
35416 KB |
Output is correct |
38 |
Correct |
95 ms |
34856 KB |
Output is correct |
39 |
Correct |
99 ms |
34760 KB |
Output is correct |
40 |
Correct |
78 ms |
32684 KB |
Output is correct |
41 |
Correct |
87 ms |
35524 KB |
Output is correct |
42 |
Correct |
97 ms |
35276 KB |
Output is correct |
43 |
Correct |
92 ms |
39408 KB |
Output is correct |
44 |
Correct |
88 ms |
35572 KB |
Output is correct |
45 |
Correct |
117 ms |
47180 KB |
Output is correct |
46 |
Correct |
95 ms |
35276 KB |
Output is correct |
47 |
Correct |
97 ms |
35388 KB |
Output is correct |
48 |
Correct |
95 ms |
39252 KB |
Output is correct |
49 |
Correct |
97 ms |
45428 KB |
Output is correct |
50 |
Correct |
80 ms |
37520 KB |
Output is correct |
51 |
Correct |
100 ms |
42216 KB |
Output is correct |
52 |
Correct |
130 ms |
49692 KB |
Output is correct |
53 |
Correct |
150 ms |
52312 KB |
Output is correct |
54 |
Correct |
163 ms |
58060 KB |
Output is correct |
55 |
Correct |
77 ms |
38988 KB |
Output is correct |
56 |
Correct |
133 ms |
53904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9812 KB |
Output is correct |
2 |
Correct |
6 ms |
9812 KB |
Output is correct |
3 |
Correct |
6 ms |
9812 KB |
Output is correct |
4 |
Correct |
6 ms |
9812 KB |
Output is correct |
5 |
Correct |
6 ms |
10024 KB |
Output is correct |
6 |
Correct |
7 ms |
9940 KB |
Output is correct |
7 |
Correct |
6 ms |
10068 KB |
Output is correct |
8 |
Correct |
6 ms |
10064 KB |
Output is correct |
9 |
Correct |
72 ms |
30028 KB |
Output is correct |
10 |
Correct |
107 ms |
34440 KB |
Output is correct |
11 |
Correct |
107 ms |
34148 KB |
Output is correct |
12 |
Correct |
85 ms |
35532 KB |
Output is correct |
13 |
Correct |
82 ms |
38596 KB |
Output is correct |
14 |
Correct |
108 ms |
30260 KB |
Output is correct |
15 |
Correct |
492 ms |
38556 KB |
Output is correct |
16 |
Correct |
515 ms |
37756 KB |
Output is correct |
17 |
Correct |
492 ms |
39372 KB |
Output is correct |
18 |
Correct |
479 ms |
42440 KB |
Output is correct |
19 |
Correct |
458 ms |
43664 KB |
Output is correct |
20 |
Correct |
490 ms |
45192 KB |
Output is correct |
21 |
Correct |
496 ms |
44484 KB |
Output is correct |
22 |
Correct |
445 ms |
44900 KB |
Output is correct |
23 |
Correct |
463 ms |
44952 KB |
Output is correct |
24 |
Correct |
488 ms |
43688 KB |
Output is correct |
25 |
Correct |
483 ms |
44684 KB |
Output is correct |
26 |
Correct |
433 ms |
43368 KB |
Output is correct |
27 |
Correct |
6 ms |
10068 KB |
Output is correct |
28 |
Correct |
7 ms |
10060 KB |
Output is correct |
29 |
Correct |
8 ms |
10068 KB |
Output is correct |
30 |
Correct |
6 ms |
9924 KB |
Output is correct |
31 |
Correct |
6 ms |
9940 KB |
Output is correct |
32 |
Correct |
6 ms |
9976 KB |
Output is correct |
33 |
Correct |
7 ms |
10060 KB |
Output is correct |
34 |
Correct |
6 ms |
10068 KB |
Output is correct |
35 |
Correct |
6 ms |
10064 KB |
Output is correct |
36 |
Correct |
16 ms |
13260 KB |
Output is correct |
37 |
Correct |
95 ms |
35276 KB |
Output is correct |
38 |
Correct |
85 ms |
35272 KB |
Output is correct |
39 |
Correct |
98 ms |
35416 KB |
Output is correct |
40 |
Correct |
95 ms |
34856 KB |
Output is correct |
41 |
Correct |
99 ms |
34760 KB |
Output is correct |
42 |
Correct |
78 ms |
32684 KB |
Output is correct |
43 |
Correct |
87 ms |
35524 KB |
Output is correct |
44 |
Correct |
97 ms |
35276 KB |
Output is correct |
45 |
Correct |
92 ms |
39408 KB |
Output is correct |
46 |
Correct |
88 ms |
35572 KB |
Output is correct |
47 |
Correct |
27 ms |
13468 KB |
Output is correct |
48 |
Correct |
485 ms |
39108 KB |
Output is correct |
49 |
Correct |
454 ms |
39228 KB |
Output is correct |
50 |
Correct |
356 ms |
39020 KB |
Output is correct |
51 |
Correct |
367 ms |
38872 KB |
Output is correct |
52 |
Correct |
323 ms |
37500 KB |
Output is correct |
53 |
Correct |
229 ms |
31908 KB |
Output is correct |
54 |
Correct |
463 ms |
39808 KB |
Output is correct |
55 |
Correct |
544 ms |
39112 KB |
Output is correct |
56 |
Correct |
491 ms |
42316 KB |
Output is correct |
57 |
Correct |
369 ms |
39900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Correct |
6 ms |
9812 KB |
Output is correct |
3 |
Correct |
6 ms |
9812 KB |
Output is correct |
4 |
Correct |
6 ms |
9812 KB |
Output is correct |
5 |
Correct |
6 ms |
9812 KB |
Output is correct |
6 |
Correct |
6 ms |
10024 KB |
Output is correct |
7 |
Correct |
7 ms |
9940 KB |
Output is correct |
8 |
Correct |
6 ms |
10068 KB |
Output is correct |
9 |
Correct |
6 ms |
10064 KB |
Output is correct |
10 |
Correct |
72 ms |
30028 KB |
Output is correct |
11 |
Correct |
107 ms |
34440 KB |
Output is correct |
12 |
Correct |
107 ms |
34148 KB |
Output is correct |
13 |
Correct |
85 ms |
35532 KB |
Output is correct |
14 |
Correct |
82 ms |
38596 KB |
Output is correct |
15 |
Correct |
108 ms |
30260 KB |
Output is correct |
16 |
Correct |
492 ms |
38556 KB |
Output is correct |
17 |
Correct |
515 ms |
37756 KB |
Output is correct |
18 |
Correct |
492 ms |
39372 KB |
Output is correct |
19 |
Correct |
479 ms |
42440 KB |
Output is correct |
20 |
Correct |
458 ms |
43664 KB |
Output is correct |
21 |
Correct |
490 ms |
45192 KB |
Output is correct |
22 |
Correct |
496 ms |
44484 KB |
Output is correct |
23 |
Correct |
445 ms |
44900 KB |
Output is correct |
24 |
Correct |
463 ms |
44952 KB |
Output is correct |
25 |
Correct |
488 ms |
43688 KB |
Output is correct |
26 |
Correct |
483 ms |
44684 KB |
Output is correct |
27 |
Correct |
433 ms |
43368 KB |
Output is correct |
28 |
Correct |
6 ms |
10068 KB |
Output is correct |
29 |
Correct |
7 ms |
10060 KB |
Output is correct |
30 |
Correct |
8 ms |
10068 KB |
Output is correct |
31 |
Correct |
6 ms |
9924 KB |
Output is correct |
32 |
Correct |
6 ms |
9940 KB |
Output is correct |
33 |
Correct |
6 ms |
9976 KB |
Output is correct |
34 |
Correct |
7 ms |
10060 KB |
Output is correct |
35 |
Correct |
6 ms |
10068 KB |
Output is correct |
36 |
Correct |
6 ms |
10064 KB |
Output is correct |
37 |
Correct |
16 ms |
13260 KB |
Output is correct |
38 |
Correct |
95 ms |
35276 KB |
Output is correct |
39 |
Correct |
85 ms |
35272 KB |
Output is correct |
40 |
Correct |
98 ms |
35416 KB |
Output is correct |
41 |
Correct |
95 ms |
34856 KB |
Output is correct |
42 |
Correct |
99 ms |
34760 KB |
Output is correct |
43 |
Correct |
78 ms |
32684 KB |
Output is correct |
44 |
Correct |
87 ms |
35524 KB |
Output is correct |
45 |
Correct |
97 ms |
35276 KB |
Output is correct |
46 |
Correct |
92 ms |
39408 KB |
Output is correct |
47 |
Correct |
88 ms |
35572 KB |
Output is correct |
48 |
Correct |
27 ms |
13468 KB |
Output is correct |
49 |
Correct |
485 ms |
39108 KB |
Output is correct |
50 |
Correct |
454 ms |
39228 KB |
Output is correct |
51 |
Correct |
356 ms |
39020 KB |
Output is correct |
52 |
Correct |
367 ms |
38872 KB |
Output is correct |
53 |
Correct |
323 ms |
37500 KB |
Output is correct |
54 |
Correct |
229 ms |
31908 KB |
Output is correct |
55 |
Correct |
463 ms |
39808 KB |
Output is correct |
56 |
Correct |
544 ms |
39112 KB |
Output is correct |
57 |
Correct |
491 ms |
42316 KB |
Output is correct |
58 |
Correct |
369 ms |
39900 KB |
Output is correct |
59 |
Correct |
146 ms |
18892 KB |
Output is correct |
60 |
Correct |
454 ms |
38744 KB |
Output is correct |
61 |
Correct |
486 ms |
38192 KB |
Output is correct |
62 |
Correct |
507 ms |
39836 KB |
Output is correct |
63 |
Correct |
564 ms |
42864 KB |
Output is correct |
64 |
Correct |
6 ms |
10044 KB |
Output is correct |
65 |
Correct |
6 ms |
10052 KB |
Output is correct |
66 |
Correct |
6 ms |
10068 KB |
Output is correct |
67 |
Correct |
7 ms |
10220 KB |
Output is correct |
68 |
Correct |
7 ms |
9940 KB |
Output is correct |
69 |
Correct |
6 ms |
10196 KB |
Output is correct |
70 |
Correct |
7 ms |
10196 KB |
Output is correct |
71 |
Correct |
7 ms |
10196 KB |
Output is correct |
72 |
Correct |
8 ms |
10132 KB |
Output is correct |
73 |
Correct |
7 ms |
10188 KB |
Output is correct |
74 |
Correct |
117 ms |
47180 KB |
Output is correct |
75 |
Correct |
95 ms |
35276 KB |
Output is correct |
76 |
Correct |
97 ms |
35388 KB |
Output is correct |
77 |
Correct |
95 ms |
39252 KB |
Output is correct |
78 |
Correct |
97 ms |
45428 KB |
Output is correct |
79 |
Correct |
80 ms |
37520 KB |
Output is correct |
80 |
Correct |
100 ms |
42216 KB |
Output is correct |
81 |
Correct |
130 ms |
49692 KB |
Output is correct |
82 |
Correct |
150 ms |
52312 KB |
Output is correct |
83 |
Correct |
163 ms |
58060 KB |
Output is correct |
84 |
Correct |
77 ms |
38988 KB |
Output is correct |
85 |
Correct |
133 ms |
53904 KB |
Output is correct |
86 |
Correct |
102 ms |
23628 KB |
Output is correct |
87 |
Correct |
428 ms |
39136 KB |
Output is correct |
88 |
Correct |
347 ms |
39136 KB |
Output is correct |
89 |
Correct |
389 ms |
41808 KB |
Output is correct |
90 |
Correct |
225 ms |
50888 KB |
Output is correct |
91 |
Correct |
248 ms |
48008 KB |
Output is correct |
92 |
Correct |
431 ms |
44996 KB |
Output is correct |
93 |
Correct |
394 ms |
52776 KB |
Output is correct |
94 |
Correct |
556 ms |
56004 KB |
Output is correct |
95 |
Correct |
596 ms |
61924 KB |
Output is correct |
96 |
Correct |
503 ms |
42660 KB |
Output is correct |
97 |
Correct |
491 ms |
49264 KB |
Output is correct |