#include "swap.h"
#include <bits/stdc++.h>
#define fr first
#define sc second
#define all(s) s.begin(), s.end()
using namespace std;
const int nmax = 300005;
int pardsu[nmax], last, lca[nmax][19], marked[nmax], grad[nmax], val[nmax], timp, in[nmax], out[nmax], apropiat[nmax];
vector<pair<int, pair<int,int>>>edges;
vector<int>nod[nmax];
int findpar(int x){
if(x != pardsu[x]){
pardsu[x] = findpar(pardsu[x]);
}
return pardsu[x];
}
void dfs(int s, int apr = 0, int par = 0){
in[s] = ++timp;
if(marked[s]) apr = s;
apropiat[s] = apr;
for(auto it : nod[s]){
if(it != par){
dfs(it, apr, s);
}
}
out[s] = ++timp;
}
bool is_lca(int x, int y){
if(in[x] < in[y] && out[x] > out[y]) return true;
else return false;
}
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
for(int i=1;i<=N+M;i++) grad[i] = 0;
last = N;
for(int i=1;i<=N;i++) pardsu[i] = i;
for(int i=0;i<M;i++){
U[i]++; V[i]++;
edges.push_back({W[i], {U[i], V[i]}});
}
sort(all(edges));
for(auto it : edges){
int x = findpar(it.sc.fr);
int y = findpar(it.sc.sc);
++last;
pardsu[y] = last;
pardsu[x] = last;
pardsu[last] = last;
val[last] = it.fr;
grad[it.sc.sc]++;
grad[it.sc.fr]++;
lca[x][0] = last;
lca[y][0] = last;
nod[last].push_back(x);
if(x != y) nod[last].push_back(y);
if(x == y){
marked[last] = 1;
}
else{
if(grad[it.sc.sc] >= 3 || grad[it.sc.fr] >= 3){
marked[last] = 1;
}
}
}
for(int i=1;i<=18;i++){
for(int j=1;j<=last;j++){
lca[j][i] = lca[lca[j][i-1]][i-1];
}
}
dfs(last);
}
int getMinimumFuelCapacity(int X, int Y) {
X++;
Y++;
int curr = X;
if(!is_lca(curr, Y)){
for(int i=18;i>=0;i--){
if(lca[curr][i] != 0 && !is_lca(lca[curr][i], Y)) curr = lca[curr][i];
}
curr = lca[curr][0];
}
if(!apropiat[curr]) return -1;
else{
return val[apropiat[curr]];
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
5 ms |
7380 KB |
Output is correct |
4 |
Correct |
5 ms |
7380 KB |
Output is correct |
5 |
Correct |
6 ms |
7508 KB |
Output is correct |
6 |
Correct |
6 ms |
7508 KB |
Output is correct |
7 |
Correct |
5 ms |
7636 KB |
Output is correct |
8 |
Correct |
7 ms |
7636 KB |
Output is correct |
9 |
Correct |
92 ms |
27608 KB |
Output is correct |
10 |
Correct |
117 ms |
32192 KB |
Output is correct |
11 |
Correct |
137 ms |
31872 KB |
Output is correct |
12 |
Correct |
147 ms |
33188 KB |
Output is correct |
13 |
Correct |
117 ms |
36288 KB |
Output is correct |
14 |
Correct |
104 ms |
27692 KB |
Output is correct |
15 |
Correct |
200 ms |
34084 KB |
Output is correct |
16 |
Correct |
186 ms |
33440 KB |
Output is correct |
17 |
Correct |
212 ms |
34980 KB |
Output is correct |
18 |
Correct |
264 ms |
38036 KB |
Output is correct |
19 |
Correct |
88 ms |
14844 KB |
Output is correct |
20 |
Correct |
199 ms |
35412 KB |
Output is correct |
21 |
Correct |
190 ms |
34624 KB |
Output is correct |
22 |
Correct |
237 ms |
36436 KB |
Output is correct |
23 |
Correct |
240 ms |
39460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
262 ms |
41136 KB |
Output is correct |
4 |
Correct |
255 ms |
42676 KB |
Output is correct |
5 |
Correct |
286 ms |
41736 KB |
Output is correct |
6 |
Correct |
264 ms |
42424 KB |
Output is correct |
7 |
Correct |
303 ms |
42148 KB |
Output is correct |
8 |
Correct |
256 ms |
40896 KB |
Output is correct |
9 |
Correct |
303 ms |
41964 KB |
Output is correct |
10 |
Correct |
281 ms |
40744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
5 ms |
7380 KB |
Output is correct |
4 |
Correct |
5 ms |
7380 KB |
Output is correct |
5 |
Correct |
6 ms |
7508 KB |
Output is correct |
6 |
Correct |
6 ms |
7508 KB |
Output is correct |
7 |
Correct |
5 ms |
7636 KB |
Output is correct |
8 |
Correct |
7 ms |
7636 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
5 ms |
7624 KB |
Output is correct |
11 |
Incorrect |
5 ms |
7636 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
5 ms |
7380 KB |
Output is correct |
5 |
Correct |
5 ms |
7380 KB |
Output is correct |
6 |
Correct |
6 ms |
7508 KB |
Output is correct |
7 |
Correct |
6 ms |
7508 KB |
Output is correct |
8 |
Correct |
5 ms |
7636 KB |
Output is correct |
9 |
Correct |
7 ms |
7636 KB |
Output is correct |
10 |
Correct |
92 ms |
27608 KB |
Output is correct |
11 |
Correct |
117 ms |
32192 KB |
Output is correct |
12 |
Correct |
137 ms |
31872 KB |
Output is correct |
13 |
Correct |
147 ms |
33188 KB |
Output is correct |
14 |
Correct |
117 ms |
36288 KB |
Output is correct |
15 |
Correct |
5 ms |
7624 KB |
Output is correct |
16 |
Incorrect |
5 ms |
7636 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
5 ms |
7380 KB |
Output is correct |
4 |
Correct |
5 ms |
7380 KB |
Output is correct |
5 |
Correct |
6 ms |
7508 KB |
Output is correct |
6 |
Correct |
6 ms |
7508 KB |
Output is correct |
7 |
Correct |
5 ms |
7636 KB |
Output is correct |
8 |
Correct |
7 ms |
7636 KB |
Output is correct |
9 |
Correct |
92 ms |
27608 KB |
Output is correct |
10 |
Correct |
117 ms |
32192 KB |
Output is correct |
11 |
Correct |
137 ms |
31872 KB |
Output is correct |
12 |
Correct |
147 ms |
33188 KB |
Output is correct |
13 |
Correct |
117 ms |
36288 KB |
Output is correct |
14 |
Correct |
104 ms |
27692 KB |
Output is correct |
15 |
Correct |
200 ms |
34084 KB |
Output is correct |
16 |
Correct |
186 ms |
33440 KB |
Output is correct |
17 |
Correct |
212 ms |
34980 KB |
Output is correct |
18 |
Correct |
264 ms |
38036 KB |
Output is correct |
19 |
Correct |
262 ms |
41136 KB |
Output is correct |
20 |
Correct |
255 ms |
42676 KB |
Output is correct |
21 |
Correct |
286 ms |
41736 KB |
Output is correct |
22 |
Correct |
264 ms |
42424 KB |
Output is correct |
23 |
Correct |
303 ms |
42148 KB |
Output is correct |
24 |
Correct |
256 ms |
40896 KB |
Output is correct |
25 |
Correct |
303 ms |
41964 KB |
Output is correct |
26 |
Correct |
281 ms |
40744 KB |
Output is correct |
27 |
Correct |
5 ms |
7624 KB |
Output is correct |
28 |
Incorrect |
5 ms |
7636 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
5 ms |
7380 KB |
Output is correct |
5 |
Correct |
5 ms |
7380 KB |
Output is correct |
6 |
Correct |
6 ms |
7508 KB |
Output is correct |
7 |
Correct |
6 ms |
7508 KB |
Output is correct |
8 |
Correct |
5 ms |
7636 KB |
Output is correct |
9 |
Correct |
7 ms |
7636 KB |
Output is correct |
10 |
Correct |
92 ms |
27608 KB |
Output is correct |
11 |
Correct |
117 ms |
32192 KB |
Output is correct |
12 |
Correct |
137 ms |
31872 KB |
Output is correct |
13 |
Correct |
147 ms |
33188 KB |
Output is correct |
14 |
Correct |
117 ms |
36288 KB |
Output is correct |
15 |
Correct |
104 ms |
27692 KB |
Output is correct |
16 |
Correct |
200 ms |
34084 KB |
Output is correct |
17 |
Correct |
186 ms |
33440 KB |
Output is correct |
18 |
Correct |
212 ms |
34980 KB |
Output is correct |
19 |
Correct |
264 ms |
38036 KB |
Output is correct |
20 |
Correct |
262 ms |
41136 KB |
Output is correct |
21 |
Correct |
255 ms |
42676 KB |
Output is correct |
22 |
Correct |
286 ms |
41736 KB |
Output is correct |
23 |
Correct |
264 ms |
42424 KB |
Output is correct |
24 |
Correct |
303 ms |
42148 KB |
Output is correct |
25 |
Correct |
256 ms |
40896 KB |
Output is correct |
26 |
Correct |
303 ms |
41964 KB |
Output is correct |
27 |
Correct |
281 ms |
40744 KB |
Output is correct |
28 |
Correct |
5 ms |
7624 KB |
Output is correct |
29 |
Incorrect |
5 ms |
7636 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |