#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
struct edge {
int u, v, w;
};
class dsu {
private: vector<int> p, rank;
public:
dsu(int n) {
p.assign(n, -1);
rank.assign(n, 0);
}
int root(int x) {
if (p[x] == -1) {
return x;
}
return (p[x] = root(p[x]));
}
bool same_set(int x, int y) {
return (root(x) == root(y));
}
void connect(int x, int y) {
x = root(x); y = root(y);
if (x != y) {
if (rank[x] > rank[y]) {
p[y] = x;
} else {
p[x] = y;
if (rank[x] == rank[y]) {
rank[y]++;
}
}
}
}
};
int n, m, dfs_cnt = 0, max_log;
vector< vector<int> > g;
vector<bool> swappable;
vector<int> dfs_in, dfs_out;
vector< vector<int> > up;
vector<edge> edges;
void dfs(int u, int p) {
//cout << u << endl;
dfs_in[u] = dfs_cnt++;
up[u][0] = p;
for (int i = 1; i <= max_log; i++) {
up[u][i] = up[up[u][i - 1]][i - 1];
}
for (auto& v : g[u]) {
if (v != p) {
dfs(v, u);
}
}
dfs_out[u] = dfs_cnt++;
}
bool is_anc(int u, int v) {
return (dfs_in[u] <= dfs_in[v] && dfs_out[u] >= dfs_out[v]);
}
int lca(int u, int v) {
if (is_anc(u, v)) {
return u;
}
if (is_anc(v, u)) {
return v;
}
for (int i = max_log; i >= 0; i--) {
if (!is_anc(up[u][i], v)) {
u = up[u][i];
}
}
return up[u][0];
}
int first_swappable(int u) {
if (swappable[u]) {
return u;
}
for (int i = max_log; i >= 0; i--) {
if (!swappable[up[u][i]]) {
u = up[u][i];
}
}
if (u == n + m - 1) {
return -1;
}
return up[u][0];
}
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
n = N; m = M;
for (int i = 0; i < M; i++) {
edges.push_back({U[i], V[i], W[i]});
}
sort(edges.begin(), edges.end(), [](const edge &a, const edge &b) {
return a.w < b.w;
});
g.resize(n + m);
swappable.assign(n + m, false);
dsu conn(n);
vector<int> deg(n, 0), cc(n);
iota(cc.begin(), cc.end(), 0);
for (int i = 0; i < m; i++) {
int x = edges[i].u, y = edges[i].v;
deg[x]++;
deg[y]++;
if (conn.same_set(x, y)) {
g[cc[conn.root(x)]].push_back(n + i);
g[n + i].push_back(cc[conn.root(x)]);
swappable[n + i] = true;
cc[conn.root(x)] = n + i;
} else {
g[cc[conn.root(x)]].push_back(n + i);
g[cc[conn.root(y)]].push_back(n + i);
g[n + i].push_back(cc[conn.root(x)]);
g[n + i].push_back(cc[conn.root(y)]);
swappable[n + i] = (swappable[cc[conn.root(x)]] || swappable[cc[conn.root(y)]]);
conn.connect(x, y);
cc[conn.root(x)] = n + i;
if (deg[x] >= 3 || deg[y] >= 3) {
swappable[n + i] = true;
}
}
}
max_log = ceil(log2(n + m));
dfs_in.resize(n + m);
dfs_out.resize(n + m);
up.assign(n + m, vector<int>(max_log + 1));
dfs(n + m - 1, n + m - 1);
}
int getMinimumFuelCapacity(int X, int Y) {
int u = first_swappable(lca(X, Y));
if (u == -1) {
return -1;
}
return edges[u - n].w;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
568 KB |
Output is correct |
8 |
Correct |
1 ms |
568 KB |
Output is correct |
9 |
Correct |
123 ms |
34220 KB |
Output is correct |
10 |
Correct |
174 ms |
41736 KB |
Output is correct |
11 |
Correct |
152 ms |
41080 KB |
Output is correct |
12 |
Correct |
167 ms |
43460 KB |
Output is correct |
13 |
Correct |
152 ms |
45900 KB |
Output is correct |
14 |
Correct |
134 ms |
34488 KB |
Output is correct |
15 |
Correct |
304 ms |
45896 KB |
Output is correct |
16 |
Correct |
251 ms |
44824 KB |
Output is correct |
17 |
Correct |
277 ms |
47412 KB |
Output is correct |
18 |
Correct |
316 ms |
49692 KB |
Output is correct |
19 |
Correct |
76 ms |
11684 KB |
Output is correct |
20 |
Correct |
255 ms |
46648 KB |
Output is correct |
21 |
Correct |
273 ms |
45260 KB |
Output is correct |
22 |
Correct |
282 ms |
48180 KB |
Output is correct |
23 |
Correct |
337 ms |
50512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
242 ms |
49748 KB |
Output is correct |
4 |
Correct |
247 ms |
51988 KB |
Output is correct |
5 |
Correct |
253 ms |
50704 KB |
Output is correct |
6 |
Correct |
247 ms |
51768 KB |
Output is correct |
7 |
Correct |
253 ms |
51340 KB |
Output is correct |
8 |
Correct |
255 ms |
49316 KB |
Output is correct |
9 |
Correct |
262 ms |
50900 KB |
Output is correct |
10 |
Correct |
265 ms |
48872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
568 KB |
Output is correct |
8 |
Correct |
1 ms |
568 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
568 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
2 ms |
724 KB |
Output is correct |
23 |
Correct |
1 ms |
564 KB |
Output is correct |
24 |
Correct |
3 ms |
824 KB |
Output is correct |
25 |
Correct |
3 ms |
816 KB |
Output is correct |
26 |
Correct |
2 ms |
852 KB |
Output is correct |
27 |
Correct |
2 ms |
556 KB |
Output is correct |
28 |
Correct |
2 ms |
824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
568 KB |
Output is correct |
9 |
Correct |
1 ms |
568 KB |
Output is correct |
10 |
Correct |
123 ms |
34220 KB |
Output is correct |
11 |
Correct |
174 ms |
41736 KB |
Output is correct |
12 |
Correct |
152 ms |
41080 KB |
Output is correct |
13 |
Correct |
167 ms |
43460 KB |
Output is correct |
14 |
Correct |
152 ms |
45900 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
2 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
568 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
2 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
564 KB |
Output is correct |
29 |
Correct |
3 ms |
824 KB |
Output is correct |
30 |
Correct |
3 ms |
816 KB |
Output is correct |
31 |
Correct |
2 ms |
852 KB |
Output is correct |
32 |
Correct |
2 ms |
556 KB |
Output is correct |
33 |
Correct |
2 ms |
824 KB |
Output is correct |
34 |
Correct |
14 ms |
5756 KB |
Output is correct |
35 |
Correct |
161 ms |
43172 KB |
Output is correct |
36 |
Correct |
151 ms |
43244 KB |
Output is correct |
37 |
Correct |
169 ms |
43196 KB |
Output is correct |
38 |
Correct |
168 ms |
42636 KB |
Output is correct |
39 |
Correct |
163 ms |
42536 KB |
Output is correct |
40 |
Correct |
142 ms |
38804 KB |
Output is correct |
41 |
Correct |
163 ms |
43436 KB |
Output is correct |
42 |
Correct |
153 ms |
43180 KB |
Output is correct |
43 |
Correct |
136 ms |
46320 KB |
Output is correct |
44 |
Correct |
161 ms |
43576 KB |
Output is correct |
45 |
Correct |
186 ms |
54756 KB |
Output is correct |
46 |
Correct |
165 ms |
43248 KB |
Output is correct |
47 |
Correct |
157 ms |
43284 KB |
Output is correct |
48 |
Correct |
167 ms |
47092 KB |
Output is correct |
49 |
Correct |
94 ms |
44628 KB |
Output is correct |
50 |
Correct |
90 ms |
33352 KB |
Output is correct |
51 |
Correct |
144 ms |
47128 KB |
Output is correct |
52 |
Correct |
192 ms |
60916 KB |
Output is correct |
53 |
Correct |
251 ms |
64960 KB |
Output is correct |
54 |
Correct |
226 ms |
71480 KB |
Output is correct |
55 |
Correct |
167 ms |
46080 KB |
Output is correct |
56 |
Correct |
221 ms |
65220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
568 KB |
Output is correct |
8 |
Correct |
1 ms |
568 KB |
Output is correct |
9 |
Correct |
123 ms |
34220 KB |
Output is correct |
10 |
Correct |
174 ms |
41736 KB |
Output is correct |
11 |
Correct |
152 ms |
41080 KB |
Output is correct |
12 |
Correct |
167 ms |
43460 KB |
Output is correct |
13 |
Correct |
152 ms |
45900 KB |
Output is correct |
14 |
Correct |
134 ms |
34488 KB |
Output is correct |
15 |
Correct |
304 ms |
45896 KB |
Output is correct |
16 |
Correct |
251 ms |
44824 KB |
Output is correct |
17 |
Correct |
277 ms |
47412 KB |
Output is correct |
18 |
Correct |
316 ms |
49692 KB |
Output is correct |
19 |
Correct |
242 ms |
49748 KB |
Output is correct |
20 |
Correct |
247 ms |
51988 KB |
Output is correct |
21 |
Correct |
253 ms |
50704 KB |
Output is correct |
22 |
Correct |
247 ms |
51768 KB |
Output is correct |
23 |
Correct |
253 ms |
51340 KB |
Output is correct |
24 |
Correct |
255 ms |
49316 KB |
Output is correct |
25 |
Correct |
262 ms |
50900 KB |
Output is correct |
26 |
Correct |
265 ms |
48872 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
2 ms |
596 KB |
Output is correct |
35 |
Correct |
1 ms |
596 KB |
Output is correct |
36 |
Correct |
14 ms |
5756 KB |
Output is correct |
37 |
Correct |
161 ms |
43172 KB |
Output is correct |
38 |
Correct |
151 ms |
43244 KB |
Output is correct |
39 |
Correct |
169 ms |
43196 KB |
Output is correct |
40 |
Correct |
168 ms |
42636 KB |
Output is correct |
41 |
Correct |
163 ms |
42536 KB |
Output is correct |
42 |
Correct |
142 ms |
38804 KB |
Output is correct |
43 |
Correct |
163 ms |
43436 KB |
Output is correct |
44 |
Correct |
153 ms |
43180 KB |
Output is correct |
45 |
Correct |
136 ms |
46320 KB |
Output is correct |
46 |
Correct |
161 ms |
43576 KB |
Output is correct |
47 |
Correct |
17 ms |
5696 KB |
Output is correct |
48 |
Correct |
250 ms |
47248 KB |
Output is correct |
49 |
Correct |
225 ms |
47216 KB |
Output is correct |
50 |
Correct |
246 ms |
47148 KB |
Output is correct |
51 |
Correct |
236 ms |
46984 KB |
Output is correct |
52 |
Correct |
229 ms |
44428 KB |
Output is correct |
53 |
Correct |
167 ms |
33972 KB |
Output is correct |
54 |
Correct |
229 ms |
48144 KB |
Output is correct |
55 |
Correct |
227 ms |
47272 KB |
Output is correct |
56 |
Correct |
267 ms |
49708 KB |
Output is correct |
57 |
Correct |
281 ms |
48212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
568 KB |
Output is correct |
9 |
Correct |
1 ms |
568 KB |
Output is correct |
10 |
Correct |
123 ms |
34220 KB |
Output is correct |
11 |
Correct |
174 ms |
41736 KB |
Output is correct |
12 |
Correct |
152 ms |
41080 KB |
Output is correct |
13 |
Correct |
167 ms |
43460 KB |
Output is correct |
14 |
Correct |
152 ms |
45900 KB |
Output is correct |
15 |
Correct |
134 ms |
34488 KB |
Output is correct |
16 |
Correct |
304 ms |
45896 KB |
Output is correct |
17 |
Correct |
251 ms |
44824 KB |
Output is correct |
18 |
Correct |
277 ms |
47412 KB |
Output is correct |
19 |
Correct |
316 ms |
49692 KB |
Output is correct |
20 |
Correct |
242 ms |
49748 KB |
Output is correct |
21 |
Correct |
247 ms |
51988 KB |
Output is correct |
22 |
Correct |
253 ms |
50704 KB |
Output is correct |
23 |
Correct |
247 ms |
51768 KB |
Output is correct |
24 |
Correct |
253 ms |
51340 KB |
Output is correct |
25 |
Correct |
255 ms |
49316 KB |
Output is correct |
26 |
Correct |
262 ms |
50900 KB |
Output is correct |
27 |
Correct |
265 ms |
48872 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
1 ms |
596 KB |
Output is correct |
35 |
Correct |
2 ms |
596 KB |
Output is correct |
36 |
Correct |
1 ms |
596 KB |
Output is correct |
37 |
Correct |
14 ms |
5756 KB |
Output is correct |
38 |
Correct |
161 ms |
43172 KB |
Output is correct |
39 |
Correct |
151 ms |
43244 KB |
Output is correct |
40 |
Correct |
169 ms |
43196 KB |
Output is correct |
41 |
Correct |
168 ms |
42636 KB |
Output is correct |
42 |
Correct |
163 ms |
42536 KB |
Output is correct |
43 |
Correct |
142 ms |
38804 KB |
Output is correct |
44 |
Correct |
163 ms |
43436 KB |
Output is correct |
45 |
Correct |
153 ms |
43180 KB |
Output is correct |
46 |
Correct |
136 ms |
46320 KB |
Output is correct |
47 |
Correct |
161 ms |
43576 KB |
Output is correct |
48 |
Correct |
17 ms |
5696 KB |
Output is correct |
49 |
Correct |
250 ms |
47248 KB |
Output is correct |
50 |
Correct |
225 ms |
47216 KB |
Output is correct |
51 |
Correct |
246 ms |
47148 KB |
Output is correct |
52 |
Correct |
236 ms |
46984 KB |
Output is correct |
53 |
Correct |
229 ms |
44428 KB |
Output is correct |
54 |
Correct |
167 ms |
33972 KB |
Output is correct |
55 |
Correct |
229 ms |
48144 KB |
Output is correct |
56 |
Correct |
227 ms |
47272 KB |
Output is correct |
57 |
Correct |
267 ms |
49708 KB |
Output is correct |
58 |
Correct |
281 ms |
48212 KB |
Output is correct |
59 |
Correct |
76 ms |
11684 KB |
Output is correct |
60 |
Correct |
255 ms |
46648 KB |
Output is correct |
61 |
Correct |
273 ms |
45260 KB |
Output is correct |
62 |
Correct |
282 ms |
48180 KB |
Output is correct |
63 |
Correct |
337 ms |
50512 KB |
Output is correct |
64 |
Correct |
1 ms |
568 KB |
Output is correct |
65 |
Correct |
1 ms |
596 KB |
Output is correct |
66 |
Correct |
1 ms |
596 KB |
Output is correct |
67 |
Correct |
2 ms |
724 KB |
Output is correct |
68 |
Correct |
1 ms |
564 KB |
Output is correct |
69 |
Correct |
3 ms |
824 KB |
Output is correct |
70 |
Correct |
3 ms |
816 KB |
Output is correct |
71 |
Correct |
2 ms |
852 KB |
Output is correct |
72 |
Correct |
2 ms |
556 KB |
Output is correct |
73 |
Correct |
2 ms |
824 KB |
Output is correct |
74 |
Correct |
186 ms |
54756 KB |
Output is correct |
75 |
Correct |
165 ms |
43248 KB |
Output is correct |
76 |
Correct |
157 ms |
43284 KB |
Output is correct |
77 |
Correct |
167 ms |
47092 KB |
Output is correct |
78 |
Correct |
94 ms |
44628 KB |
Output is correct |
79 |
Correct |
90 ms |
33352 KB |
Output is correct |
80 |
Correct |
144 ms |
47128 KB |
Output is correct |
81 |
Correct |
192 ms |
60916 KB |
Output is correct |
82 |
Correct |
251 ms |
64960 KB |
Output is correct |
83 |
Correct |
226 ms |
71480 KB |
Output is correct |
84 |
Correct |
167 ms |
46080 KB |
Output is correct |
85 |
Correct |
221 ms |
65220 KB |
Output is correct |
86 |
Correct |
70 ms |
19192 KB |
Output is correct |
87 |
Correct |
226 ms |
47292 KB |
Output is correct |
88 |
Correct |
215 ms |
47276 KB |
Output is correct |
89 |
Correct |
266 ms |
48736 KB |
Output is correct |
90 |
Correct |
162 ms |
51208 KB |
Output is correct |
91 |
Correct |
172 ms |
48448 KB |
Output is correct |
92 |
Correct |
293 ms |
49688 KB |
Output is correct |
93 |
Correct |
303 ms |
64040 KB |
Output is correct |
94 |
Correct |
362 ms |
68384 KB |
Output is correct |
95 |
Correct |
305 ms |
75444 KB |
Output is correct |
96 |
Correct |
259 ms |
49960 KB |
Output is correct |
97 |
Correct |
350 ms |
59056 KB |
Output is correct |