#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(v) v.begin(), v.end()
#define endl '\n'
#define pl(var) " [" << #var << ": " << (var) << "] "
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "[" << p.first << ", " << p.second << "]"; }
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) { cout << "["; for(int i = 0; i < (int)v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";}
template<typename A, typename B> istream& operator>>(istream& cin, pair<A, B> &p) { cin >> p.first; return cin >> p.second; }
struct DSU { // Sorry UFDS
vector<int> p, s, degree;
vector<bool> swappable;
int components;
void init(int n) {
p = vector<int>(n);
iota(all(p), 0);
s = vector<int>(n, 1);
swappable = vector<bool>(n, 0);
degree = vector<int>(n, 0);
components = n;
}
int get(int x) {
return p[x] = (p[x] == x ? x : get(p[x]));
}
void unite(int a, int b) {
degree[a]++;
degree[b]++;
bool _swappable = 0;
if (degree[a] >= 3 || degree[b] >= 3) _swappable = 1;
a = get(a);
b = get(b);
if (a == b) {
swappable[a] = 1;
return;
}
if (s[a] > s[b]) swap(a, b);
p[a] = b;
s[b] += s[a];
swappable[b] = (swappable[a] | swappable[b] | _swappable);
components--;
}
bool same_set(int a, int b) {
return get(a) == get(b);
}
int size(int x) {
return s[get(x)];
}
bool is_swappable(int a, int b) {
return same_set(a, b) && swappable[get(a)] && swappable[get(b)];
}
};
int n, m;
vector<vector<pair<int, int>>> adj;
vector<pair<int, pair<int, int>>> edges;
int inf = 1e9 + 7;
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
n = N;
m = M;
adj.resize(n);
for (int i = 0; i < m; i++) {
adj[U[i]].emplace_back(V[i], W[i]);
adj[V[i]].emplace_back(U[i], W[i]);
edges.emplace_back(W[i], make_pair(U[i], V[i]));
}
sort(all(edges));
}
int getMinimumFuelCapacity(int x, int y) {
int ptr = 0;
DSU dsu;
dsu.init(n);
// cout << pl(x) << pl(y) << endl;
while (!dsu.is_swappable(x, y) && ptr < m) {
// cout << pl(ptr) << pl(dsu.swappable) << pl(dsu.p) << pl(dsu.degree) << endl;
// cout << pl(edges[ptr]) << endl;
dsu.unite(edges[ptr].second.first, edges[ptr].second.second);
ptr++;
}
// cout << pl(ptr) << pl(dsu.swappable) << pl(dsu.p) << pl(dsu.degree) << endl;
if (!dsu.is_swappable(x, y)) return -1;
// if (ptr == m && !dsu.swappable[dsu) return -1;
return edges[ptr - 1].first;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
49 ms |
7832 KB |
Output is correct |
10 |
Correct |
96 ms |
9036 KB |
Output is correct |
11 |
Correct |
90 ms |
8896 KB |
Output is correct |
12 |
Correct |
94 ms |
9308 KB |
Output is correct |
13 |
Correct |
75 ms |
9220 KB |
Output is correct |
14 |
Execution timed out |
2087 ms |
7936 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Execution timed out |
2095 ms |
11748 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
308 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 |
324 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
444 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
476 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
316 KB |
Output is correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
49 ms |
7832 KB |
Output is correct |
11 |
Correct |
96 ms |
9036 KB |
Output is correct |
12 |
Correct |
90 ms |
8896 KB |
Output is correct |
13 |
Correct |
94 ms |
9308 KB |
Output is correct |
14 |
Correct |
75 ms |
9220 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
308 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 |
324 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
444 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
476 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
316 KB |
Output is correct |
33 |
Correct |
2 ms |
468 KB |
Output is correct |
34 |
Correct |
9 ms |
1932 KB |
Output is correct |
35 |
Correct |
92 ms |
11104 KB |
Output is correct |
36 |
Correct |
83 ms |
11120 KB |
Output is correct |
37 |
Correct |
91 ms |
11108 KB |
Output is correct |
38 |
Correct |
92 ms |
11044 KB |
Output is correct |
39 |
Correct |
76 ms |
10984 KB |
Output is correct |
40 |
Correct |
75 ms |
10300 KB |
Output is correct |
41 |
Correct |
84 ms |
11396 KB |
Output is correct |
42 |
Correct |
76 ms |
11136 KB |
Output is correct |
43 |
Correct |
85 ms |
11132 KB |
Output is correct |
44 |
Correct |
100 ms |
11928 KB |
Output is correct |
45 |
Correct |
103 ms |
15932 KB |
Output is correct |
46 |
Correct |
82 ms |
11068 KB |
Output is correct |
47 |
Correct |
85 ms |
11128 KB |
Output is correct |
48 |
Correct |
104 ms |
12048 KB |
Output is correct |
49 |
Correct |
70 ms |
13564 KB |
Output is correct |
50 |
Correct |
60 ms |
10328 KB |
Output is correct |
51 |
Correct |
93 ms |
12984 KB |
Output is correct |
52 |
Correct |
123 ms |
17324 KB |
Output is correct |
53 |
Correct |
135 ms |
18308 KB |
Output is correct |
54 |
Correct |
168 ms |
19860 KB |
Output is correct |
55 |
Correct |
79 ms |
11140 KB |
Output is correct |
56 |
Correct |
150 ms |
17840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
49 ms |
7832 KB |
Output is correct |
10 |
Correct |
96 ms |
9036 KB |
Output is correct |
11 |
Correct |
90 ms |
8896 KB |
Output is correct |
12 |
Correct |
94 ms |
9308 KB |
Output is correct |
13 |
Correct |
75 ms |
9220 KB |
Output is correct |
14 |
Execution timed out |
2087 ms |
7936 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
49 ms |
7832 KB |
Output is correct |
11 |
Correct |
96 ms |
9036 KB |
Output is correct |
12 |
Correct |
90 ms |
8896 KB |
Output is correct |
13 |
Correct |
94 ms |
9308 KB |
Output is correct |
14 |
Correct |
75 ms |
9220 KB |
Output is correct |
15 |
Execution timed out |
2087 ms |
7936 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |