#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
struct EDGE {
int u, v, w;
};
struct DSUNODE;
vector<DSUNODE> nodes;
struct DSUNODE {
int vert;
int parent;
int distToParent;
int size;
int left;
int right;
int when;
int leader(int mxpath = 2e9) {
if (parent == -1 || distToParent > mxpath) return vert;
return nodes[nodes[parent].leader(mxpath)].vert;
}
};
vector<EDGE> edges;
void init(int N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
edges = vector<EDGE>(M);
nodes = vector<DSUNODE>(N);
for (int i = 0; i < N; i++) {
DSUNODE& a = nodes[i];
a.parent = -1;
a.distToParent = 0;
a.size = 1;
a.vert = a.left = a.right = i;
a.when = 2e9;
}
for (int i = 0; i < M; i++) {
edges[i] = {U[i], V[i], W[i]};
}
sort(edges.begin(), edges.end(), [](const EDGE& a, const EDGE& b){return a.w < b.w;});
//cout << "WORKS" << endl;
for (int i = 0; i < M; i++) {
//cout << nodes[edges[i].u].leader() << " " << nodes[edges[i].v].leader() << endl;
DSUNODE& u = nodes[nodes[edges[i].u].leader()];
DSUNODE& v = nodes[nodes[edges[i].v].leader()];
if (u.vert == v.vert) {
if ((edges[i].u == u.left && edges[i].v == u.right) || (edges[i].u == u.right && edges[i].v == u.left)) {
u.left = -1;
u.right = -1;
u.when = min(u.when, edges[i].w);
}
continue;
}
int newleft = -1, newright = -1;
if (edges[i].u == u.left && edges[i].v == v.left) {
newleft = u.right;
newright = v.right;
} else if (edges[i].u == u.left && edges[i].v == v.right) {
newleft = u.right;
newright = v.left;
} else if (edges[i].u == u.right && edges[i].v == v.left) {
newleft = u.left;
newright = v.right;
} else if (edges[i].u == u.right && edges[i].v == v.right) {
newleft = u.left;
newright = v.left;
}
if (u.size > v.size) {
v.parent = u.vert;
v.distToParent = edges[i].w;
u.size += v.size;
u.left = newleft;
u.right = newright;
if (newleft == -1) {
u.when = min(u.when, edges[i].w);
}
} else {
u.parent = v.vert;
u.distToParent = edges[i].w;
v.size += u.size;
v.left = newleft;
v.right = newright;
if (newleft == -1) {
v.when = min(v.when, edges[i].w);
}
}
}
}
bool can(int X, int Y, int M) {
int u = nodes[X].leader(M), v = nodes[Y].leader(M);
return u == v && M >= nodes[u].when;
}
int getMinimumFuelCapacity(int X, int Y) {
if (!can(X, Y, 1e9)) return -1;
int l = 0, r = 1e9;
while (l + 1 < r) {
int m = (l + r) / 2;
if (can(X, Y, m)) r = m;
else l = m;
}
return r;
}
int mai2n() {
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
41 ms |
6840 KB |
Output is correct |
10 |
Correct |
42 ms |
8324 KB |
Output is correct |
11 |
Correct |
40 ms |
8220 KB |
Output is correct |
12 |
Correct |
47 ms |
8624 KB |
Output is correct |
13 |
Correct |
53 ms |
8628 KB |
Output is correct |
14 |
Correct |
40 ms |
7180 KB |
Output is correct |
15 |
Correct |
125 ms |
12448 KB |
Output is correct |
16 |
Correct |
121 ms |
12204 KB |
Output is correct |
17 |
Correct |
126 ms |
12648 KB |
Output is correct |
18 |
Correct |
100 ms |
12704 KB |
Output is correct |
19 |
Correct |
209 ms |
6620 KB |
Output is correct |
20 |
Correct |
334 ms |
13508 KB |
Output is correct |
21 |
Correct |
356 ms |
13632 KB |
Output is correct |
22 |
Correct |
352 ms |
14132 KB |
Output is correct |
23 |
Correct |
146 ms |
14076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
139 ms |
12964 KB |
Output is correct |
4 |
Correct |
144 ms |
13236 KB |
Output is correct |
5 |
Correct |
152 ms |
13576 KB |
Output is correct |
6 |
Correct |
145 ms |
13100 KB |
Output is correct |
7 |
Correct |
150 ms |
13452 KB |
Output is correct |
8 |
Correct |
166 ms |
13052 KB |
Output is correct |
9 |
Correct |
147 ms |
13220 KB |
Output is correct |
10 |
Correct |
143 ms |
13064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
352 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
312 KB |
Output is correct |
18 |
Correct |
1 ms |
312 KB |
Output is correct |
19 |
Correct |
1 ms |
316 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
25 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
41 ms |
6840 KB |
Output is correct |
11 |
Correct |
42 ms |
8324 KB |
Output is correct |
12 |
Correct |
40 ms |
8220 KB |
Output is correct |
13 |
Correct |
47 ms |
8624 KB |
Output is correct |
14 |
Correct |
53 ms |
8628 KB |
Output is correct |
15 |
Correct |
1 ms |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
352 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
312 KB |
Output is correct |
23 |
Correct |
1 ms |
312 KB |
Output is correct |
24 |
Correct |
1 ms |
316 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
41 ms |
6840 KB |
Output is correct |
10 |
Correct |
42 ms |
8324 KB |
Output is correct |
11 |
Correct |
40 ms |
8220 KB |
Output is correct |
12 |
Correct |
47 ms |
8624 KB |
Output is correct |
13 |
Correct |
53 ms |
8628 KB |
Output is correct |
14 |
Correct |
40 ms |
7180 KB |
Output is correct |
15 |
Correct |
125 ms |
12448 KB |
Output is correct |
16 |
Correct |
121 ms |
12204 KB |
Output is correct |
17 |
Correct |
126 ms |
12648 KB |
Output is correct |
18 |
Correct |
100 ms |
12704 KB |
Output is correct |
19 |
Correct |
139 ms |
12964 KB |
Output is correct |
20 |
Correct |
144 ms |
13236 KB |
Output is correct |
21 |
Correct |
152 ms |
13576 KB |
Output is correct |
22 |
Correct |
145 ms |
13100 KB |
Output is correct |
23 |
Correct |
150 ms |
13452 KB |
Output is correct |
24 |
Correct |
166 ms |
13052 KB |
Output is correct |
25 |
Correct |
147 ms |
13220 KB |
Output is correct |
26 |
Correct |
143 ms |
13064 KB |
Output is correct |
27 |
Correct |
1 ms |
328 KB |
Output is correct |
28 |
Correct |
1 ms |
352 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
312 KB |
Output is correct |
35 |
Correct |
1 ms |
312 KB |
Output is correct |
36 |
Correct |
9 ms |
1364 KB |
Output is correct |
37 |
Correct |
42 ms |
8364 KB |
Output is correct |
38 |
Correct |
47 ms |
8364 KB |
Output is correct |
39 |
Correct |
44 ms |
8376 KB |
Output is correct |
40 |
Correct |
43 ms |
8236 KB |
Output is correct |
41 |
Correct |
43 ms |
8228 KB |
Output is correct |
42 |
Correct |
54 ms |
7556 KB |
Output is correct |
43 |
Correct |
48 ms |
8628 KB |
Output is correct |
44 |
Correct |
42 ms |
8388 KB |
Output is correct |
45 |
Correct |
45 ms |
8376 KB |
Output is correct |
46 |
Correct |
48 ms |
8652 KB |
Output is correct |
47 |
Correct |
33 ms |
1804 KB |
Output is correct |
48 |
Correct |
360 ms |
13236 KB |
Output is correct |
49 |
Correct |
371 ms |
13272 KB |
Output is correct |
50 |
Correct |
361 ms |
13128 KB |
Output is correct |
51 |
Correct |
382 ms |
13092 KB |
Output is correct |
52 |
Correct |
323 ms |
12684 KB |
Output is correct |
53 |
Correct |
257 ms |
11676 KB |
Output is correct |
54 |
Correct |
368 ms |
14128 KB |
Output is correct |
55 |
Correct |
444 ms |
13244 KB |
Output is correct |
56 |
Correct |
175 ms |
13204 KB |
Output is correct |
57 |
Correct |
285 ms |
14112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
41 ms |
6840 KB |
Output is correct |
11 |
Correct |
42 ms |
8324 KB |
Output is correct |
12 |
Correct |
40 ms |
8220 KB |
Output is correct |
13 |
Correct |
47 ms |
8624 KB |
Output is correct |
14 |
Correct |
53 ms |
8628 KB |
Output is correct |
15 |
Correct |
40 ms |
7180 KB |
Output is correct |
16 |
Correct |
125 ms |
12448 KB |
Output is correct |
17 |
Correct |
121 ms |
12204 KB |
Output is correct |
18 |
Correct |
126 ms |
12648 KB |
Output is correct |
19 |
Correct |
100 ms |
12704 KB |
Output is correct |
20 |
Correct |
139 ms |
12964 KB |
Output is correct |
21 |
Correct |
144 ms |
13236 KB |
Output is correct |
22 |
Correct |
152 ms |
13576 KB |
Output is correct |
23 |
Correct |
145 ms |
13100 KB |
Output is correct |
24 |
Correct |
150 ms |
13452 KB |
Output is correct |
25 |
Correct |
166 ms |
13052 KB |
Output is correct |
26 |
Correct |
147 ms |
13220 KB |
Output is correct |
27 |
Correct |
143 ms |
13064 KB |
Output is correct |
28 |
Correct |
1 ms |
328 KB |
Output is correct |
29 |
Correct |
1 ms |
352 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
312 KB |
Output is correct |
36 |
Correct |
1 ms |
312 KB |
Output is correct |
37 |
Correct |
9 ms |
1364 KB |
Output is correct |
38 |
Correct |
42 ms |
8364 KB |
Output is correct |
39 |
Correct |
47 ms |
8364 KB |
Output is correct |
40 |
Correct |
44 ms |
8376 KB |
Output is correct |
41 |
Correct |
43 ms |
8236 KB |
Output is correct |
42 |
Correct |
43 ms |
8228 KB |
Output is correct |
43 |
Correct |
54 ms |
7556 KB |
Output is correct |
44 |
Correct |
48 ms |
8628 KB |
Output is correct |
45 |
Correct |
42 ms |
8388 KB |
Output is correct |
46 |
Correct |
45 ms |
8376 KB |
Output is correct |
47 |
Correct |
48 ms |
8652 KB |
Output is correct |
48 |
Correct |
33 ms |
1804 KB |
Output is correct |
49 |
Correct |
360 ms |
13236 KB |
Output is correct |
50 |
Correct |
371 ms |
13272 KB |
Output is correct |
51 |
Correct |
361 ms |
13128 KB |
Output is correct |
52 |
Correct |
382 ms |
13092 KB |
Output is correct |
53 |
Correct |
323 ms |
12684 KB |
Output is correct |
54 |
Correct |
257 ms |
11676 KB |
Output is correct |
55 |
Correct |
368 ms |
14128 KB |
Output is correct |
56 |
Correct |
444 ms |
13244 KB |
Output is correct |
57 |
Correct |
175 ms |
13204 KB |
Output is correct |
58 |
Correct |
285 ms |
14112 KB |
Output is correct |
59 |
Correct |
209 ms |
6620 KB |
Output is correct |
60 |
Correct |
334 ms |
13508 KB |
Output is correct |
61 |
Correct |
356 ms |
13632 KB |
Output is correct |
62 |
Correct |
352 ms |
14132 KB |
Output is correct |
63 |
Correct |
146 ms |
14076 KB |
Output is correct |
64 |
Correct |
1 ms |
316 KB |
Output is correct |
65 |
Correct |
1 ms |
340 KB |
Output is correct |
66 |
Correct |
1 ms |
340 KB |
Output is correct |
67 |
Correct |
1 ms |
340 KB |
Output is correct |
68 |
Correct |
1 ms |
340 KB |
Output is correct |
69 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
70 |
Halted |
0 ms |
0 KB |
- |