#include "swap.h"
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_map>
#include <cmath>
#include <climits>
#include <algorithm>
#include <random>
#include <queue>
#include <deque>
#include <iomanip>
#include <string>
#include <tuple>
#include <bitset>
#include <chrono>
#include <ctime>
#include <fstream>
#include <stack>
using namespace std;
using ll = long long;
vector<int> minKox, skizb, verj, par, sizes, weight;
map<pair<int, int>, int> mp;
vector<pair<int, pair<int, int>>> edges;
int get(int u, bool f, int w) {
if (par[u] == u) return u;
if (f && weight[u] <= w) return get(par[u], f, w);
else if (f) return u;
return get(par[u], f, w);
}
void union_sets(int u, int v, int w) {
int a = get(u, 0, 0), b = get(v, 0, 0);
if (a == b) return;
if (sizes[a] < sizes[b]) swap(a, b);
par[b] = a;
sizes[a] += sizes[b];
weight[b] = w;
}
void mst() {
sort(edges.begin(), edges.end());
for (int i = 0; i < edges.size(); ++i) {
int w = edges[i].first, u = edges[i].second.first, v = edges[i].second.second;
int p1 = get(u, 0, 0), p2 = get(v, 0, 0);
if (p1 == p2) {
skizb[p1] = verj[p1] = -1;
minKox[p1] = min(minKox[p1], w);
continue;
}
if (sizes[p1] < sizes[p2]) swap(p1, p2), swap(u, v);
union_sets(p1, p2, w);
if (minKox[p1] != 2e9 || minKox[p2] != 2e9) {
skizb[p1] = verj[p1] = -1;
minKox[p1] = min(minKox[p1], w);
continue;
}
if (skizb[p1] == u && skizb[p2] == v) skizb[p1] = verj[p2];
else if (skizb[p1] == u && verj[p2] == v) skizb[p1] = skizb[p2];
else if (verj[p1] == u && verj[p2] == v) verj[p1] = skizb[p2];
else if (verj[p1] == u && skizb[p2] == v) verj[p1] = verj[p2];
else {
verj[p1] = skizb[p1] = -1;
minKox[p1] = min(minKox[p1], w);
}
}
}
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
minKox = vector<int>(N, 2e9);
weight = skizb = verj = par = sizes = vector<int>(N);
for (int i = 0; i < N; ++i) {
skizb[i] = verj[i] = par[i] = i;
sizes[i] = 1;
}
for (int i = 0; i < M; ++i) {
mp[{U[i], V[i]}] = mp[{V[i], U[i]}] = W[i];
edges.push_back({ W[i], {U[i], V[i]} });
}
mst();
}
bool check(int x, int y, int w) {
int p1 = get(x, 1, w), p2 = get(y, 1, w);
if (p1 != p2) return 0;
if (minKox[p1] > w) return 0;
return 1;
}
int getMinimumFuelCapacity(int X, int Y) {
ll l = 0, r = 1e9 + 5;
while (l + 1 < r) {
ll m = (l + r) / 2;
if (check(X, Y, m)) r = m;
else l = m;
}
if (r == 1e9 + 5) return -1;
else return r;
return 0;
}
Compilation message
swap.cpp: In function 'void mst()':
swap.cpp:48:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for (int i = 0; i < edges.size(); ++i) {
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
104 ms |
14744 KB |
Output is correct |
10 |
Correct |
132 ms |
17932 KB |
Output is correct |
11 |
Correct |
139 ms |
17740 KB |
Output is correct |
12 |
Correct |
153 ms |
18676 KB |
Output is correct |
13 |
Correct |
152 ms |
18672 KB |
Output is correct |
14 |
Correct |
125 ms |
14740 KB |
Output is correct |
15 |
Correct |
334 ms |
19832 KB |
Output is correct |
16 |
Correct |
357 ms |
19376 KB |
Output is correct |
17 |
Correct |
355 ms |
20440 KB |
Output is correct |
18 |
Correct |
260 ms |
20472 KB |
Output is correct |
19 |
Correct |
174 ms |
6620 KB |
Output is correct |
20 |
Correct |
354 ms |
25264 KB |
Output is correct |
21 |
Correct |
347 ms |
24912 KB |
Output is correct |
22 |
Correct |
361 ms |
26220 KB |
Output is correct |
23 |
Correct |
262 ms |
26220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
266 ms |
20696 KB |
Output is correct |
4 |
Correct |
223 ms |
21476 KB |
Output is correct |
5 |
Correct |
247 ms |
21244 KB |
Output is correct |
6 |
Correct |
219 ms |
21400 KB |
Output is correct |
7 |
Correct |
234 ms |
21372 KB |
Output is correct |
8 |
Correct |
246 ms |
20544 KB |
Output is correct |
9 |
Correct |
219 ms |
21172 KB |
Output is correct |
10 |
Correct |
258 ms |
20512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
480 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
2 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
436 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
3 ms |
596 KB |
Output is correct |
26 |
Correct |
2 ms |
596 KB |
Output is correct |
27 |
Correct |
2 ms |
440 KB |
Output is correct |
28 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
104 ms |
14744 KB |
Output is correct |
11 |
Correct |
132 ms |
17932 KB |
Output is correct |
12 |
Correct |
139 ms |
17740 KB |
Output is correct |
13 |
Correct |
153 ms |
18676 KB |
Output is correct |
14 |
Correct |
152 ms |
18672 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
480 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
436 KB |
Output is correct |
29 |
Correct |
2 ms |
596 KB |
Output is correct |
30 |
Correct |
3 ms |
596 KB |
Output is correct |
31 |
Correct |
2 ms |
596 KB |
Output is correct |
32 |
Correct |
2 ms |
440 KB |
Output is correct |
33 |
Correct |
2 ms |
596 KB |
Output is correct |
34 |
Correct |
13 ms |
2772 KB |
Output is correct |
35 |
Correct |
139 ms |
18684 KB |
Output is correct |
36 |
Correct |
145 ms |
18620 KB |
Output is correct |
37 |
Correct |
192 ms |
18668 KB |
Output is correct |
38 |
Correct |
163 ms |
18408 KB |
Output is correct |
39 |
Correct |
132 ms |
18304 KB |
Output is correct |
40 |
Correct |
122 ms |
16808 KB |
Output is correct |
41 |
Correct |
150 ms |
18628 KB |
Output is correct |
42 |
Correct |
158 ms |
18680 KB |
Output is correct |
43 |
Correct |
140 ms |
18600 KB |
Output is correct |
44 |
Correct |
154 ms |
18620 KB |
Output is correct |
45 |
Correct |
233 ms |
28752 KB |
Output is correct |
46 |
Correct |
135 ms |
20444 KB |
Output is correct |
47 |
Correct |
136 ms |
20500 KB |
Output is correct |
48 |
Correct |
166 ms |
22344 KB |
Output is correct |
49 |
Correct |
246 ms |
28376 KB |
Output is correct |
50 |
Correct |
188 ms |
22024 KB |
Output is correct |
51 |
Correct |
206 ms |
25112 KB |
Output is correct |
52 |
Correct |
266 ms |
31916 KB |
Output is correct |
53 |
Correct |
311 ms |
35148 KB |
Output is correct |
54 |
Correct |
346 ms |
38692 KB |
Output is correct |
55 |
Correct |
136 ms |
20480 KB |
Output is correct |
56 |
Correct |
284 ms |
33956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
104 ms |
14744 KB |
Output is correct |
10 |
Correct |
132 ms |
17932 KB |
Output is correct |
11 |
Correct |
139 ms |
17740 KB |
Output is correct |
12 |
Correct |
153 ms |
18676 KB |
Output is correct |
13 |
Correct |
152 ms |
18672 KB |
Output is correct |
14 |
Correct |
125 ms |
14740 KB |
Output is correct |
15 |
Correct |
334 ms |
19832 KB |
Output is correct |
16 |
Correct |
357 ms |
19376 KB |
Output is correct |
17 |
Correct |
355 ms |
20440 KB |
Output is correct |
18 |
Correct |
260 ms |
20472 KB |
Output is correct |
19 |
Correct |
266 ms |
20696 KB |
Output is correct |
20 |
Correct |
223 ms |
21476 KB |
Output is correct |
21 |
Correct |
247 ms |
21244 KB |
Output is correct |
22 |
Correct |
219 ms |
21400 KB |
Output is correct |
23 |
Correct |
234 ms |
21372 KB |
Output is correct |
24 |
Correct |
246 ms |
20544 KB |
Output is correct |
25 |
Correct |
219 ms |
21172 KB |
Output is correct |
26 |
Correct |
258 ms |
20512 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 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 |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
13 ms |
2772 KB |
Output is correct |
37 |
Correct |
139 ms |
18684 KB |
Output is correct |
38 |
Correct |
145 ms |
18620 KB |
Output is correct |
39 |
Correct |
192 ms |
18668 KB |
Output is correct |
40 |
Correct |
163 ms |
18408 KB |
Output is correct |
41 |
Correct |
132 ms |
18304 KB |
Output is correct |
42 |
Correct |
122 ms |
16808 KB |
Output is correct |
43 |
Correct |
150 ms |
18628 KB |
Output is correct |
44 |
Correct |
158 ms |
18680 KB |
Output is correct |
45 |
Correct |
140 ms |
18600 KB |
Output is correct |
46 |
Correct |
154 ms |
18620 KB |
Output is correct |
47 |
Correct |
36 ms |
2912 KB |
Output is correct |
48 |
Correct |
355 ms |
21292 KB |
Output is correct |
49 |
Correct |
336 ms |
21480 KB |
Output is correct |
50 |
Correct |
332 ms |
21124 KB |
Output is correct |
51 |
Correct |
354 ms |
21260 KB |
Output is correct |
52 |
Correct |
313 ms |
20100 KB |
Output is correct |
53 |
Correct |
269 ms |
16036 KB |
Output is correct |
54 |
Correct |
367 ms |
21852 KB |
Output is correct |
55 |
Correct |
356 ms |
21220 KB |
Output is correct |
56 |
Correct |
244 ms |
21240 KB |
Output is correct |
57 |
Correct |
325 ms |
21880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
104 ms |
14744 KB |
Output is correct |
11 |
Correct |
132 ms |
17932 KB |
Output is correct |
12 |
Correct |
139 ms |
17740 KB |
Output is correct |
13 |
Correct |
153 ms |
18676 KB |
Output is correct |
14 |
Correct |
152 ms |
18672 KB |
Output is correct |
15 |
Correct |
125 ms |
14740 KB |
Output is correct |
16 |
Correct |
334 ms |
19832 KB |
Output is correct |
17 |
Correct |
357 ms |
19376 KB |
Output is correct |
18 |
Correct |
355 ms |
20440 KB |
Output is correct |
19 |
Correct |
260 ms |
20472 KB |
Output is correct |
20 |
Correct |
266 ms |
20696 KB |
Output is correct |
21 |
Correct |
223 ms |
21476 KB |
Output is correct |
22 |
Correct |
247 ms |
21244 KB |
Output is correct |
23 |
Correct |
219 ms |
21400 KB |
Output is correct |
24 |
Correct |
234 ms |
21372 KB |
Output is correct |
25 |
Correct |
246 ms |
20544 KB |
Output is correct |
26 |
Correct |
219 ms |
21172 KB |
Output is correct |
27 |
Correct |
258 ms |
20512 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
13 ms |
2772 KB |
Output is correct |
38 |
Correct |
139 ms |
18684 KB |
Output is correct |
39 |
Correct |
145 ms |
18620 KB |
Output is correct |
40 |
Correct |
192 ms |
18668 KB |
Output is correct |
41 |
Correct |
163 ms |
18408 KB |
Output is correct |
42 |
Correct |
132 ms |
18304 KB |
Output is correct |
43 |
Correct |
122 ms |
16808 KB |
Output is correct |
44 |
Correct |
150 ms |
18628 KB |
Output is correct |
45 |
Correct |
158 ms |
18680 KB |
Output is correct |
46 |
Correct |
140 ms |
18600 KB |
Output is correct |
47 |
Correct |
154 ms |
18620 KB |
Output is correct |
48 |
Correct |
36 ms |
2912 KB |
Output is correct |
49 |
Correct |
355 ms |
21292 KB |
Output is correct |
50 |
Correct |
336 ms |
21480 KB |
Output is correct |
51 |
Correct |
332 ms |
21124 KB |
Output is correct |
52 |
Correct |
354 ms |
21260 KB |
Output is correct |
53 |
Correct |
313 ms |
20100 KB |
Output is correct |
54 |
Correct |
269 ms |
16036 KB |
Output is correct |
55 |
Correct |
367 ms |
21852 KB |
Output is correct |
56 |
Correct |
356 ms |
21220 KB |
Output is correct |
57 |
Correct |
244 ms |
21240 KB |
Output is correct |
58 |
Correct |
325 ms |
21880 KB |
Output is correct |
59 |
Correct |
174 ms |
6620 KB |
Output is correct |
60 |
Correct |
354 ms |
25264 KB |
Output is correct |
61 |
Correct |
347 ms |
24912 KB |
Output is correct |
62 |
Correct |
361 ms |
26220 KB |
Output is correct |
63 |
Correct |
262 ms |
26220 KB |
Output is correct |
64 |
Correct |
1 ms |
480 KB |
Output is correct |
65 |
Correct |
1 ms |
468 KB |
Output is correct |
66 |
Correct |
1 ms |
468 KB |
Output is correct |
67 |
Correct |
2 ms |
596 KB |
Output is correct |
68 |
Correct |
1 ms |
436 KB |
Output is correct |
69 |
Correct |
2 ms |
596 KB |
Output is correct |
70 |
Correct |
3 ms |
596 KB |
Output is correct |
71 |
Correct |
2 ms |
596 KB |
Output is correct |
72 |
Correct |
2 ms |
440 KB |
Output is correct |
73 |
Correct |
2 ms |
596 KB |
Output is correct |
74 |
Correct |
233 ms |
28752 KB |
Output is correct |
75 |
Correct |
135 ms |
20444 KB |
Output is correct |
76 |
Correct |
136 ms |
20500 KB |
Output is correct |
77 |
Correct |
166 ms |
22344 KB |
Output is correct |
78 |
Correct |
246 ms |
28376 KB |
Output is correct |
79 |
Correct |
188 ms |
22024 KB |
Output is correct |
80 |
Correct |
206 ms |
25112 KB |
Output is correct |
81 |
Correct |
266 ms |
31916 KB |
Output is correct |
82 |
Correct |
311 ms |
35148 KB |
Output is correct |
83 |
Correct |
346 ms |
38692 KB |
Output is correct |
84 |
Correct |
136 ms |
20480 KB |
Output is correct |
85 |
Correct |
284 ms |
33956 KB |
Output is correct |
86 |
Correct |
97 ms |
11044 KB |
Output is correct |
87 |
Correct |
339 ms |
25308 KB |
Output is correct |
88 |
Correct |
345 ms |
25288 KB |
Output is correct |
89 |
Correct |
286 ms |
26096 KB |
Output is correct |
90 |
Correct |
379 ms |
33920 KB |
Output is correct |
91 |
Correct |
414 ms |
31672 KB |
Output is correct |
92 |
Correct |
323 ms |
29120 KB |
Output is correct |
93 |
Correct |
465 ms |
36212 KB |
Output is correct |
94 |
Correct |
452 ms |
39264 KB |
Output is correct |
95 |
Correct |
566 ms |
42484 KB |
Output is correct |
96 |
Correct |
256 ms |
25456 KB |
Output is correct |
97 |
Correct |
350 ms |
32308 KB |
Output is correct |