#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) 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) {
| ~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
108 ms |
14696 KB |
Output is correct |
10 |
Correct |
134 ms |
17880 KB |
Output is correct |
11 |
Correct |
133 ms |
17608 KB |
Output is correct |
12 |
Correct |
139 ms |
18628 KB |
Output is correct |
13 |
Correct |
134 ms |
18620 KB |
Output is correct |
14 |
Correct |
122 ms |
14740 KB |
Output is correct |
15 |
Correct |
338 ms |
20036 KB |
Output is correct |
16 |
Correct |
328 ms |
23628 KB |
Output is correct |
17 |
Correct |
349 ms |
24812 KB |
Output is correct |
18 |
Correct |
253 ms |
24808 KB |
Output is correct |
19 |
Incorrect |
156 ms |
7492 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
218 ms |
20728 KB |
Output is correct |
4 |
Correct |
220 ms |
21448 KB |
Output is correct |
5 |
Correct |
230 ms |
21184 KB |
Output is correct |
6 |
Correct |
220 ms |
21344 KB |
Output is correct |
7 |
Correct |
229 ms |
21348 KB |
Output is correct |
8 |
Correct |
221 ms |
20672 KB |
Output is correct |
9 |
Correct |
223 ms |
21144 KB |
Output is correct |
10 |
Correct |
219 ms |
20436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
108 ms |
14696 KB |
Output is correct |
10 |
Correct |
134 ms |
17880 KB |
Output is correct |
11 |
Correct |
133 ms |
17608 KB |
Output is correct |
12 |
Correct |
139 ms |
18628 KB |
Output is correct |
13 |
Correct |
134 ms |
18620 KB |
Output is correct |
14 |
Correct |
122 ms |
14740 KB |
Output is correct |
15 |
Correct |
338 ms |
20036 KB |
Output is correct |
16 |
Correct |
328 ms |
23628 KB |
Output is correct |
17 |
Correct |
349 ms |
24812 KB |
Output is correct |
18 |
Correct |
253 ms |
24808 KB |
Output is correct |
19 |
Correct |
218 ms |
20728 KB |
Output is correct |
20 |
Correct |
220 ms |
21448 KB |
Output is correct |
21 |
Correct |
230 ms |
21184 KB |
Output is correct |
22 |
Correct |
220 ms |
21344 KB |
Output is correct |
23 |
Correct |
229 ms |
21348 KB |
Output is correct |
24 |
Correct |
221 ms |
20672 KB |
Output is correct |
25 |
Correct |
223 ms |
21144 KB |
Output is correct |
26 |
Correct |
219 ms |
20436 KB |
Output is correct |
27 |
Correct |
1 ms |
456 KB |
Output is correct |
28 |
Correct |
1 ms |
448 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 |
452 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 |
14 ms |
3032 KB |
Output is correct |
37 |
Correct |
142 ms |
20456 KB |
Output is correct |
38 |
Correct |
158 ms |
20492 KB |
Output is correct |
39 |
Correct |
139 ms |
20464 KB |
Output is correct |
40 |
Correct |
156 ms |
20204 KB |
Output is correct |
41 |
Correct |
136 ms |
20040 KB |
Output is correct |
42 |
Correct |
120 ms |
18456 KB |
Output is correct |
43 |
Correct |
139 ms |
20804 KB |
Output is correct |
44 |
Correct |
138 ms |
20444 KB |
Output is correct |
45 |
Correct |
136 ms |
20456 KB |
Output is correct |
46 |
Correct |
141 ms |
20752 KB |
Output is correct |
47 |
Correct |
31 ms |
3276 KB |
Output is correct |
48 |
Correct |
349 ms |
25292 KB |
Output is correct |
49 |
Correct |
338 ms |
25420 KB |
Output is correct |
50 |
Correct |
327 ms |
25448 KB |
Output is correct |
51 |
Correct |
336 ms |
25204 KB |
Output is correct |
52 |
Correct |
335 ms |
24264 KB |
Output is correct |
53 |
Correct |
268 ms |
19664 KB |
Output is correct |
54 |
Correct |
374 ms |
26208 KB |
Output is correct |
55 |
Correct |
370 ms |
25284 KB |
Output is correct |
56 |
Correct |
258 ms |
25316 KB |
Output is correct |
57 |
Correct |
330 ms |
26308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |