#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, j, k, l) for(int i = (j); i < (k); i += (l))
#define FORD(i, j, k, l) for(int i = (j); i>=(k); i -= (l))
#define REP(i, n) FOR(i, 0, n, 1)
#define REPD(i, n) FORD(i, n, 0, 1)
#define pb push_back
typedef long long ll;
const int INF = (int)1e9 + 7;
const ll INFF = (ll)1e18;
int n, m;
vector<vector<array<int, 2>>> merge_history;// ked sa mensi mergne do vacsieho, zapamatam si ze mensi -> merge vacsi; max logn krat sa potom nieco mergne; usortim podla casov;
// when comp became good
vector<int> good_history;
struct dsu{
vector<int> p;
vector<bool> good;
vector<int> deg;
int counter = -1;
dsu(int n){
deg.resize(n, 0);
p.resize(n, -1);
good.resize(n, false);
}
int find(int x){
return (p[x] < 0 ? x : p[x] = find(p[x]));
}
bool same_set(int u, int v){
return find(u) == find(v);
}
bool join(int u, int v){
counter++;
deg[u]++; deg[v]++;
bool dgood = ((deg[u] > 2 || deg[v] > 2) ? true : false);
u = find(u);
v = find(v);
if(u == v){
if(!good[u]){
good_history[u] = counter;
good[u] = true;
}
return false;
}
if(-p[u] < -p[v])
swap(u, v);
if(!good[u] && (dgood || good[v])){
good[u] = true;
good_history[u] = counter;
}
p[u] += p[v]; p[v] = u;
merge_history[v].pb({counter, u});
return true;
}
};
vector<array<int, 3>> edges;
void init(int N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
n = N; m = M;
good_history.resize(n, -1);
merge_history.resize(n);
REP(i, m){
edges.pb({W[i], U[i], V[i]});
}
sort(edges.begin(), edges.end());
dsu d(n);
for(auto e : edges){
d.join(e[1], e[2]);
}
}
int getMinimumFuelCapacity(int u, int v) {
int ans = INF;
int curr_time = 0;
bool fn = false;
while(true){
array<int, 2> tm = {curr_time, -1};
auto it = lower_bound(merge_history[u].begin(), merge_history[u].end(), tm);
auto it2 = lower_bound(merge_history[v].begin(), merge_history[v].end(), tm);
int tf = INF;
int ts = INF;
if(it != merge_history[u].end())
tf = (*it)[0];
if(it2 != merge_history[v].end())
ts = (*it2)[0];
if(tf == ts && tf == INF)
break;
if(tf == ts){
curr_time = tf + 1;
u = (*it)[1];
v = (*it)[1];
}
else if(tf < ts){
curr_time = tf + 1;
u = (*it)[1];
}
else{
curr_time = ts + 1;
v = (*it2)[1];
}
if(u == v){
if(good_history[u] != -1){
fn = true;
ans = min(ans, max(edges[good_history[u]][0], edges[curr_time - 1][0]));
}
}
}
if(ans == INF)
return -1;
return ans;
}
Compilation message
swap.cpp: In function 'int getMinimumFuelCapacity(int, int)':
swap.cpp:75:10: warning: variable 'fn' set but not used [-Wunused-but-set-variable]
75 | bool fn = false;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
51 ms |
8928 KB |
Output is correct |
10 |
Correct |
60 ms |
10720 KB |
Output is correct |
11 |
Correct |
63 ms |
10476 KB |
Output is correct |
12 |
Correct |
84 ms |
11044 KB |
Output is correct |
13 |
Correct |
64 ms |
11064 KB |
Output is correct |
14 |
Correct |
65 ms |
9060 KB |
Output is correct |
15 |
Correct |
202 ms |
12772 KB |
Output is correct |
16 |
Correct |
203 ms |
12520 KB |
Output is correct |
17 |
Correct |
210 ms |
12864 KB |
Output is correct |
18 |
Correct |
173 ms |
12856 KB |
Output is correct |
19 |
Correct |
103 ms |
6176 KB |
Output is correct |
20 |
Correct |
201 ms |
14136 KB |
Output is correct |
21 |
Correct |
225 ms |
14752 KB |
Output is correct |
22 |
Correct |
208 ms |
14624 KB |
Output is correct |
23 |
Correct |
177 ms |
14652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
150 ms |
12808 KB |
Output is correct |
4 |
Correct |
161 ms |
13484 KB |
Output is correct |
5 |
Correct |
163 ms |
14128 KB |
Output is correct |
6 |
Correct |
164 ms |
13384 KB |
Output is correct |
7 |
Correct |
158 ms |
13408 KB |
Output is correct |
8 |
Correct |
159 ms |
13816 KB |
Output is correct |
9 |
Correct |
147 ms |
13240 KB |
Output is correct |
10 |
Correct |
149 ms |
13764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
304 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
2 ms |
316 KB |
Output is correct |
21 |
Correct |
1 ms |
304 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
460 KB |
Output is correct |
26 |
Correct |
2 ms |
460 KB |
Output is correct |
27 |
Correct |
2 ms |
332 KB |
Output is correct |
28 |
Correct |
2 ms |
440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
51 ms |
8928 KB |
Output is correct |
11 |
Correct |
60 ms |
10720 KB |
Output is correct |
12 |
Correct |
63 ms |
10476 KB |
Output is correct |
13 |
Correct |
84 ms |
11044 KB |
Output is correct |
14 |
Correct |
64 ms |
11064 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
332 KB |
Output is correct |
19 |
Correct |
2 ms |
332 KB |
Output is correct |
20 |
Correct |
2 ms |
304 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
316 KB |
Output is correct |
26 |
Correct |
1 ms |
304 KB |
Output is correct |
27 |
Correct |
2 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
2 ms |
332 KB |
Output is correct |
30 |
Correct |
2 ms |
460 KB |
Output is correct |
31 |
Correct |
2 ms |
460 KB |
Output is correct |
32 |
Correct |
2 ms |
332 KB |
Output is correct |
33 |
Correct |
2 ms |
440 KB |
Output is correct |
34 |
Correct |
9 ms |
1904 KB |
Output is correct |
35 |
Correct |
62 ms |
12192 KB |
Output is correct |
36 |
Correct |
66 ms |
12196 KB |
Output is correct |
37 |
Correct |
61 ms |
12192 KB |
Output is correct |
38 |
Correct |
72 ms |
12064 KB |
Output is correct |
39 |
Correct |
61 ms |
12048 KB |
Output is correct |
40 |
Correct |
55 ms |
11056 KB |
Output is correct |
41 |
Correct |
64 ms |
12580 KB |
Output is correct |
42 |
Correct |
63 ms |
12188 KB |
Output is correct |
43 |
Correct |
78 ms |
12200 KB |
Output is correct |
44 |
Correct |
66 ms |
12600 KB |
Output is correct |
45 |
Correct |
89 ms |
13764 KB |
Output is correct |
46 |
Correct |
74 ms |
12216 KB |
Output is correct |
47 |
Correct |
72 ms |
12184 KB |
Output is correct |
48 |
Correct |
66 ms |
12712 KB |
Output is correct |
49 |
Correct |
84 ms |
9912 KB |
Output is correct |
50 |
Correct |
59 ms |
7316 KB |
Output is correct |
51 |
Correct |
72 ms |
11760 KB |
Output is correct |
52 |
Correct |
98 ms |
15980 KB |
Output is correct |
53 |
Correct |
109 ms |
17044 KB |
Output is correct |
54 |
Correct |
125 ms |
17844 KB |
Output is correct |
55 |
Correct |
67 ms |
12216 KB |
Output is correct |
56 |
Correct |
104 ms |
16620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
51 ms |
8928 KB |
Output is correct |
10 |
Correct |
60 ms |
10720 KB |
Output is correct |
11 |
Correct |
63 ms |
10476 KB |
Output is correct |
12 |
Correct |
84 ms |
11044 KB |
Output is correct |
13 |
Correct |
64 ms |
11064 KB |
Output is correct |
14 |
Correct |
65 ms |
9060 KB |
Output is correct |
15 |
Correct |
202 ms |
12772 KB |
Output is correct |
16 |
Correct |
203 ms |
12520 KB |
Output is correct |
17 |
Correct |
210 ms |
12864 KB |
Output is correct |
18 |
Correct |
173 ms |
12856 KB |
Output is correct |
19 |
Correct |
150 ms |
12808 KB |
Output is correct |
20 |
Correct |
161 ms |
13484 KB |
Output is correct |
21 |
Correct |
163 ms |
14128 KB |
Output is correct |
22 |
Correct |
164 ms |
13384 KB |
Output is correct |
23 |
Correct |
158 ms |
13408 KB |
Output is correct |
24 |
Correct |
159 ms |
13816 KB |
Output is correct |
25 |
Correct |
147 ms |
13240 KB |
Output is correct |
26 |
Correct |
149 ms |
13764 KB |
Output is correct |
27 |
Correct |
2 ms |
332 KB |
Output is correct |
28 |
Correct |
2 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
2 ms |
332 KB |
Output is correct |
31 |
Correct |
2 ms |
332 KB |
Output is correct |
32 |
Correct |
2 ms |
304 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
9 ms |
1904 KB |
Output is correct |
37 |
Correct |
62 ms |
12192 KB |
Output is correct |
38 |
Correct |
66 ms |
12196 KB |
Output is correct |
39 |
Correct |
61 ms |
12192 KB |
Output is correct |
40 |
Correct |
72 ms |
12064 KB |
Output is correct |
41 |
Correct |
61 ms |
12048 KB |
Output is correct |
42 |
Correct |
55 ms |
11056 KB |
Output is correct |
43 |
Correct |
64 ms |
12580 KB |
Output is correct |
44 |
Correct |
63 ms |
12188 KB |
Output is correct |
45 |
Correct |
78 ms |
12200 KB |
Output is correct |
46 |
Correct |
66 ms |
12600 KB |
Output is correct |
47 |
Correct |
18 ms |
2264 KB |
Output is correct |
48 |
Correct |
203 ms |
16328 KB |
Output is correct |
49 |
Correct |
213 ms |
16288 KB |
Output is correct |
50 |
Correct |
266 ms |
16312 KB |
Output is correct |
51 |
Correct |
269 ms |
16196 KB |
Output is correct |
52 |
Correct |
223 ms |
16348 KB |
Output is correct |
53 |
Correct |
187 ms |
14252 KB |
Output is correct |
54 |
Correct |
255 ms |
17284 KB |
Output is correct |
55 |
Correct |
288 ms |
16280 KB |
Output is correct |
56 |
Correct |
192 ms |
16312 KB |
Output is correct |
57 |
Correct |
264 ms |
17176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
51 ms |
8928 KB |
Output is correct |
11 |
Correct |
60 ms |
10720 KB |
Output is correct |
12 |
Correct |
63 ms |
10476 KB |
Output is correct |
13 |
Correct |
84 ms |
11044 KB |
Output is correct |
14 |
Correct |
64 ms |
11064 KB |
Output is correct |
15 |
Correct |
65 ms |
9060 KB |
Output is correct |
16 |
Correct |
202 ms |
12772 KB |
Output is correct |
17 |
Correct |
203 ms |
12520 KB |
Output is correct |
18 |
Correct |
210 ms |
12864 KB |
Output is correct |
19 |
Correct |
173 ms |
12856 KB |
Output is correct |
20 |
Correct |
150 ms |
12808 KB |
Output is correct |
21 |
Correct |
161 ms |
13484 KB |
Output is correct |
22 |
Correct |
163 ms |
14128 KB |
Output is correct |
23 |
Correct |
164 ms |
13384 KB |
Output is correct |
24 |
Correct |
158 ms |
13408 KB |
Output is correct |
25 |
Correct |
159 ms |
13816 KB |
Output is correct |
26 |
Correct |
147 ms |
13240 KB |
Output is correct |
27 |
Correct |
149 ms |
13764 KB |
Output is correct |
28 |
Correct |
2 ms |
332 KB |
Output is correct |
29 |
Correct |
2 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
2 ms |
332 KB |
Output is correct |
32 |
Correct |
2 ms |
332 KB |
Output is correct |
33 |
Correct |
2 ms |
304 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
9 ms |
1904 KB |
Output is correct |
38 |
Correct |
62 ms |
12192 KB |
Output is correct |
39 |
Correct |
66 ms |
12196 KB |
Output is correct |
40 |
Correct |
61 ms |
12192 KB |
Output is correct |
41 |
Correct |
72 ms |
12064 KB |
Output is correct |
42 |
Correct |
61 ms |
12048 KB |
Output is correct |
43 |
Correct |
55 ms |
11056 KB |
Output is correct |
44 |
Correct |
64 ms |
12580 KB |
Output is correct |
45 |
Correct |
63 ms |
12188 KB |
Output is correct |
46 |
Correct |
78 ms |
12200 KB |
Output is correct |
47 |
Correct |
66 ms |
12600 KB |
Output is correct |
48 |
Correct |
18 ms |
2264 KB |
Output is correct |
49 |
Correct |
203 ms |
16328 KB |
Output is correct |
50 |
Correct |
213 ms |
16288 KB |
Output is correct |
51 |
Correct |
266 ms |
16312 KB |
Output is correct |
52 |
Correct |
269 ms |
16196 KB |
Output is correct |
53 |
Correct |
223 ms |
16348 KB |
Output is correct |
54 |
Correct |
187 ms |
14252 KB |
Output is correct |
55 |
Correct |
255 ms |
17284 KB |
Output is correct |
56 |
Correct |
288 ms |
16280 KB |
Output is correct |
57 |
Correct |
192 ms |
16312 KB |
Output is correct |
58 |
Correct |
264 ms |
17176 KB |
Output is correct |
59 |
Correct |
103 ms |
6176 KB |
Output is correct |
60 |
Correct |
201 ms |
14136 KB |
Output is correct |
61 |
Correct |
225 ms |
14752 KB |
Output is correct |
62 |
Correct |
208 ms |
14624 KB |
Output is correct |
63 |
Correct |
177 ms |
14652 KB |
Output is correct |
64 |
Correct |
1 ms |
332 KB |
Output is correct |
65 |
Correct |
2 ms |
316 KB |
Output is correct |
66 |
Correct |
1 ms |
304 KB |
Output is correct |
67 |
Correct |
2 ms |
332 KB |
Output is correct |
68 |
Correct |
1 ms |
332 KB |
Output is correct |
69 |
Correct |
2 ms |
332 KB |
Output is correct |
70 |
Correct |
2 ms |
460 KB |
Output is correct |
71 |
Correct |
2 ms |
460 KB |
Output is correct |
72 |
Correct |
2 ms |
332 KB |
Output is correct |
73 |
Correct |
2 ms |
440 KB |
Output is correct |
74 |
Correct |
89 ms |
13764 KB |
Output is correct |
75 |
Correct |
74 ms |
12216 KB |
Output is correct |
76 |
Correct |
72 ms |
12184 KB |
Output is correct |
77 |
Correct |
66 ms |
12712 KB |
Output is correct |
78 |
Correct |
84 ms |
9912 KB |
Output is correct |
79 |
Correct |
59 ms |
7316 KB |
Output is correct |
80 |
Correct |
72 ms |
11760 KB |
Output is correct |
81 |
Correct |
98 ms |
15980 KB |
Output is correct |
82 |
Correct |
109 ms |
17044 KB |
Output is correct |
83 |
Correct |
125 ms |
17844 KB |
Output is correct |
84 |
Correct |
67 ms |
12216 KB |
Output is correct |
85 |
Correct |
104 ms |
16620 KB |
Output is correct |
86 |
Correct |
63 ms |
6060 KB |
Output is correct |
87 |
Correct |
220 ms |
16300 KB |
Output is correct |
88 |
Correct |
219 ms |
16304 KB |
Output is correct |
89 |
Correct |
186 ms |
16784 KB |
Output is correct |
90 |
Correct |
166 ms |
13732 KB |
Output is correct |
91 |
Correct |
185 ms |
13672 KB |
Output is correct |
92 |
Correct |
179 ms |
16560 KB |
Output is correct |
93 |
Correct |
247 ms |
19852 KB |
Output is correct |
94 |
Correct |
253 ms |
20740 KB |
Output is correct |
95 |
Correct |
292 ms |
21816 KB |
Output is correct |
96 |
Correct |
165 ms |
16428 KB |
Output is correct |
97 |
Correct |
212 ms |
18824 KB |
Output is correct |