#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define Submit
#ifdef Submit
#include "swap.h"
#endif // Submit
using namespace __gnu_pbds;
using namespace std;
const int N = 2e5 + 10;
int n;
int deg[N];
int root[N], ok[N];
vector <int> g[N];
int par[N][20];
int in[N], out[N], ti, ans[N];
int Find(int u) {
return (u == root[u] ? u : root[u] = Find(root[u]));
}
int Union(int u, int v, int _) {
++n;
root[n] = n;
if ((u = Find(u)) == (v = Find(v))) {
ok[n] = 1;
root[u] = n;
g[n].push_back(u);
} else {
ok[n] = ok[u] | ok[v] | _;
root[u] = root[v] = n;
g[n].push_back(u);
g[n].push_back(v);
}
return n;
}
void dfs(int u) {
in[u] = ++ti;
for (auto v: g[u]) {
par[v][0] = u;
for (int i = 1; i < 20; ++i)
par[v][i] = par[par[v][i - 1]][i - 1];
dfs(v);
}
out[u] = ti;
}
int check(int u, int v) {
return in[u] <= in[v] && out[v] <= out[u];
}
int LCA(int u, int v) {
if (check(u, v)) return u;
for (int i = 19; i >= 0; --i)
if (par[u][i] && !check(par[u][i], v)) u = par[u][i];
return par[u][0];
}
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
n = N;
vector <tuple <int, int, int> > e;
for (int i = 1; i <= n; ++i) root[i] = i;
for (int i = 0; i < M; ++i) {
++U[i], ++V[i];
e.push_back({W[i], U[i], V[i]});
}
sort(e.begin(), e.end());
for (int i = 0; i < e.size(); ++i) {
auto [w, u, v] = e[i];
++deg[u], ++deg[v];
int cur = Union(u, v, (deg[u] > 2 || deg[v] > 2));
ans[cur] = w;
}
for (int i = n; i >= 1; --i)
if (in[i] == 0) dfs(i);
}
int getMinimumFuelCapacity(int X, int Y) {
++X, ++Y;
int w = LCA(X, Y);
if (ok[w]) return ans[w];
for (int i = 19; i >= 0; --i)
if (par[w][i] && !ok[par[w][i]]) w = par[w][i];
w = par[w][0];
if (ok[w]) return ans[w];
return -1;
}
#ifndef Submit
int 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 // Submit
Compilation message
swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:75:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for (int i = 0; i < e.size(); ++i) {
| ~~^~~~~~~~~~
swap.cpp:76:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
76 | auto [w, u, v] = e[i];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
6 |
Correct |
4 ms |
5284 KB |
Output is correct |
7 |
Correct |
4 ms |
5204 KB |
Output is correct |
8 |
Correct |
4 ms |
5204 KB |
Output is correct |
9 |
Correct |
78 ms |
25216 KB |
Output is correct |
10 |
Correct |
103 ms |
29764 KB |
Output is correct |
11 |
Correct |
106 ms |
29396 KB |
Output is correct |
12 |
Correct |
97 ms |
30784 KB |
Output is correct |
13 |
Correct |
91 ms |
32320 KB |
Output is correct |
14 |
Correct |
90 ms |
25376 KB |
Output is correct |
15 |
Correct |
174 ms |
31640 KB |
Output is correct |
16 |
Correct |
182 ms |
31004 KB |
Output is correct |
17 |
Correct |
218 ms |
32544 KB |
Output is correct |
18 |
Correct |
260 ms |
34104 KB |
Output is correct |
19 |
Correct |
74 ms |
12488 KB |
Output is correct |
20 |
Correct |
183 ms |
32000 KB |
Output is correct |
21 |
Correct |
163 ms |
31328 KB |
Output is correct |
22 |
Correct |
202 ms |
32908 KB |
Output is correct |
23 |
Correct |
261 ms |
34460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
258 ms |
34260 KB |
Output is correct |
4 |
Correct |
224 ms |
35720 KB |
Output is correct |
5 |
Correct |
254 ms |
35000 KB |
Output is correct |
6 |
Correct |
214 ms |
35564 KB |
Output is correct |
7 |
Correct |
253 ms |
35396 KB |
Output is correct |
8 |
Correct |
229 ms |
34352 KB |
Output is correct |
9 |
Correct |
248 ms |
35136 KB |
Output is correct |
10 |
Correct |
225 ms |
34020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
6 |
Correct |
4 ms |
5284 KB |
Output is correct |
7 |
Correct |
4 ms |
5204 KB |
Output is correct |
8 |
Correct |
4 ms |
5204 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
4 ms |
5300 KB |
Output is correct |
11 |
Correct |
4 ms |
5308 KB |
Output is correct |
12 |
Correct |
3 ms |
5204 KB |
Output is correct |
13 |
Correct |
3 ms |
5204 KB |
Output is correct |
14 |
Correct |
3 ms |
5204 KB |
Output is correct |
15 |
Correct |
4 ms |
5204 KB |
Output is correct |
16 |
Correct |
3 ms |
5204 KB |
Output is correct |
17 |
Correct |
4 ms |
5204 KB |
Output is correct |
18 |
Correct |
4 ms |
5204 KB |
Output is correct |
19 |
Correct |
4 ms |
5204 KB |
Output is correct |
20 |
Correct |
4 ms |
5280 KB |
Output is correct |
21 |
Correct |
4 ms |
5332 KB |
Output is correct |
22 |
Correct |
4 ms |
5320 KB |
Output is correct |
23 |
Correct |
3 ms |
5204 KB |
Output is correct |
24 |
Correct |
4 ms |
5460 KB |
Output is correct |
25 |
Correct |
4 ms |
5412 KB |
Output is correct |
26 |
Correct |
5 ms |
5452 KB |
Output is correct |
27 |
Correct |
4 ms |
5332 KB |
Output is correct |
28 |
Correct |
4 ms |
5452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
4 ms |
5284 KB |
Output is correct |
8 |
Correct |
4 ms |
5204 KB |
Output is correct |
9 |
Correct |
4 ms |
5204 KB |
Output is correct |
10 |
Correct |
78 ms |
25216 KB |
Output is correct |
11 |
Correct |
103 ms |
29764 KB |
Output is correct |
12 |
Correct |
106 ms |
29396 KB |
Output is correct |
13 |
Correct |
97 ms |
30784 KB |
Output is correct |
14 |
Correct |
91 ms |
32320 KB |
Output is correct |
15 |
Correct |
4 ms |
5300 KB |
Output is correct |
16 |
Correct |
4 ms |
5308 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
3 ms |
5204 KB |
Output is correct |
19 |
Correct |
3 ms |
5204 KB |
Output is correct |
20 |
Correct |
4 ms |
5204 KB |
Output is correct |
21 |
Correct |
3 ms |
5204 KB |
Output is correct |
22 |
Correct |
4 ms |
5204 KB |
Output is correct |
23 |
Correct |
4 ms |
5204 KB |
Output is correct |
24 |
Correct |
4 ms |
5204 KB |
Output is correct |
25 |
Correct |
4 ms |
5280 KB |
Output is correct |
26 |
Correct |
4 ms |
5332 KB |
Output is correct |
27 |
Correct |
4 ms |
5320 KB |
Output is correct |
28 |
Correct |
3 ms |
5204 KB |
Output is correct |
29 |
Correct |
4 ms |
5460 KB |
Output is correct |
30 |
Correct |
4 ms |
5412 KB |
Output is correct |
31 |
Correct |
5 ms |
5452 KB |
Output is correct |
32 |
Correct |
4 ms |
5332 KB |
Output is correct |
33 |
Correct |
4 ms |
5452 KB |
Output is correct |
34 |
Correct |
13 ms |
8468 KB |
Output is correct |
35 |
Correct |
109 ms |
30772 KB |
Output is correct |
36 |
Correct |
100 ms |
30820 KB |
Output is correct |
37 |
Correct |
99 ms |
30764 KB |
Output is correct |
38 |
Correct |
97 ms |
30504 KB |
Output is correct |
39 |
Correct |
95 ms |
30252 KB |
Output is correct |
40 |
Correct |
100 ms |
28208 KB |
Output is correct |
41 |
Correct |
102 ms |
30764 KB |
Output is correct |
42 |
Correct |
106 ms |
30756 KB |
Output is correct |
43 |
Correct |
89 ms |
32916 KB |
Output is correct |
44 |
Correct |
103 ms |
30868 KB |
Output is correct |
45 |
Runtime error |
99 ms |
34292 KB |
Execution killed with signal 11 |
46 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
6 |
Correct |
4 ms |
5284 KB |
Output is correct |
7 |
Correct |
4 ms |
5204 KB |
Output is correct |
8 |
Correct |
4 ms |
5204 KB |
Output is correct |
9 |
Correct |
78 ms |
25216 KB |
Output is correct |
10 |
Correct |
103 ms |
29764 KB |
Output is correct |
11 |
Correct |
106 ms |
29396 KB |
Output is correct |
12 |
Correct |
97 ms |
30784 KB |
Output is correct |
13 |
Correct |
91 ms |
32320 KB |
Output is correct |
14 |
Correct |
90 ms |
25376 KB |
Output is correct |
15 |
Correct |
174 ms |
31640 KB |
Output is correct |
16 |
Correct |
182 ms |
31004 KB |
Output is correct |
17 |
Correct |
218 ms |
32544 KB |
Output is correct |
18 |
Correct |
260 ms |
34104 KB |
Output is correct |
19 |
Correct |
258 ms |
34260 KB |
Output is correct |
20 |
Correct |
224 ms |
35720 KB |
Output is correct |
21 |
Correct |
254 ms |
35000 KB |
Output is correct |
22 |
Correct |
214 ms |
35564 KB |
Output is correct |
23 |
Correct |
253 ms |
35396 KB |
Output is correct |
24 |
Correct |
229 ms |
34352 KB |
Output is correct |
25 |
Correct |
248 ms |
35136 KB |
Output is correct |
26 |
Correct |
225 ms |
34020 KB |
Output is correct |
27 |
Correct |
4 ms |
5300 KB |
Output is correct |
28 |
Correct |
4 ms |
5308 KB |
Output is correct |
29 |
Correct |
3 ms |
5204 KB |
Output is correct |
30 |
Correct |
3 ms |
5204 KB |
Output is correct |
31 |
Correct |
3 ms |
5204 KB |
Output is correct |
32 |
Correct |
4 ms |
5204 KB |
Output is correct |
33 |
Correct |
3 ms |
5204 KB |
Output is correct |
34 |
Correct |
4 ms |
5204 KB |
Output is correct |
35 |
Correct |
4 ms |
5204 KB |
Output is correct |
36 |
Correct |
13 ms |
8468 KB |
Output is correct |
37 |
Correct |
109 ms |
30772 KB |
Output is correct |
38 |
Correct |
100 ms |
30820 KB |
Output is correct |
39 |
Correct |
99 ms |
30764 KB |
Output is correct |
40 |
Correct |
97 ms |
30504 KB |
Output is correct |
41 |
Correct |
95 ms |
30252 KB |
Output is correct |
42 |
Correct |
100 ms |
28208 KB |
Output is correct |
43 |
Correct |
102 ms |
30764 KB |
Output is correct |
44 |
Correct |
106 ms |
30756 KB |
Output is correct |
45 |
Correct |
89 ms |
32916 KB |
Output is correct |
46 |
Correct |
103 ms |
30868 KB |
Output is correct |
47 |
Correct |
18 ms |
8524 KB |
Output is correct |
48 |
Correct |
178 ms |
32532 KB |
Output is correct |
49 |
Correct |
166 ms |
32448 KB |
Output is correct |
50 |
Correct |
172 ms |
32448 KB |
Output is correct |
51 |
Correct |
176 ms |
32312 KB |
Output is correct |
52 |
Correct |
163 ms |
30804 KB |
Output is correct |
53 |
Correct |
147 ms |
25000 KB |
Output is correct |
54 |
Correct |
168 ms |
32868 KB |
Output is correct |
55 |
Correct |
166 ms |
32444 KB |
Output is correct |
56 |
Correct |
231 ms |
34108 KB |
Output is correct |
57 |
Correct |
179 ms |
32912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
4 ms |
5284 KB |
Output is correct |
8 |
Correct |
4 ms |
5204 KB |
Output is correct |
9 |
Correct |
4 ms |
5204 KB |
Output is correct |
10 |
Correct |
78 ms |
25216 KB |
Output is correct |
11 |
Correct |
103 ms |
29764 KB |
Output is correct |
12 |
Correct |
106 ms |
29396 KB |
Output is correct |
13 |
Correct |
97 ms |
30784 KB |
Output is correct |
14 |
Correct |
91 ms |
32320 KB |
Output is correct |
15 |
Correct |
90 ms |
25376 KB |
Output is correct |
16 |
Correct |
174 ms |
31640 KB |
Output is correct |
17 |
Correct |
182 ms |
31004 KB |
Output is correct |
18 |
Correct |
218 ms |
32544 KB |
Output is correct |
19 |
Correct |
260 ms |
34104 KB |
Output is correct |
20 |
Correct |
258 ms |
34260 KB |
Output is correct |
21 |
Correct |
224 ms |
35720 KB |
Output is correct |
22 |
Correct |
254 ms |
35000 KB |
Output is correct |
23 |
Correct |
214 ms |
35564 KB |
Output is correct |
24 |
Correct |
253 ms |
35396 KB |
Output is correct |
25 |
Correct |
229 ms |
34352 KB |
Output is correct |
26 |
Correct |
248 ms |
35136 KB |
Output is correct |
27 |
Correct |
225 ms |
34020 KB |
Output is correct |
28 |
Correct |
4 ms |
5300 KB |
Output is correct |
29 |
Correct |
4 ms |
5308 KB |
Output is correct |
30 |
Correct |
3 ms |
5204 KB |
Output is correct |
31 |
Correct |
3 ms |
5204 KB |
Output is correct |
32 |
Correct |
3 ms |
5204 KB |
Output is correct |
33 |
Correct |
4 ms |
5204 KB |
Output is correct |
34 |
Correct |
3 ms |
5204 KB |
Output is correct |
35 |
Correct |
4 ms |
5204 KB |
Output is correct |
36 |
Correct |
4 ms |
5204 KB |
Output is correct |
37 |
Correct |
13 ms |
8468 KB |
Output is correct |
38 |
Correct |
109 ms |
30772 KB |
Output is correct |
39 |
Correct |
100 ms |
30820 KB |
Output is correct |
40 |
Correct |
99 ms |
30764 KB |
Output is correct |
41 |
Correct |
97 ms |
30504 KB |
Output is correct |
42 |
Correct |
95 ms |
30252 KB |
Output is correct |
43 |
Correct |
100 ms |
28208 KB |
Output is correct |
44 |
Correct |
102 ms |
30764 KB |
Output is correct |
45 |
Correct |
106 ms |
30756 KB |
Output is correct |
46 |
Correct |
89 ms |
32916 KB |
Output is correct |
47 |
Correct |
103 ms |
30868 KB |
Output is correct |
48 |
Correct |
18 ms |
8524 KB |
Output is correct |
49 |
Correct |
178 ms |
32532 KB |
Output is correct |
50 |
Correct |
166 ms |
32448 KB |
Output is correct |
51 |
Correct |
172 ms |
32448 KB |
Output is correct |
52 |
Correct |
176 ms |
32312 KB |
Output is correct |
53 |
Correct |
163 ms |
30804 KB |
Output is correct |
54 |
Correct |
147 ms |
25000 KB |
Output is correct |
55 |
Correct |
168 ms |
32868 KB |
Output is correct |
56 |
Correct |
166 ms |
32444 KB |
Output is correct |
57 |
Correct |
231 ms |
34108 KB |
Output is correct |
58 |
Correct |
179 ms |
32912 KB |
Output is correct |
59 |
Correct |
74 ms |
12488 KB |
Output is correct |
60 |
Correct |
183 ms |
32000 KB |
Output is correct |
61 |
Correct |
163 ms |
31328 KB |
Output is correct |
62 |
Correct |
202 ms |
32908 KB |
Output is correct |
63 |
Correct |
261 ms |
34460 KB |
Output is correct |
64 |
Correct |
4 ms |
5204 KB |
Output is correct |
65 |
Correct |
4 ms |
5280 KB |
Output is correct |
66 |
Correct |
4 ms |
5332 KB |
Output is correct |
67 |
Correct |
4 ms |
5320 KB |
Output is correct |
68 |
Correct |
3 ms |
5204 KB |
Output is correct |
69 |
Correct |
4 ms |
5460 KB |
Output is correct |
70 |
Correct |
4 ms |
5412 KB |
Output is correct |
71 |
Correct |
5 ms |
5452 KB |
Output is correct |
72 |
Correct |
4 ms |
5332 KB |
Output is correct |
73 |
Correct |
4 ms |
5452 KB |
Output is correct |
74 |
Runtime error |
99 ms |
34292 KB |
Execution killed with signal 11 |
75 |
Halted |
0 ms |
0 KB |
- |