#include "swap.h"
#include<bits/stdc++.h>
using namespace std;
const int INF = 1e9;
struct DSU {
vector<int> e;
vector<vector<int>> done;
vector<int> cycle;
vector<bool> ok;
void init(int n) {
e = vector<int>(n, -1);
done.resize(n);
cycle = vector<int>(n, INF);
ok = vector<bool>(n, false);
for (int i = 0; i < n; i++) {
done[i].push_back(i);
}
}
int get(int x) {
return e[x] < 0 ? x : e[x] = get(e[x]);
}
bool same_set(int a, int b) {
return get(a) == get(b);
}
int size(int x) {
return -e[get(x)];
}
bool unite(int x, int y, int w) {
cycle[x] = min(cycle[x], w);
cycle[y] = min(cycle[y], w);
x = get(x), y = get(y);
if (x == y) return false;
if (e[x] > e[y]) swap(x, y);
for (int v : done[y]) {
done[x].push_back(v);
}
e[x] += e[y]; e[y] = x;
return true;
}
void purge(int x, int w) {
x = get(x);
for (int v : done[x]) {
cycle[v] = w;
ok[v] = true;
}
done[x].clear();
}
};
DSU D;
void init(int N, int M,
std::vector<int> U, std::vector<int> V, std::vector<int> W) {
vector<array<int, 3>> edges(M);
for (int i = 0; i < M; i++) {
edges[i][0] = W[i];
edges[i][1] = U[i];
edges[i][2] = V[i];
}
sort(edges.begin(), edges.end());
D.init(N);
for (auto e : edges) {
int u = e[1];
int v = e[2];
int w = e[0];
if (D.same_set(u, v)) {
D.purge(u, w);
} else {
D.unite(u, v, w);
}
}
}
int getMinimumFuelCapacity(int X, int Y) {
int ans = max(D.cycle[X], D.cycle[Y]);
if (ans >= INF || D.ok[X] == false && D.ok[Y] == false) {
return -1;
} else {
return ans;
}
return 0;
}
Compilation message
swap.cpp: In function 'int getMinimumFuelCapacity(int, int)':
swap.cpp:75:40: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
75 | if (ans >= INF || D.ok[X] == false && D.ok[Y] == false) {
| ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
2 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
504 KB |
Output is correct |
9 |
Correct |
70 ms |
12768 KB |
Output is correct |
10 |
Correct |
86 ms |
15200 KB |
Output is correct |
11 |
Correct |
84 ms |
14940 KB |
Output is correct |
12 |
Correct |
88 ms |
15844 KB |
Output is correct |
13 |
Correct |
67 ms |
13092 KB |
Output is correct |
14 |
Correct |
74 ms |
13280 KB |
Output is correct |
15 |
Correct |
154 ms |
19552 KB |
Output is correct |
16 |
Correct |
146 ms |
19044 KB |
Output is correct |
17 |
Correct |
155 ms |
19940 KB |
Output is correct |
18 |
Correct |
138 ms |
16992 KB |
Output is correct |
19 |
Correct |
67 ms |
7592 KB |
Output is correct |
20 |
Correct |
152 ms |
19168 KB |
Output is correct |
21 |
Correct |
151 ms |
19156 KB |
Output is correct |
22 |
Correct |
156 ms |
20144 KB |
Output is correct |
23 |
Correct |
140 ms |
17328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Incorrect |
146 ms |
15720 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
504 KB |
Output is correct |
10 |
Incorrect |
1 ms |
492 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
504 KB |
Output is correct |
10 |
Correct |
70 ms |
12768 KB |
Output is correct |
11 |
Correct |
86 ms |
15200 KB |
Output is correct |
12 |
Correct |
84 ms |
14940 KB |
Output is correct |
13 |
Correct |
88 ms |
15844 KB |
Output is correct |
14 |
Correct |
67 ms |
13092 KB |
Output is correct |
15 |
Incorrect |
1 ms |
492 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
2 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
504 KB |
Output is correct |
9 |
Correct |
70 ms |
12768 KB |
Output is correct |
10 |
Correct |
86 ms |
15200 KB |
Output is correct |
11 |
Correct |
84 ms |
14940 KB |
Output is correct |
12 |
Correct |
88 ms |
15844 KB |
Output is correct |
13 |
Correct |
67 ms |
13092 KB |
Output is correct |
14 |
Correct |
74 ms |
13280 KB |
Output is correct |
15 |
Correct |
154 ms |
19552 KB |
Output is correct |
16 |
Correct |
146 ms |
19044 KB |
Output is correct |
17 |
Correct |
155 ms |
19940 KB |
Output is correct |
18 |
Correct |
138 ms |
16992 KB |
Output is correct |
19 |
Incorrect |
146 ms |
15720 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
504 KB |
Output is correct |
10 |
Correct |
70 ms |
12768 KB |
Output is correct |
11 |
Correct |
86 ms |
15200 KB |
Output is correct |
12 |
Correct |
84 ms |
14940 KB |
Output is correct |
13 |
Correct |
88 ms |
15844 KB |
Output is correct |
14 |
Correct |
67 ms |
13092 KB |
Output is correct |
15 |
Correct |
74 ms |
13280 KB |
Output is correct |
16 |
Correct |
154 ms |
19552 KB |
Output is correct |
17 |
Correct |
146 ms |
19044 KB |
Output is correct |
18 |
Correct |
155 ms |
19940 KB |
Output is correct |
19 |
Correct |
138 ms |
16992 KB |
Output is correct |
20 |
Incorrect |
146 ms |
15720 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |