#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll inf = 1e9 + 37;
vector<vector<int> > g;
struct Edge
{
int from, to, c;
Edge() { }
Edge(int u, int v, int w) : from(u), to(v), c(w) {
}
bool operator<(const Edge& other) const {
return c < other.c;
}
};
struct Node
{
bool is_line = true;
array<int, 2> endpoints;
int par, size, w;
Node(int u) : endpoints({u, u}), par(u), size(1), w(inf) {
}
};
vector<Node> dsu;
vector<Edge> edges;
int idx = -1;
int get_root(int v) { // currently slow
return (dsu[v].par == v ? v : dsu[v].par = get_root(dsu[v].par));
}
void merge(int u, int v, int c) {
int paru = get_root(u), parv = get_root(v);
if(paru == parv) {
dsu[paru].is_line = false;
dsu[paru].w = min(dsu[paru].w, c);
return;
}
if(dsu[paru].size > dsu[parv].size) {
swap(u, v);
swap(paru, parv);
}
int cur_node = dsu.size();
dsu.emplace_back(Node(cur_node));
if(find(dsu[paru].endpoints.begin(), dsu[paru].endpoints.end(), u) != dsu[paru].endpoints.end() and
find(dsu[parv].endpoints.begin(), dsu[parv].endpoints.end(), v) != dsu[parv].endpoints.end()) {
dsu[cur_node].endpoints = {dsu[paru].endpoints[dsu[paru].endpoints[0] == u], dsu[parv].endpoints[dsu[parv].endpoints[0] == v]};
}
else {
dsu[cur_node].endpoints = {-1, -1};
dsu[cur_node].is_line = false;
dsu[cur_node].w = min(dsu[cur_node].w, c);
}
g.emplace_back();
g[cur_node].push_back(paru);
g[cur_node].push_back(parv);
dsu[paru].par = dsu[parv].par = cur_node;
dsu[cur_node].size = dsu[paru].size + dsu[parv].size;
}
vector<vector<int> > fa;
vector<int> dep, closest_anc;
const int LOG = 20;
void depth_dfs(int v, int cur_close) {
if(dsu[v].w != inf)
cur_close = v;
closest_anc[v] = cur_close;
for(int to : g[v]) {
dep[to] = dep[v] + 1;
fa[to][0] = v;
depth_dfs(to, cur_close);
}
}
void init_lca(void) {
int n = dsu.size();
closest_anc.assign(n, -1);
fa.assign(n, vector<int>(LOG, -1));
dep.assign(n, -1);
for(int i = 0; i < n; ++i) {
if(dep[i] == -1) {
int v = get_root(i);
dep[v] = 0;
depth_dfs(v, -1);
}
}
for(int jump = 1; jump < LOG; ++jump) {
for(int i = 0; i < n; ++i) {
fa[i][jump] = (fa[i][jump - 1] == -1 ? -1 : fa[fa[i][jump - 1]][jump - 1]);
}
}
// for(int i = 0; i < n; ++i) {
// cout << i << " : ";
// for(int j = 0; j < 3; ++j)
// cout << fa[i][j] << ' ';
// cout << endl;
// }
}
int lca(int u, int v) {
if(dep[u] < dep[v]) {
swap(u, v);
}
int diff = dep[u] - dep[v];
// cout << u << ' ' << v << " Diff : " << diff << endl;
for(int jump = 0; jump < LOG; ++jump) {
if((1 << jump) & diff)
u = fa[u][jump];
}
assert(u != v);
assert(u != -1);
for(int jump = LOG - 1; jump >= 0; --jump) {
if(u == -1 or v == -1) {
// if(u == -1)
// cout << "u";
// if(v == -1)
// cout << "v";
// cout << endl;
assert(false);
}
if(fa[u][jump] != fa[v][jump]) {
u = fa[u][jump];
v = fa[v][jump];
assert(u != -1 and v != -1);
}
}
assert(u != v);
return closest_anc[fa[u][0]];
}
void init(int N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
g.assign(N, vector<int>());
edges.assign(M, Edge());
for(int i = 0; i < M; ++i) {
edges[i] = {U[i], V[i], W[i]};
}
sort(edges.begin(), edges.end());
for(int i = 0; i < N; ++i) {
dsu.emplace_back(Node(i));
}
for(const Edge& e : edges) {
merge(e.from, e.to, e.c);
}
init_lca();
}
int getMinimumFuelCapacity(int X, int Y) {
if(get_root(X) != get_root(Y)) {
return -1;
}
int anc = lca(X, Y);
if(anc == -1) {
return -1;
}
assert(dsu[anc].w != inf);
return dsu[anc].w;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
684 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
83 ms |
35972 KB |
Output is correct |
10 |
Correct |
111 ms |
42516 KB |
Output is correct |
11 |
Correct |
113 ms |
41756 KB |
Output is correct |
12 |
Correct |
116 ms |
44696 KB |
Output is correct |
13 |
Correct |
120 ms |
46744 KB |
Output is correct |
14 |
Correct |
111 ms |
34704 KB |
Output is correct |
15 |
Correct |
261 ms |
47044 KB |
Output is correct |
16 |
Correct |
233 ms |
46716 KB |
Output is correct |
17 |
Correct |
258 ms |
48296 KB |
Output is correct |
18 |
Correct |
355 ms |
51692 KB |
Output is correct |
19 |
Correct |
71 ms |
12240 KB |
Output is correct |
20 |
Correct |
273 ms |
48208 KB |
Output is correct |
21 |
Correct |
243 ms |
46320 KB |
Output is correct |
22 |
Correct |
258 ms |
49848 KB |
Output is correct |
23 |
Correct |
343 ms |
52228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
303 ms |
52356 KB |
Output is correct |
4 |
Correct |
304 ms |
54660 KB |
Output is correct |
5 |
Correct |
370 ms |
54396 KB |
Output is correct |
6 |
Correct |
289 ms |
55300 KB |
Output is correct |
7 |
Correct |
315 ms |
53584 KB |
Output is correct |
8 |
Correct |
312 ms |
52944 KB |
Output is correct |
9 |
Correct |
308 ms |
54092 KB |
Output is correct |
10 |
Correct |
287 ms |
51700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
684 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
704 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
676 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
860 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
1 ms |
860 KB |
Output is correct |
26 |
Correct |
1 ms |
860 KB |
Output is correct |
27 |
Correct |
1 ms |
856 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
684 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
83 ms |
35972 KB |
Output is correct |
11 |
Correct |
111 ms |
42516 KB |
Output is correct |
12 |
Correct |
113 ms |
41756 KB |
Output is correct |
13 |
Correct |
116 ms |
44696 KB |
Output is correct |
14 |
Correct |
120 ms |
46744 KB |
Output is correct |
15 |
Correct |
1 ms |
704 KB |
Output is correct |
16 |
Correct |
1 ms |
860 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
676 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
860 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
860 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
856 KB |
Output is correct |
33 |
Correct |
1 ms |
860 KB |
Output is correct |
34 |
Correct |
12 ms |
6140 KB |
Output is correct |
35 |
Correct |
127 ms |
43412 KB |
Output is correct |
36 |
Correct |
126 ms |
44312 KB |
Output is correct |
37 |
Correct |
139 ms |
44172 KB |
Output is correct |
38 |
Correct |
150 ms |
43740 KB |
Output is correct |
39 |
Correct |
137 ms |
43508 KB |
Output is correct |
40 |
Correct |
120 ms |
40088 KB |
Output is correct |
41 |
Correct |
139 ms |
44176 KB |
Output is correct |
42 |
Correct |
135 ms |
43192 KB |
Output is correct |
43 |
Correct |
136 ms |
48828 KB |
Output is correct |
44 |
Correct |
134 ms |
43892 KB |
Output is correct |
45 |
Correct |
124 ms |
40620 KB |
Output is correct |
46 |
Correct |
128 ms |
43280 KB |
Output is correct |
47 |
Correct |
128 ms |
44068 KB |
Output is correct |
48 |
Correct |
127 ms |
45928 KB |
Output is correct |
49 |
Correct |
50 ms |
9048 KB |
Output is correct |
50 |
Correct |
49 ms |
9796 KB |
Output is correct |
51 |
Correct |
99 ms |
32860 KB |
Output is correct |
52 |
Correct |
156 ms |
48216 KB |
Output is correct |
53 |
Correct |
166 ms |
49940 KB |
Output is correct |
54 |
Correct |
181 ms |
50096 KB |
Output is correct |
55 |
Correct |
120 ms |
47016 KB |
Output is correct |
56 |
Correct |
155 ms |
50040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
684 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
83 ms |
35972 KB |
Output is correct |
10 |
Correct |
111 ms |
42516 KB |
Output is correct |
11 |
Correct |
113 ms |
41756 KB |
Output is correct |
12 |
Correct |
116 ms |
44696 KB |
Output is correct |
13 |
Correct |
120 ms |
46744 KB |
Output is correct |
14 |
Correct |
111 ms |
34704 KB |
Output is correct |
15 |
Correct |
261 ms |
47044 KB |
Output is correct |
16 |
Correct |
233 ms |
46716 KB |
Output is correct |
17 |
Correct |
258 ms |
48296 KB |
Output is correct |
18 |
Correct |
355 ms |
51692 KB |
Output is correct |
19 |
Correct |
303 ms |
52356 KB |
Output is correct |
20 |
Correct |
304 ms |
54660 KB |
Output is correct |
21 |
Correct |
370 ms |
54396 KB |
Output is correct |
22 |
Correct |
289 ms |
55300 KB |
Output is correct |
23 |
Correct |
315 ms |
53584 KB |
Output is correct |
24 |
Correct |
312 ms |
52944 KB |
Output is correct |
25 |
Correct |
308 ms |
54092 KB |
Output is correct |
26 |
Correct |
287 ms |
51700 KB |
Output is correct |
27 |
Correct |
1 ms |
704 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
676 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
860 KB |
Output is correct |
35 |
Correct |
1 ms |
860 KB |
Output is correct |
36 |
Correct |
12 ms |
6140 KB |
Output is correct |
37 |
Correct |
127 ms |
43412 KB |
Output is correct |
38 |
Correct |
126 ms |
44312 KB |
Output is correct |
39 |
Correct |
139 ms |
44172 KB |
Output is correct |
40 |
Correct |
150 ms |
43740 KB |
Output is correct |
41 |
Correct |
137 ms |
43508 KB |
Output is correct |
42 |
Correct |
120 ms |
40088 KB |
Output is correct |
43 |
Correct |
139 ms |
44176 KB |
Output is correct |
44 |
Correct |
135 ms |
43192 KB |
Output is correct |
45 |
Correct |
136 ms |
48828 KB |
Output is correct |
46 |
Correct |
134 ms |
43892 KB |
Output is correct |
47 |
Correct |
19 ms |
6124 KB |
Output is correct |
48 |
Correct |
323 ms |
49456 KB |
Output is correct |
49 |
Correct |
287 ms |
48532 KB |
Output is correct |
50 |
Correct |
293 ms |
48960 KB |
Output is correct |
51 |
Correct |
292 ms |
48380 KB |
Output is correct |
52 |
Correct |
281 ms |
46200 KB |
Output is correct |
53 |
Correct |
215 ms |
35696 KB |
Output is correct |
54 |
Correct |
271 ms |
49000 KB |
Output is correct |
55 |
Correct |
265 ms |
48128 KB |
Output is correct |
56 |
Correct |
390 ms |
52432 KB |
Output is correct |
57 |
Correct |
302 ms |
49292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
684 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
83 ms |
35972 KB |
Output is correct |
11 |
Correct |
111 ms |
42516 KB |
Output is correct |
12 |
Correct |
113 ms |
41756 KB |
Output is correct |
13 |
Correct |
116 ms |
44696 KB |
Output is correct |
14 |
Correct |
120 ms |
46744 KB |
Output is correct |
15 |
Correct |
111 ms |
34704 KB |
Output is correct |
16 |
Correct |
261 ms |
47044 KB |
Output is correct |
17 |
Correct |
233 ms |
46716 KB |
Output is correct |
18 |
Correct |
258 ms |
48296 KB |
Output is correct |
19 |
Correct |
355 ms |
51692 KB |
Output is correct |
20 |
Correct |
303 ms |
52356 KB |
Output is correct |
21 |
Correct |
304 ms |
54660 KB |
Output is correct |
22 |
Correct |
370 ms |
54396 KB |
Output is correct |
23 |
Correct |
289 ms |
55300 KB |
Output is correct |
24 |
Correct |
315 ms |
53584 KB |
Output is correct |
25 |
Correct |
312 ms |
52944 KB |
Output is correct |
26 |
Correct |
308 ms |
54092 KB |
Output is correct |
27 |
Correct |
287 ms |
51700 KB |
Output is correct |
28 |
Correct |
1 ms |
704 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
676 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
860 KB |
Output is correct |
36 |
Correct |
1 ms |
860 KB |
Output is correct |
37 |
Correct |
12 ms |
6140 KB |
Output is correct |
38 |
Correct |
127 ms |
43412 KB |
Output is correct |
39 |
Correct |
126 ms |
44312 KB |
Output is correct |
40 |
Correct |
139 ms |
44172 KB |
Output is correct |
41 |
Correct |
150 ms |
43740 KB |
Output is correct |
42 |
Correct |
137 ms |
43508 KB |
Output is correct |
43 |
Correct |
120 ms |
40088 KB |
Output is correct |
44 |
Correct |
139 ms |
44176 KB |
Output is correct |
45 |
Correct |
135 ms |
43192 KB |
Output is correct |
46 |
Correct |
136 ms |
48828 KB |
Output is correct |
47 |
Correct |
134 ms |
43892 KB |
Output is correct |
48 |
Correct |
19 ms |
6124 KB |
Output is correct |
49 |
Correct |
323 ms |
49456 KB |
Output is correct |
50 |
Correct |
287 ms |
48532 KB |
Output is correct |
51 |
Correct |
293 ms |
48960 KB |
Output is correct |
52 |
Correct |
292 ms |
48380 KB |
Output is correct |
53 |
Correct |
281 ms |
46200 KB |
Output is correct |
54 |
Correct |
215 ms |
35696 KB |
Output is correct |
55 |
Correct |
271 ms |
49000 KB |
Output is correct |
56 |
Correct |
265 ms |
48128 KB |
Output is correct |
57 |
Correct |
390 ms |
52432 KB |
Output is correct |
58 |
Correct |
302 ms |
49292 KB |
Output is correct |
59 |
Correct |
71 ms |
12240 KB |
Output is correct |
60 |
Correct |
273 ms |
48208 KB |
Output is correct |
61 |
Correct |
243 ms |
46320 KB |
Output is correct |
62 |
Correct |
258 ms |
49848 KB |
Output is correct |
63 |
Correct |
343 ms |
52228 KB |
Output is correct |
64 |
Correct |
1 ms |
604 KB |
Output is correct |
65 |
Correct |
1 ms |
600 KB |
Output is correct |
66 |
Correct |
1 ms |
604 KB |
Output is correct |
67 |
Correct |
1 ms |
604 KB |
Output is correct |
68 |
Correct |
1 ms |
604 KB |
Output is correct |
69 |
Correct |
1 ms |
860 KB |
Output is correct |
70 |
Correct |
1 ms |
860 KB |
Output is correct |
71 |
Correct |
1 ms |
860 KB |
Output is correct |
72 |
Correct |
1 ms |
856 KB |
Output is correct |
73 |
Correct |
1 ms |
860 KB |
Output is correct |
74 |
Correct |
124 ms |
40620 KB |
Output is correct |
75 |
Correct |
128 ms |
43280 KB |
Output is correct |
76 |
Correct |
128 ms |
44068 KB |
Output is correct |
77 |
Correct |
127 ms |
45928 KB |
Output is correct |
78 |
Correct |
50 ms |
9048 KB |
Output is correct |
79 |
Correct |
49 ms |
9796 KB |
Output is correct |
80 |
Correct |
99 ms |
32860 KB |
Output is correct |
81 |
Correct |
156 ms |
48216 KB |
Output is correct |
82 |
Correct |
166 ms |
49940 KB |
Output is correct |
83 |
Correct |
181 ms |
50096 KB |
Output is correct |
84 |
Correct |
120 ms |
47016 KB |
Output is correct |
85 |
Correct |
155 ms |
50040 KB |
Output is correct |
86 |
Correct |
61 ms |
16416 KB |
Output is correct |
87 |
Correct |
292 ms |
49524 KB |
Output is correct |
88 |
Correct |
317 ms |
49172 KB |
Output is correct |
89 |
Correct |
298 ms |
47180 KB |
Output is correct |
90 |
Correct |
119 ms |
13632 KB |
Output is correct |
91 |
Correct |
135 ms |
17384 KB |
Output is correct |
92 |
Correct |
232 ms |
37708 KB |
Output is correct |
93 |
Correct |
305 ms |
52128 KB |
Output is correct |
94 |
Correct |
330 ms |
53024 KB |
Output is correct |
95 |
Correct |
313 ms |
54024 KB |
Output is correct |
96 |
Correct |
359 ms |
51564 KB |
Output is correct |
97 |
Correct |
372 ms |
53404 KB |
Output is correct |