#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, M = 2e5 + 5;
int n, m, fa[N + M], val[N + M], col[N + M];
int par[19][N + M], nxt[N + M], d[N + M];
vector<int> adj[N + M];
inline int find(int u) { return fa[u] == u ? u : fa[u] = find(fa[u]); }
inline void merge(int u, int v, int w) {
if ((u = find(u)) == (v = find(v))) {
if (!col[u]) {
fa[u] = ++ n;
adj[n].push_back(u);
col[n] = 1, val[n] = w;
}
return ;
}
fa[u] = fa[v] = ++ n;
adj[n].push_back(u);
adj[n].push_back(v);
col[n] = col[u] | col[v], val[n] = w;
}
void pre_dfs(int u, int f = 0) {
if (col[u]) f = u;
nxt[u] = f;
for (int v : adj[u]) {
d[v] = d[par[0][v] = u] + 1;
for (int i = 1; i < 19; i++)
par[i][v] = par[i - 1][par[i - 1][v]];
pre_dfs(v, f);
}
}
inline int lca(int u, int v) {
if (d[u] > d[v]) swap(u, v);
for (int i = 19; i --; )
if ((d[v] - d[u]) >> i & 1)
v = par[i][v];
if (u == v) return u;
for (int i = 19; i --; )
if (par[i][u] != par[i][v]) {
u = par[i][u];
v = par[i][v];
}
return par[0][u];
}
void init(int _n, int _m, vector<int> U, vector<int> V, vector<int> W) {
n = _n, m = _m;
iota(fa, fa + N + M, 0);
vector<int> ids(m);
iota(ids.begin(), ids.end(), 0);
sort(ids.begin(), ids.end(), [&](const int &i, const int &j){
return W[i] < W[j];
});
for (int i : ids)
merge(U[i] + 1, V[i] + 1, W[i]);
val[0] = -1, pre_dfs(n);
}
int getMinimumFuelCapacity(int X, int Y) {
int _lca = lca(X + 1, Y + 1);
return col[_lca] ? val[_lca] : val[par[0][_lca]];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
8660 KB |
Output is correct |
2 |
Correct |
5 ms |
8636 KB |
Output is correct |
3 |
Incorrect |
5 ms |
8660 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
8660 KB |
Output is correct |
2 |
Correct |
5 ms |
8636 KB |
Output is correct |
3 |
Incorrect |
283 ms |
40844 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
8660 KB |
Output is correct |
2 |
Correct |
5 ms |
8636 KB |
Output is correct |
3 |
Incorrect |
5 ms |
8660 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
8660 KB |
Output is correct |
2 |
Correct |
5 ms |
8636 KB |
Output is correct |
3 |
Incorrect |
5 ms |
8660 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
8660 KB |
Output is correct |
2 |
Correct |
5 ms |
8636 KB |
Output is correct |
3 |
Incorrect |
5 ms |
8660 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
8660 KB |
Output is correct |
2 |
Correct |
5 ms |
8636 KB |
Output is correct |
3 |
Incorrect |
5 ms |
8660 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |