#include "swap.h"
#include <vector>
#include <algorithm>
using namespace std;
#define pii pair<int, int>
#define f first
#define s second
#define pb push_back
#define oo 1000000001
struct edge{
int u, v;
int cost;
bool operator< (edge e) const{
return cost < e.cost;
}
};
vector<int> pr;
vector<edge> ed;
vector<int> deg;
vector<vector<int>> cmp;
vector<vector<pii>> arr;
vector<int> when;
void Union(edge e){
deg[e.u]++; deg[e.v]++;
int x = pr[e.u];
int y = pr[e.v];
if (x == y){
when[x] = min(when[x], e.cost);
return;
}
if (cmp[x].size() < cmp[y].size()) swap(x, y);
for (auto node : cmp[y]){
cmp[x].pb(node);
pr[node] = x;
arr[node].pb({x, e.cost});
}
cmp[y].clear();
when[x] = min(when[x], when[y]);
if (deg[e.u] >= 3 || deg[e.v] >= 3) when[x] = min(when[x], e.cost);
}
void init(int n, int m, vector<int> u, vector<int> v, vector<int> w) {
pr.resize(n);
ed.resize(m);
deg.resize(n);
cmp.resize(n);
arr.resize(n);
when.resize(n);
for (int i = 0; i < m; ++i) {
ed[i].u = u[i];
ed[i].v = v[i];
ed[i].cost = w[i];
}
for (int i = 0; i < n; ++i) {
pr[i] = i;
cmp[i].pb(i);
deg[i] = 0;
arr[i].pb({i, 0});
when[i] = oo;
}
sort(ed.begin(), ed.end());
for (int i = 0; i < m; ++i) {
Union(ed[i]);
}
}
int getMinimumFuelCapacity(int x, int y) {
int ans = -1;
if (when[pr[x]] == oo) return -1;
int cmpx = x, cmpy = y;
int a = 0, b = 0;
while (cmpx != cmpy){
if (a == arr[x].size()){
cmpy = arr[y][b].f;
ans = max(ans, arr[y][b++].s);
} else if (b == arr[y].size()){
cmpx = arr[x][a].f;
ans = max(ans, arr[x][a++].s);
} else if (arr[x][a].s < arr[y][b].s){
cmpx = arr[x][a].f;
ans = max(ans, arr[x][a++].s);
} else{
cmpy = arr[y][b].f;
ans = max(ans, arr[y][b++].s);
}
}
ans = max(ans, when[cmpx]);
return ans;
}
Compilation message
swap.cpp: In function 'int getMinimumFuelCapacity(int, int)':
swap.cpp:95:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | if (a == arr[x].size()){
| ~~^~~~~~~~~~~~~~~~
swap.cpp:98:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | } else if (b == arr[y].size()){
| ~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
182 ms |
22604 KB |
Output is correct |
10 |
Correct |
218 ms |
27188 KB |
Output is correct |
11 |
Correct |
223 ms |
26716 KB |
Output is correct |
12 |
Correct |
264 ms |
28412 KB |
Output is correct |
13 |
Correct |
160 ms |
20968 KB |
Output is correct |
14 |
Correct |
180 ms |
22876 KB |
Output is correct |
15 |
Correct |
324 ms |
31876 KB |
Output is correct |
16 |
Correct |
284 ms |
31052 KB |
Output is correct |
17 |
Correct |
313 ms |
32896 KB |
Output is correct |
18 |
Correct |
199 ms |
25024 KB |
Output is correct |
19 |
Incorrect |
103 ms |
9756 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
188 ms |
22404 KB |
Output is correct |
4 |
Correct |
210 ms |
23024 KB |
Output is correct |
5 |
Correct |
205 ms |
23036 KB |
Output is correct |
6 |
Correct |
203 ms |
22956 KB |
Output is correct |
7 |
Correct |
217 ms |
23148 KB |
Output is correct |
8 |
Correct |
195 ms |
22340 KB |
Output is correct |
9 |
Correct |
194 ms |
22792 KB |
Output is correct |
10 |
Correct |
197 ms |
22260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
420 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
182 ms |
22604 KB |
Output is correct |
11 |
Correct |
218 ms |
27188 KB |
Output is correct |
12 |
Correct |
223 ms |
26716 KB |
Output is correct |
13 |
Correct |
264 ms |
28412 KB |
Output is correct |
14 |
Correct |
160 ms |
20968 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
420 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
182 ms |
22604 KB |
Output is correct |
10 |
Correct |
218 ms |
27188 KB |
Output is correct |
11 |
Correct |
223 ms |
26716 KB |
Output is correct |
12 |
Correct |
264 ms |
28412 KB |
Output is correct |
13 |
Correct |
160 ms |
20968 KB |
Output is correct |
14 |
Correct |
180 ms |
22876 KB |
Output is correct |
15 |
Correct |
324 ms |
31876 KB |
Output is correct |
16 |
Correct |
284 ms |
31052 KB |
Output is correct |
17 |
Correct |
313 ms |
32896 KB |
Output is correct |
18 |
Correct |
199 ms |
25024 KB |
Output is correct |
19 |
Correct |
188 ms |
22404 KB |
Output is correct |
20 |
Correct |
210 ms |
23024 KB |
Output is correct |
21 |
Correct |
205 ms |
23036 KB |
Output is correct |
22 |
Correct |
203 ms |
22956 KB |
Output is correct |
23 |
Correct |
217 ms |
23148 KB |
Output is correct |
24 |
Correct |
195 ms |
22340 KB |
Output is correct |
25 |
Correct |
194 ms |
22792 KB |
Output is correct |
26 |
Correct |
197 ms |
22260 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
460 KB |
Output is correct |
29 |
Correct |
1 ms |
420 KB |
Output is correct |
30 |
Correct |
1 ms |
460 KB |
Output is correct |
31 |
Correct |
1 ms |
460 KB |
Output is correct |
32 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
182 ms |
22604 KB |
Output is correct |
11 |
Correct |
218 ms |
27188 KB |
Output is correct |
12 |
Correct |
223 ms |
26716 KB |
Output is correct |
13 |
Correct |
264 ms |
28412 KB |
Output is correct |
14 |
Correct |
160 ms |
20968 KB |
Output is correct |
15 |
Correct |
180 ms |
22876 KB |
Output is correct |
16 |
Correct |
324 ms |
31876 KB |
Output is correct |
17 |
Correct |
284 ms |
31052 KB |
Output is correct |
18 |
Correct |
313 ms |
32896 KB |
Output is correct |
19 |
Correct |
199 ms |
25024 KB |
Output is correct |
20 |
Correct |
188 ms |
22404 KB |
Output is correct |
21 |
Correct |
210 ms |
23024 KB |
Output is correct |
22 |
Correct |
205 ms |
23036 KB |
Output is correct |
23 |
Correct |
203 ms |
22956 KB |
Output is correct |
24 |
Correct |
217 ms |
23148 KB |
Output is correct |
25 |
Correct |
195 ms |
22340 KB |
Output is correct |
26 |
Correct |
194 ms |
22792 KB |
Output is correct |
27 |
Correct |
197 ms |
22260 KB |
Output is correct |
28 |
Correct |
1 ms |
460 KB |
Output is correct |
29 |
Correct |
1 ms |
460 KB |
Output is correct |
30 |
Correct |
1 ms |
420 KB |
Output is correct |
31 |
Correct |
1 ms |
460 KB |
Output is correct |
32 |
Correct |
1 ms |
460 KB |
Output is correct |
33 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |