#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
int par[MAXN], depth[MAXN], t[MAXN], third[MAXN], deg[MAXN];
int findSet(int u) {
return u == par[u] ? u : findSet(par[u]);
}
int h(int u) {
return u == par[u] ? 0 : 1 + h(par[u]);
}
bool merge(int u, int v, int w) {
u = findSet(u);
v = findSet(v);
if(u == v) return false;
if(depth[u] > depth[v]) swap(u, v);
par[u] = v;
depth[v] += depth[u] == depth[v];
third[v] = min(third[v], third[u]);
t[u] = w;
return true;
}
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
for(int i = 0; i < N; ++i) {
par[i] = i;
third[i] = INT_MAX;
}
vector<tuple<int, int, int>> edge(M);
for(int i = 0; i < M; ++i) edge[i] = {W[i], U[i], V[i]};
sort(edge.begin(), edge.end());
for(auto [w, u, v] : edge) {
++deg[u], ++deg[v];
bool e = merge(u, v, w);
if(!e || max(deg[u], deg[v]) > 2) {
int x = findSet(u);
third[x] = min(third[x], w);
}
}
}
int getMinimumFuelCapacity(int X, int Y) {
int Z = findSet(X);
int hx = h(X), hy = h(Y), ans = third[Z];
if(ans == INT_MAX) return -1;
while(hx != hy) {
if(hx > hy) {
ans = max(ans, t[X]);
X = par[X];
--hx;
} else {
ans = max(ans, t[Y]);
Y = par[Y];
--hy;
}
}
while(X != Y) {
ans = max(ans, max(t[X], t[Y]));
X = par[X];
Y = par[Y];
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
35 ms |
6208 KB |
Output is correct |
10 |
Correct |
44 ms |
7572 KB |
Output is correct |
11 |
Correct |
40 ms |
7428 KB |
Output is correct |
12 |
Correct |
42 ms |
7876 KB |
Output is correct |
13 |
Correct |
41 ms |
7488 KB |
Output is correct |
14 |
Correct |
39 ms |
6592 KB |
Output is correct |
15 |
Correct |
102 ms |
11440 KB |
Output is correct |
16 |
Correct |
103 ms |
11276 KB |
Output is correct |
17 |
Correct |
108 ms |
11728 KB |
Output is correct |
18 |
Correct |
97 ms |
11320 KB |
Output is correct |
19 |
Correct |
73 ms |
6396 KB |
Output is correct |
20 |
Correct |
109 ms |
11588 KB |
Output is correct |
21 |
Correct |
116 ms |
11900 KB |
Output is correct |
22 |
Correct |
127 ms |
12168 KB |
Output is correct |
23 |
Correct |
109 ms |
11824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
89 ms |
10828 KB |
Output is correct |
4 |
Correct |
93 ms |
10780 KB |
Output is correct |
5 |
Correct |
101 ms |
11212 KB |
Output is correct |
6 |
Correct |
90 ms |
10784 KB |
Output is correct |
7 |
Correct |
103 ms |
11176 KB |
Output is correct |
8 |
Correct |
100 ms |
10836 KB |
Output is correct |
9 |
Correct |
91 ms |
10860 KB |
Output is correct |
10 |
Correct |
99 ms |
10960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
35 ms |
6208 KB |
Output is correct |
11 |
Correct |
44 ms |
7572 KB |
Output is correct |
12 |
Correct |
40 ms |
7428 KB |
Output is correct |
13 |
Correct |
42 ms |
7876 KB |
Output is correct |
14 |
Correct |
41 ms |
7488 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
35 ms |
6208 KB |
Output is correct |
10 |
Correct |
44 ms |
7572 KB |
Output is correct |
11 |
Correct |
40 ms |
7428 KB |
Output is correct |
12 |
Correct |
42 ms |
7876 KB |
Output is correct |
13 |
Correct |
41 ms |
7488 KB |
Output is correct |
14 |
Correct |
39 ms |
6592 KB |
Output is correct |
15 |
Correct |
102 ms |
11440 KB |
Output is correct |
16 |
Correct |
103 ms |
11276 KB |
Output is correct |
17 |
Correct |
108 ms |
11728 KB |
Output is correct |
18 |
Correct |
97 ms |
11320 KB |
Output is correct |
19 |
Correct |
89 ms |
10828 KB |
Output is correct |
20 |
Correct |
93 ms |
10780 KB |
Output is correct |
21 |
Correct |
101 ms |
11212 KB |
Output is correct |
22 |
Correct |
90 ms |
10784 KB |
Output is correct |
23 |
Correct |
103 ms |
11176 KB |
Output is correct |
24 |
Correct |
100 ms |
10836 KB |
Output is correct |
25 |
Correct |
91 ms |
10860 KB |
Output is correct |
26 |
Correct |
99 ms |
10960 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
35 ms |
6208 KB |
Output is correct |
11 |
Correct |
44 ms |
7572 KB |
Output is correct |
12 |
Correct |
40 ms |
7428 KB |
Output is correct |
13 |
Correct |
42 ms |
7876 KB |
Output is correct |
14 |
Correct |
41 ms |
7488 KB |
Output is correct |
15 |
Correct |
39 ms |
6592 KB |
Output is correct |
16 |
Correct |
102 ms |
11440 KB |
Output is correct |
17 |
Correct |
103 ms |
11276 KB |
Output is correct |
18 |
Correct |
108 ms |
11728 KB |
Output is correct |
19 |
Correct |
97 ms |
11320 KB |
Output is correct |
20 |
Correct |
89 ms |
10828 KB |
Output is correct |
21 |
Correct |
93 ms |
10780 KB |
Output is correct |
22 |
Correct |
101 ms |
11212 KB |
Output is correct |
23 |
Correct |
90 ms |
10784 KB |
Output is correct |
24 |
Correct |
103 ms |
11176 KB |
Output is correct |
25 |
Correct |
100 ms |
10836 KB |
Output is correct |
26 |
Correct |
91 ms |
10860 KB |
Output is correct |
27 |
Correct |
99 ms |
10960 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |