#include "swap.h"
#include <bits/stdc++.h>
#include <vector>
using namespace std;
long long n, m, i, j, sz[200001], p[200001], d[200001], mx[200001], szm[200001], kek[200001];
multiset<pair<long long, long long>> ms;
int mxx;
bool st1, st2;
vector<pair<long long, long long>> g[200001];
vector<pair<long long, pair<long long, long long>>> gg;
long long get(long long x) {
return x == p[x] ? x : p[x] = get(p[x]);
}
void merge(long long xx, long long yy) {
long long x = get(xx), y = get(yy);
mx[x] = max({mx[x], d[x], d[xx]});
mx[y] = max({mx[y], d[y], d[yy]});
mx[x] = max(mx[x], mx[y]);
mx[y] = max(mx[y], mx[x]);
szm[x]++;
if (x == y) return;
if (sz[x] < sz[y]) swap(x, y);
p[y] = x;
sz[x] += sz[y];
szm[x] += szm[y];
mx[x] = max(mx[x], mx[y]);
mx[y] = max(mx[y], mx[x]);
}
void init(int N, int M,
std::vector<int> U, std::vector<int> V, std::vector<int> W) {
n = N, m = M;
st2 = 1;
for (long long i = 0; i < M; i++) {
g[U[i]].push_back({V[i], W[i]});
g[V[i]].push_back({U[i], W[i]});
if (U[i]) {
st2 = 0;
}
gg.push_back({W[i], {U[i], V[i]}});
kek[V[i]] = W[i];
mxx = max(mxx, W[i]);
}
if (st2) {
for (long long i = 0; i < N; i++) ms.insert({kek[i], i});
}
st1 = 1;
for (long long i = 0; i < N; i++) {
if (g[i].size() > 2) st1 = 0;
}
sort(gg.begin(), gg.end());
}
bool check(long long md, long long x, long long y) {
if (md < 0 || md >= gg.size()) return 0;
for (long long i = 0; i < n; i++) sz[i] = 1, p[i] = i, d[i] = mx[i] = szm[i] = 0;
for (long long i = 0; i <= md; i++) {
d[gg[i].second.first]++;
d[gg[i].second.second]++;
merge(gg[i].second.first, gg[i].second.second);
}
if (get(x) != get(y)) return 0;
if (mx[get(x)] <= 2 && szm[get(x)] < sz[get(x)]) return 0;
return 1;
}
int getMinimumFuelCapacity(int X, int Y) {
if (st1) {
if (n == m) return mxx;
return -1;
}
if (st2) {
if (ms.size() > 2) {
ms.erase({kek[X], X});
ms.erase({kek[Y], Y});
long long ans = max({kek[X], kek[Y], (gg[2].first)});
ms.insert({kek[X], X});
ms.insert({kek[Y], Y});
return ans;
}
return -1;
}
long long l = 0, r = (int)gg.size(), md, ans = 0;
while (l <= r) {
md = (l + r) / 2;
if (check(md, X, Y)) ans = md, r = md - 1;
else l = md + 1;
}
if (!check(ans, X, Y)) return -1;
return gg[ans].first;
}
Compilation message
swap.cpp: In function 'bool check(long long int, long long int, long long int)':
swap.cpp:57:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | if (md < 0 || md >= gg.size()) return 0;
| ~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
2 ms |
6744 KB |
Output is correct |
8 |
Correct |
2 ms |
6744 KB |
Output is correct |
9 |
Correct |
36 ms |
16316 KB |
Output is correct |
10 |
Correct |
44 ms |
16128 KB |
Output is correct |
11 |
Correct |
46 ms |
16388 KB |
Output is correct |
12 |
Correct |
47 ms |
16652 KB |
Output is correct |
13 |
Correct |
51 ms |
16644 KB |
Output is correct |
14 |
Correct |
42 ms |
16388 KB |
Output is correct |
15 |
Correct |
86 ms |
18320 KB |
Output is correct |
16 |
Correct |
84 ms |
17720 KB |
Output is correct |
17 |
Correct |
86 ms |
18540 KB |
Output is correct |
18 |
Correct |
93 ms |
18232 KB |
Output is correct |
19 |
Correct |
42 ms |
11660 KB |
Output is correct |
20 |
Correct |
85 ms |
19392 KB |
Output is correct |
21 |
Correct |
96 ms |
19252 KB |
Output is correct |
22 |
Correct |
92 ms |
19748 KB |
Output is correct |
23 |
Correct |
94 ms |
20336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
256 ms |
25048 KB |
Output is correct |
4 |
Correct |
263 ms |
25060 KB |
Output is correct |
5 |
Correct |
267 ms |
25704 KB |
Output is correct |
6 |
Correct |
254 ms |
29584 KB |
Output is correct |
7 |
Correct |
261 ms |
29096 KB |
Output is correct |
8 |
Correct |
306 ms |
28440 KB |
Output is correct |
9 |
Correct |
262 ms |
28800 KB |
Output is correct |
10 |
Correct |
284 ms |
28104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
2 ms |
6744 KB |
Output is correct |
8 |
Correct |
2 ms |
6744 KB |
Output is correct |
9 |
Correct |
2 ms |
12892 KB |
Output is correct |
10 |
Correct |
3 ms |
12892 KB |
Output is correct |
11 |
Correct |
3 ms |
12892 KB |
Output is correct |
12 |
Correct |
4 ms |
12892 KB |
Output is correct |
13 |
Correct |
3 ms |
12892 KB |
Output is correct |
14 |
Correct |
3 ms |
12892 KB |
Output is correct |
15 |
Correct |
3 ms |
12892 KB |
Output is correct |
16 |
Correct |
3 ms |
12892 KB |
Output is correct |
17 |
Correct |
3 ms |
12892 KB |
Output is correct |
18 |
Correct |
3 ms |
12892 KB |
Output is correct |
19 |
Correct |
3 ms |
13036 KB |
Output is correct |
20 |
Correct |
3 ms |
12892 KB |
Output is correct |
21 |
Correct |
3 ms |
12892 KB |
Output is correct |
22 |
Correct |
3 ms |
12892 KB |
Output is correct |
23 |
Correct |
3 ms |
12892 KB |
Output is correct |
24 |
Correct |
4 ms |
12892 KB |
Output is correct |
25 |
Correct |
4 ms |
13144 KB |
Output is correct |
26 |
Correct |
4 ms |
12892 KB |
Output is correct |
27 |
Correct |
3 ms |
12892 KB |
Output is correct |
28 |
Correct |
3 ms |
12928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12892 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6744 KB |
Output is correct |
9 |
Correct |
2 ms |
6744 KB |
Output is correct |
10 |
Correct |
36 ms |
16316 KB |
Output is correct |
11 |
Correct |
44 ms |
16128 KB |
Output is correct |
12 |
Correct |
46 ms |
16388 KB |
Output is correct |
13 |
Correct |
47 ms |
16652 KB |
Output is correct |
14 |
Correct |
51 ms |
16644 KB |
Output is correct |
15 |
Correct |
3 ms |
12892 KB |
Output is correct |
16 |
Correct |
3 ms |
12892 KB |
Output is correct |
17 |
Correct |
4 ms |
12892 KB |
Output is correct |
18 |
Correct |
3 ms |
12892 KB |
Output is correct |
19 |
Correct |
3 ms |
12892 KB |
Output is correct |
20 |
Correct |
3 ms |
12892 KB |
Output is correct |
21 |
Correct |
3 ms |
12892 KB |
Output is correct |
22 |
Correct |
3 ms |
12892 KB |
Output is correct |
23 |
Correct |
3 ms |
12892 KB |
Output is correct |
24 |
Correct |
3 ms |
13036 KB |
Output is correct |
25 |
Correct |
3 ms |
12892 KB |
Output is correct |
26 |
Correct |
3 ms |
12892 KB |
Output is correct |
27 |
Correct |
3 ms |
12892 KB |
Output is correct |
28 |
Correct |
3 ms |
12892 KB |
Output is correct |
29 |
Correct |
4 ms |
12892 KB |
Output is correct |
30 |
Correct |
4 ms |
13144 KB |
Output is correct |
31 |
Correct |
4 ms |
12892 KB |
Output is correct |
32 |
Correct |
3 ms |
12892 KB |
Output is correct |
33 |
Correct |
3 ms |
12928 KB |
Output is correct |
34 |
Correct |
13 ms |
14232 KB |
Output is correct |
35 |
Correct |
363 ms |
22952 KB |
Output is correct |
36 |
Correct |
334 ms |
22900 KB |
Output is correct |
37 |
Correct |
346 ms |
22456 KB |
Output is correct |
38 |
Correct |
337 ms |
22136 KB |
Output is correct |
39 |
Correct |
322 ms |
22404 KB |
Output is correct |
40 |
Correct |
315 ms |
21628 KB |
Output is correct |
41 |
Correct |
359 ms |
22304 KB |
Output is correct |
42 |
Correct |
325 ms |
22816 KB |
Output is correct |
43 |
Correct |
192 ms |
22476 KB |
Output is correct |
44 |
Correct |
341 ms |
23200 KB |
Output is correct |
45 |
Correct |
321 ms |
27688 KB |
Output is correct |
46 |
Correct |
326 ms |
22452 KB |
Output is correct |
47 |
Correct |
344 ms |
22472 KB |
Output is correct |
48 |
Correct |
289 ms |
23448 KB |
Output is correct |
49 |
Correct |
71 ms |
28284 KB |
Output is correct |
50 |
Correct |
66 ms |
23816 KB |
Output is correct |
51 |
Correct |
208 ms |
24600 KB |
Output is correct |
52 |
Correct |
412 ms |
27672 KB |
Output is correct |
53 |
Correct |
345 ms |
29124 KB |
Output is correct |
54 |
Correct |
403 ms |
31728 KB |
Output is correct |
55 |
Correct |
218 ms |
22376 KB |
Output is correct |
56 |
Correct |
414 ms |
29468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
2 ms |
6744 KB |
Output is correct |
8 |
Correct |
2 ms |
6744 KB |
Output is correct |
9 |
Correct |
36 ms |
16316 KB |
Output is correct |
10 |
Correct |
44 ms |
16128 KB |
Output is correct |
11 |
Correct |
46 ms |
16388 KB |
Output is correct |
12 |
Correct |
47 ms |
16652 KB |
Output is correct |
13 |
Correct |
51 ms |
16644 KB |
Output is correct |
14 |
Correct |
42 ms |
16388 KB |
Output is correct |
15 |
Correct |
86 ms |
18320 KB |
Output is correct |
16 |
Correct |
84 ms |
17720 KB |
Output is correct |
17 |
Correct |
86 ms |
18540 KB |
Output is correct |
18 |
Correct |
93 ms |
18232 KB |
Output is correct |
19 |
Correct |
256 ms |
25048 KB |
Output is correct |
20 |
Correct |
263 ms |
25060 KB |
Output is correct |
21 |
Correct |
267 ms |
25704 KB |
Output is correct |
22 |
Correct |
254 ms |
29584 KB |
Output is correct |
23 |
Correct |
261 ms |
29096 KB |
Output is correct |
24 |
Correct |
306 ms |
28440 KB |
Output is correct |
25 |
Correct |
262 ms |
28800 KB |
Output is correct |
26 |
Correct |
284 ms |
28104 KB |
Output is correct |
27 |
Correct |
3 ms |
12892 KB |
Output is correct |
28 |
Correct |
3 ms |
12892 KB |
Output is correct |
29 |
Correct |
4 ms |
12892 KB |
Output is correct |
30 |
Correct |
3 ms |
12892 KB |
Output is correct |
31 |
Correct |
3 ms |
12892 KB |
Output is correct |
32 |
Correct |
3 ms |
12892 KB |
Output is correct |
33 |
Correct |
3 ms |
12892 KB |
Output is correct |
34 |
Correct |
3 ms |
12892 KB |
Output is correct |
35 |
Correct |
3 ms |
12892 KB |
Output is correct |
36 |
Correct |
13 ms |
14232 KB |
Output is correct |
37 |
Correct |
363 ms |
22952 KB |
Output is correct |
38 |
Correct |
334 ms |
22900 KB |
Output is correct |
39 |
Correct |
346 ms |
22456 KB |
Output is correct |
40 |
Correct |
337 ms |
22136 KB |
Output is correct |
41 |
Correct |
322 ms |
22404 KB |
Output is correct |
42 |
Correct |
315 ms |
21628 KB |
Output is correct |
43 |
Correct |
359 ms |
22304 KB |
Output is correct |
44 |
Correct |
325 ms |
22816 KB |
Output is correct |
45 |
Correct |
192 ms |
22476 KB |
Output is correct |
46 |
Correct |
341 ms |
23200 KB |
Output is correct |
47 |
Execution timed out |
2045 ms |
14992 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12892 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6744 KB |
Output is correct |
9 |
Correct |
2 ms |
6744 KB |
Output is correct |
10 |
Correct |
36 ms |
16316 KB |
Output is correct |
11 |
Correct |
44 ms |
16128 KB |
Output is correct |
12 |
Correct |
46 ms |
16388 KB |
Output is correct |
13 |
Correct |
47 ms |
16652 KB |
Output is correct |
14 |
Correct |
51 ms |
16644 KB |
Output is correct |
15 |
Correct |
42 ms |
16388 KB |
Output is correct |
16 |
Correct |
86 ms |
18320 KB |
Output is correct |
17 |
Correct |
84 ms |
17720 KB |
Output is correct |
18 |
Correct |
86 ms |
18540 KB |
Output is correct |
19 |
Correct |
93 ms |
18232 KB |
Output is correct |
20 |
Correct |
256 ms |
25048 KB |
Output is correct |
21 |
Correct |
263 ms |
25060 KB |
Output is correct |
22 |
Correct |
267 ms |
25704 KB |
Output is correct |
23 |
Correct |
254 ms |
29584 KB |
Output is correct |
24 |
Correct |
261 ms |
29096 KB |
Output is correct |
25 |
Correct |
306 ms |
28440 KB |
Output is correct |
26 |
Correct |
262 ms |
28800 KB |
Output is correct |
27 |
Correct |
284 ms |
28104 KB |
Output is correct |
28 |
Correct |
3 ms |
12892 KB |
Output is correct |
29 |
Correct |
3 ms |
12892 KB |
Output is correct |
30 |
Correct |
4 ms |
12892 KB |
Output is correct |
31 |
Correct |
3 ms |
12892 KB |
Output is correct |
32 |
Correct |
3 ms |
12892 KB |
Output is correct |
33 |
Correct |
3 ms |
12892 KB |
Output is correct |
34 |
Correct |
3 ms |
12892 KB |
Output is correct |
35 |
Correct |
3 ms |
12892 KB |
Output is correct |
36 |
Correct |
3 ms |
12892 KB |
Output is correct |
37 |
Correct |
13 ms |
14232 KB |
Output is correct |
38 |
Correct |
363 ms |
22952 KB |
Output is correct |
39 |
Correct |
334 ms |
22900 KB |
Output is correct |
40 |
Correct |
346 ms |
22456 KB |
Output is correct |
41 |
Correct |
337 ms |
22136 KB |
Output is correct |
42 |
Correct |
322 ms |
22404 KB |
Output is correct |
43 |
Correct |
315 ms |
21628 KB |
Output is correct |
44 |
Correct |
359 ms |
22304 KB |
Output is correct |
45 |
Correct |
325 ms |
22816 KB |
Output is correct |
46 |
Correct |
192 ms |
22476 KB |
Output is correct |
47 |
Correct |
341 ms |
23200 KB |
Output is correct |
48 |
Execution timed out |
2045 ms |
14992 KB |
Time limit exceeded |
49 |
Halted |
0 ms |
0 KB |
- |