#include "swap.h"
#ifndef EVAL
#include "grader.cpp"
#endif
#include <bits/stdc++.h>
#define pii pair<int, int>
#define F first
#define S second
using namespace std;
namespace
{
int N, M, t, pre[100000][20], out[100000][20], dsu[100000], rk[100000], jump[100000][20];
pii io[100000];
vector<pair<int, pii>> edge;
vector<pii> E[100000];
int find(int k)
{
return dsu[k] == k ? k : dsu[k] = find(dsu[k]);
}
void merge(int x, int y)
{
x = find(x), y = find(y);
if (x == y)
return;
if (rk[x] < rk[y])
{
merge(y, x);
return;
}
dsu[y] = x;
rk[x] += rk[y];
}
void dfs(int u)
{
io[u].F = ++t;
for (auto [v, w] : E[u])
if (v != pre[u][0])
{
pre[v][0] = u;
jump[v][0] = w;
dfs(v);
}
io[u].S = t;
}
bool isChild(int r, int c)
{
return io[r].F <= io[c].F && io[c].S <= io[r].S;
}
}
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W)
{
::N = N, ::M = M;
for (int i = 0; i < N; i++)
out[i][0] = 2e9, dsu[i] = i, rk[i] = 1;
for (int i = 0; i < M; i++)
edge.emplace_back(make_pair(W[i], pii(U[i], V[i])));
sort(edge.begin(), edge.end());
for (auto [w, p] : edge)
{
auto [u, v] = p;
if (find(u) == find(v))
out[u][0] = min(out[u][0], w), out[v][0] = min(out[v][0], w);
else
{
merge(u, v);
E[u].emplace_back(pii(v, w));
E[v].emplace_back(pii(u, w));
}
}
dfs(0);
for (int p = 0; p < 19; p++)
for (int i = 0; i < N; i++)
{
pre[i][p + 1] = pre[pre[i][p]][p];
jump[i][p + 1] = max(jump[i][p], jump[pre[i][p]][p]);
out[i][p + 1] = min(out[i][p], out[pre[i][p]][p]);
}
}
int getMinimumFuelCapacity(int X, int Y)
{
int lca = X, path = 0, alt = 2e9;
for (int p = 19; p >= 0; p--)
if (!isChild(pre[lca][p], Y))
lca = pre[lca][p];
if (!isChild(lca, Y))
lca = pre[lca][0];
alt = out[lca][0];
for (int p = 19; p >= 0; p--)
if (!isChild(pre[X][p], lca))
{
path = max(path, jump[X][p]);
alt = min(alt, out[X][p]);
X = pre[X][p];
}
if (!isChild(X, lca))
{
path = max(path, jump[X][0]);
alt = min(alt, out[X][0]);
X = pre[X][0];
}
for (int p = 19; p >= 0; p--)
if (!isChild(pre[Y][p], lca))
{
path = max(path, jump[Y][p]);
alt = min(alt, out[Y][p]);
Y = pre[Y][p];
}
if (!isChild(Y, lca))
{
path = max(path, jump[Y][0]);
alt = min(alt, out[Y][0]);
Y = pre[Y][0];
}
int ans = max(path, alt);
return (ans > 1e9 ? -1 : ans);
}
Compilation message
swap.cpp: In function 'void {anonymous}::dfs(int)':
swap.cpp:40:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
40 | for (auto [v, w] : E[u])
| ^
swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:64:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
64 | for (auto [w, p] : edge)
| ^
swap.cpp:66:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
66 | auto [u, v] = p;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2772 KB |
Output is correct |
5 |
Correct |
3 ms |
2924 KB |
Output is correct |
6 |
Correct |
2 ms |
2900 KB |
Output is correct |
7 |
Correct |
2 ms |
2928 KB |
Output is correct |
8 |
Correct |
2 ms |
3028 KB |
Output is correct |
9 |
Correct |
111 ms |
32156 KB |
Output is correct |
10 |
Correct |
159 ms |
39860 KB |
Output is correct |
11 |
Correct |
172 ms |
38848 KB |
Output is correct |
12 |
Correct |
155 ms |
41320 KB |
Output is correct |
13 |
Correct |
140 ms |
42520 KB |
Output is correct |
14 |
Correct |
156 ms |
31960 KB |
Output is correct |
15 |
Correct |
440 ms |
43900 KB |
Output is correct |
16 |
Correct |
503 ms |
41736 KB |
Output is correct |
17 |
Correct |
495 ms |
46696 KB |
Output is correct |
18 |
Correct |
458 ms |
45496 KB |
Output is correct |
19 |
Incorrect |
130 ms |
12904 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Incorrect |
212 ms |
35052 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2772 KB |
Output is correct |
5 |
Correct |
3 ms |
2924 KB |
Output is correct |
6 |
Correct |
2 ms |
2900 KB |
Output is correct |
7 |
Correct |
2 ms |
2928 KB |
Output is correct |
8 |
Correct |
2 ms |
3028 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Incorrect |
3 ms |
2900 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
3 ms |
2924 KB |
Output is correct |
7 |
Correct |
2 ms |
2900 KB |
Output is correct |
8 |
Correct |
2 ms |
2928 KB |
Output is correct |
9 |
Correct |
2 ms |
3028 KB |
Output is correct |
10 |
Correct |
111 ms |
32156 KB |
Output is correct |
11 |
Correct |
159 ms |
39860 KB |
Output is correct |
12 |
Correct |
172 ms |
38848 KB |
Output is correct |
13 |
Correct |
155 ms |
41320 KB |
Output is correct |
14 |
Correct |
140 ms |
42520 KB |
Output is correct |
15 |
Incorrect |
3 ms |
2900 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2772 KB |
Output is correct |
5 |
Correct |
3 ms |
2924 KB |
Output is correct |
6 |
Correct |
2 ms |
2900 KB |
Output is correct |
7 |
Correct |
2 ms |
2928 KB |
Output is correct |
8 |
Correct |
2 ms |
3028 KB |
Output is correct |
9 |
Correct |
111 ms |
32156 KB |
Output is correct |
10 |
Correct |
159 ms |
39860 KB |
Output is correct |
11 |
Correct |
172 ms |
38848 KB |
Output is correct |
12 |
Correct |
155 ms |
41320 KB |
Output is correct |
13 |
Correct |
140 ms |
42520 KB |
Output is correct |
14 |
Correct |
156 ms |
31960 KB |
Output is correct |
15 |
Correct |
440 ms |
43900 KB |
Output is correct |
16 |
Correct |
503 ms |
41736 KB |
Output is correct |
17 |
Correct |
495 ms |
46696 KB |
Output is correct |
18 |
Correct |
458 ms |
45496 KB |
Output is correct |
19 |
Incorrect |
212 ms |
35052 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
3 ms |
2924 KB |
Output is correct |
7 |
Correct |
2 ms |
2900 KB |
Output is correct |
8 |
Correct |
2 ms |
2928 KB |
Output is correct |
9 |
Correct |
2 ms |
3028 KB |
Output is correct |
10 |
Correct |
111 ms |
32156 KB |
Output is correct |
11 |
Correct |
159 ms |
39860 KB |
Output is correct |
12 |
Correct |
172 ms |
38848 KB |
Output is correct |
13 |
Correct |
155 ms |
41320 KB |
Output is correct |
14 |
Correct |
140 ms |
42520 KB |
Output is correct |
15 |
Correct |
156 ms |
31960 KB |
Output is correct |
16 |
Correct |
440 ms |
43900 KB |
Output is correct |
17 |
Correct |
503 ms |
41736 KB |
Output is correct |
18 |
Correct |
495 ms |
46696 KB |
Output is correct |
19 |
Correct |
458 ms |
45496 KB |
Output is correct |
20 |
Incorrect |
212 ms |
35052 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |