#pragma GCC optimize("Ofast,O3,unroll-loops")
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int MaxN = 100002;
const int MaxM = 200002;
int N, M;
const int blockLength = 850;
struct Set{
int parent;
bool line;
pair<int, int> tail;
Set(){};
Set(int i){
parent = i;
line = true;
tail = make_pair(i, i);
}
};
Set t[MaxN];
int MAGIC;
bool flag[MaxN];
Set mem[MaxN][MaxM / blockLength + 3];
int re[MaxN];
int _nxt;
int leader(int x){
if (MAGIC != -1 && !flag[x]){
t[x] = (MAGIC == 0 ? Set(x) : mem[x][MAGIC - 1]);
flag[x] = true;
re[_nxt++] = x;
}
return t[x].parent == x ? x : t[x].parent = leader(t[x].parent);
}
void unite(int x, int y){
int X = x, Y = y;
x = leader(x);
y = leader(y);
if (rng() & 1){
swap(x, y);
swap(X, Y);
}
if (x == y){
t[y].line = false;
return;
}
t[x].parent = y;
if (!t[x].line){
t[y].line = false;
return;
}
if (!t[y].line) return;
if (t[x].tail.first != X && t[x].tail.second != X){
t[y].line = false;
return;
}
if (t[y].tail.first != Y && t[y].tail.second != Y){
t[y].line = false;
return;
}
pair<int, int> new_tail;
new_tail.first = t[x].tail.first ^ t[x].tail.second ^ X;
new_tail.second = t[y].tail.first ^ t[y].tail.second ^ Y;
t[y].tail = new_tail;
}
int el[MaxM], er[MaxM];
int blockCount;
bool sub1, sub2;
tuple<int, int, int> edges[MaxM];
int foo[MaxN];
void init(int _N, int _M, vector<int> U, vector<int> V, vector<int> W) {
N = _N, M = _M;
vector<int> deg(N, 0);
sub2 = true;
for (int i = 0; i < M; i++){
edges[i] = make_tuple(W[i], U[i], V[i]);
if (U[i] != 0) sub2 = false;
deg[U[i]]++;
deg[V[i]]++;
foo[V[i]] = W[i];
}
sub1 = true;
for (int i = 0; i < N; i++){
if (deg[i] > 2) sub1 = false;
}
sort(edges, edges + M);
for (int i = 0; i < N; i++) t[i] = Set(i);
MAGIC = -1;
blockCount = 0;
for (int i = 0; i < M; i++){
int u = get<1>(edges[i]), v = get<2>(edges[i]);
unite(u, v);
if ((i + 1) % blockLength == 0 || i == M - 1){
int id = blockCount++;
el[id] = 0, er[id] = i;
if (id > 0) el[id] = er[id - 1] + 1;
for (int j = 0; j < N; j++) mem[j][id] = t[leader(j)];
}
}
for (int i = 0; i < N; i++) flag[i] = false;
}
bool can(int x, int y){
x = leader(x);
y = leader(y);
return x == y && !t[x].line;
}
int getMinimumFuelCapacity(int X, int Y) {
if (sub1) return (M == N ? get<0>(edges[M - 1]) : -1);
if (sub2){
if (X == 0){
return max(foo[Y], get<0>(edges[2]));
} else {
multiset<int> s;
for (int i = 0; i < 3; i++) s.insert(get<0>(edges[i]));
if (s.find(foo[X]) != s.end()) s.erase(s.find(foo[X]));
if (s.find(foo[Y]) != s.end()) s.erase(s.find(foo[Y]));
return max({foo[X], foo[Y], *s.begin()});
}
}
int id = 0;
while (id < blockCount){
int xx = mem[X][id].parent, yy = mem[Y][id].parent;
if (xx == yy && !mem[xx][id].line){
break;
} else {
id++;
}
}
if (id == blockCount) return -1;
MAGIC = id;
int ans = -1;
_nxt = 0;
for (int i = el[id]; i <= er[id]; i++){
auto [w, u, v] = edges[i];
unite(u, v);
if (can(X, Y)){
ans = w;
break;
}
}
for (int i = 0; i < _nxt; i++) flag[re[i]] = false;
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
167 ms |
374424 KB |
Output is correct |
2 |
Correct |
170 ms |
374484 KB |
Output is correct |
3 |
Correct |
171 ms |
374412 KB |
Output is correct |
4 |
Correct |
168 ms |
374468 KB |
Output is correct |
5 |
Correct |
170 ms |
374404 KB |
Output is correct |
6 |
Correct |
171 ms |
374472 KB |
Output is correct |
7 |
Correct |
169 ms |
374412 KB |
Output is correct |
8 |
Correct |
174 ms |
374436 KB |
Output is correct |
9 |
Correct |
318 ms |
377876 KB |
Output is correct |
10 |
Correct |
399 ms |
378640 KB |
Output is correct |
11 |
Correct |
441 ms |
378608 KB |
Output is correct |
12 |
Correct |
417 ms |
378884 KB |
Output is correct |
13 |
Correct |
419 ms |
378808 KB |
Output is correct |
14 |
Correct |
367 ms |
378028 KB |
Output is correct |
15 |
Correct |
456 ms |
380352 KB |
Output is correct |
16 |
Correct |
441 ms |
380276 KB |
Output is correct |
17 |
Correct |
520 ms |
380376 KB |
Output is correct |
18 |
Correct |
473 ms |
380380 KB |
Output is correct |
19 |
Correct |
228 ms |
378580 KB |
Output is correct |
20 |
Correct |
440 ms |
381324 KB |
Output is correct |
21 |
Correct |
442 ms |
381472 KB |
Output is correct |
22 |
Correct |
454 ms |
381616 KB |
Output is correct |
23 |
Correct |
468 ms |
381692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
167 ms |
374424 KB |
Output is correct |
2 |
Correct |
170 ms |
374484 KB |
Output is correct |
3 |
Correct |
485 ms |
381288 KB |
Output is correct |
4 |
Correct |
513 ms |
381232 KB |
Output is correct |
5 |
Correct |
495 ms |
381560 KB |
Output is correct |
6 |
Correct |
467 ms |
385040 KB |
Output is correct |
7 |
Correct |
520 ms |
385500 KB |
Output is correct |
8 |
Correct |
471 ms |
385280 KB |
Output is correct |
9 |
Correct |
471 ms |
385280 KB |
Output is correct |
10 |
Correct |
446 ms |
385160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
167 ms |
374424 KB |
Output is correct |
2 |
Correct |
170 ms |
374484 KB |
Output is correct |
3 |
Correct |
171 ms |
374412 KB |
Output is correct |
4 |
Correct |
168 ms |
374468 KB |
Output is correct |
5 |
Correct |
170 ms |
374404 KB |
Output is correct |
6 |
Correct |
171 ms |
374472 KB |
Output is correct |
7 |
Correct |
169 ms |
374412 KB |
Output is correct |
8 |
Correct |
174 ms |
374436 KB |
Output is correct |
9 |
Correct |
191 ms |
374492 KB |
Output is correct |
10 |
Correct |
170 ms |
374540 KB |
Output is correct |
11 |
Correct |
166 ms |
374496 KB |
Output is correct |
12 |
Correct |
167 ms |
374528 KB |
Output is correct |
13 |
Correct |
174 ms |
374584 KB |
Output is correct |
14 |
Correct |
168 ms |
374540 KB |
Output is correct |
15 |
Correct |
176 ms |
374604 KB |
Output is correct |
16 |
Correct |
166 ms |
374500 KB |
Output is correct |
17 |
Correct |
163 ms |
374516 KB |
Output is correct |
18 |
Correct |
187 ms |
374536 KB |
Output is correct |
19 |
Correct |
165 ms |
374408 KB |
Output is correct |
20 |
Correct |
167 ms |
374640 KB |
Output is correct |
21 |
Correct |
175 ms |
374428 KB |
Output is correct |
22 |
Correct |
168 ms |
374620 KB |
Output is correct |
23 |
Correct |
166 ms |
374480 KB |
Output is correct |
24 |
Correct |
185 ms |
374536 KB |
Output is correct |
25 |
Correct |
173 ms |
374512 KB |
Output is correct |
26 |
Correct |
162 ms |
374480 KB |
Output is correct |
27 |
Correct |
175 ms |
374464 KB |
Output is correct |
28 |
Correct |
162 ms |
374488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
191 ms |
374492 KB |
Output is correct |
2 |
Correct |
167 ms |
374424 KB |
Output is correct |
3 |
Correct |
170 ms |
374484 KB |
Output is correct |
4 |
Correct |
171 ms |
374412 KB |
Output is correct |
5 |
Correct |
168 ms |
374468 KB |
Output is correct |
6 |
Correct |
170 ms |
374404 KB |
Output is correct |
7 |
Correct |
171 ms |
374472 KB |
Output is correct |
8 |
Correct |
169 ms |
374412 KB |
Output is correct |
9 |
Correct |
174 ms |
374436 KB |
Output is correct |
10 |
Correct |
318 ms |
377876 KB |
Output is correct |
11 |
Correct |
399 ms |
378640 KB |
Output is correct |
12 |
Correct |
441 ms |
378608 KB |
Output is correct |
13 |
Correct |
417 ms |
378884 KB |
Output is correct |
14 |
Correct |
419 ms |
378808 KB |
Output is correct |
15 |
Correct |
170 ms |
374540 KB |
Output is correct |
16 |
Correct |
166 ms |
374496 KB |
Output is correct |
17 |
Correct |
167 ms |
374528 KB |
Output is correct |
18 |
Correct |
174 ms |
374584 KB |
Output is correct |
19 |
Correct |
168 ms |
374540 KB |
Output is correct |
20 |
Correct |
176 ms |
374604 KB |
Output is correct |
21 |
Correct |
166 ms |
374500 KB |
Output is correct |
22 |
Correct |
163 ms |
374516 KB |
Output is correct |
23 |
Correct |
187 ms |
374536 KB |
Output is correct |
24 |
Correct |
165 ms |
374408 KB |
Output is correct |
25 |
Correct |
167 ms |
374640 KB |
Output is correct |
26 |
Correct |
175 ms |
374428 KB |
Output is correct |
27 |
Correct |
168 ms |
374620 KB |
Output is correct |
28 |
Correct |
166 ms |
374480 KB |
Output is correct |
29 |
Correct |
185 ms |
374536 KB |
Output is correct |
30 |
Correct |
173 ms |
374512 KB |
Output is correct |
31 |
Correct |
162 ms |
374480 KB |
Output is correct |
32 |
Correct |
175 ms |
374464 KB |
Output is correct |
33 |
Correct |
162 ms |
374488 KB |
Output is correct |
34 |
Correct |
180 ms |
375024 KB |
Output is correct |
35 |
Correct |
427 ms |
378844 KB |
Output is correct |
36 |
Correct |
407 ms |
378864 KB |
Output is correct |
37 |
Correct |
414 ms |
378872 KB |
Output is correct |
38 |
Correct |
406 ms |
378752 KB |
Output is correct |
39 |
Correct |
400 ms |
378740 KB |
Output is correct |
40 |
Correct |
380 ms |
378392 KB |
Output is correct |
41 |
Correct |
478 ms |
378856 KB |
Output is correct |
42 |
Correct |
446 ms |
378928 KB |
Output is correct |
43 |
Correct |
427 ms |
378780 KB |
Output is correct |
44 |
Correct |
410 ms |
378808 KB |
Output is correct |
45 |
Correct |
500 ms |
380296 KB |
Output is correct |
46 |
Correct |
432 ms |
378804 KB |
Output is correct |
47 |
Correct |
426 ms |
378912 KB |
Output is correct |
48 |
Correct |
463 ms |
379212 KB |
Output is correct |
49 |
Correct |
230 ms |
380084 KB |
Output is correct |
50 |
Correct |
225 ms |
378792 KB |
Output is correct |
51 |
Correct |
406 ms |
379656 KB |
Output is correct |
52 |
Correct |
788 ms |
380968 KB |
Output is correct |
53 |
Correct |
883 ms |
381580 KB |
Output is correct |
54 |
Correct |
869 ms |
382324 KB |
Output is correct |
55 |
Correct |
413 ms |
378828 KB |
Output is correct |
56 |
Correct |
644 ms |
381336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
167 ms |
374424 KB |
Output is correct |
2 |
Correct |
170 ms |
374484 KB |
Output is correct |
3 |
Correct |
171 ms |
374412 KB |
Output is correct |
4 |
Correct |
168 ms |
374468 KB |
Output is correct |
5 |
Correct |
170 ms |
374404 KB |
Output is correct |
6 |
Correct |
171 ms |
374472 KB |
Output is correct |
7 |
Correct |
169 ms |
374412 KB |
Output is correct |
8 |
Correct |
174 ms |
374436 KB |
Output is correct |
9 |
Correct |
318 ms |
377876 KB |
Output is correct |
10 |
Correct |
399 ms |
378640 KB |
Output is correct |
11 |
Correct |
441 ms |
378608 KB |
Output is correct |
12 |
Correct |
417 ms |
378884 KB |
Output is correct |
13 |
Correct |
419 ms |
378808 KB |
Output is correct |
14 |
Correct |
367 ms |
378028 KB |
Output is correct |
15 |
Correct |
456 ms |
380352 KB |
Output is correct |
16 |
Correct |
441 ms |
380276 KB |
Output is correct |
17 |
Correct |
520 ms |
380376 KB |
Output is correct |
18 |
Correct |
473 ms |
380380 KB |
Output is correct |
19 |
Correct |
485 ms |
381288 KB |
Output is correct |
20 |
Correct |
513 ms |
381232 KB |
Output is correct |
21 |
Correct |
495 ms |
381560 KB |
Output is correct |
22 |
Correct |
467 ms |
385040 KB |
Output is correct |
23 |
Correct |
520 ms |
385500 KB |
Output is correct |
24 |
Correct |
471 ms |
385280 KB |
Output is correct |
25 |
Correct |
471 ms |
385280 KB |
Output is correct |
26 |
Correct |
446 ms |
385160 KB |
Output is correct |
27 |
Correct |
170 ms |
374540 KB |
Output is correct |
28 |
Correct |
166 ms |
374496 KB |
Output is correct |
29 |
Correct |
167 ms |
374528 KB |
Output is correct |
30 |
Correct |
174 ms |
374584 KB |
Output is correct |
31 |
Correct |
168 ms |
374540 KB |
Output is correct |
32 |
Correct |
176 ms |
374604 KB |
Output is correct |
33 |
Correct |
166 ms |
374500 KB |
Output is correct |
34 |
Correct |
163 ms |
374516 KB |
Output is correct |
35 |
Correct |
187 ms |
374536 KB |
Output is correct |
36 |
Correct |
180 ms |
375024 KB |
Output is correct |
37 |
Correct |
427 ms |
378844 KB |
Output is correct |
38 |
Correct |
407 ms |
378864 KB |
Output is correct |
39 |
Correct |
414 ms |
378872 KB |
Output is correct |
40 |
Correct |
406 ms |
378752 KB |
Output is correct |
41 |
Correct |
400 ms |
378740 KB |
Output is correct |
42 |
Correct |
380 ms |
378392 KB |
Output is correct |
43 |
Correct |
478 ms |
378856 KB |
Output is correct |
44 |
Correct |
446 ms |
378928 KB |
Output is correct |
45 |
Correct |
427 ms |
378780 KB |
Output is correct |
46 |
Correct |
410 ms |
378808 KB |
Output is correct |
47 |
Correct |
992 ms |
375700 KB |
Output is correct |
48 |
Execution timed out |
2077 ms |
384584 KB |
Time limit exceeded |
49 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
191 ms |
374492 KB |
Output is correct |
2 |
Correct |
167 ms |
374424 KB |
Output is correct |
3 |
Correct |
170 ms |
374484 KB |
Output is correct |
4 |
Correct |
171 ms |
374412 KB |
Output is correct |
5 |
Correct |
168 ms |
374468 KB |
Output is correct |
6 |
Correct |
170 ms |
374404 KB |
Output is correct |
7 |
Correct |
171 ms |
374472 KB |
Output is correct |
8 |
Correct |
169 ms |
374412 KB |
Output is correct |
9 |
Correct |
174 ms |
374436 KB |
Output is correct |
10 |
Correct |
318 ms |
377876 KB |
Output is correct |
11 |
Correct |
399 ms |
378640 KB |
Output is correct |
12 |
Correct |
441 ms |
378608 KB |
Output is correct |
13 |
Correct |
417 ms |
378884 KB |
Output is correct |
14 |
Correct |
419 ms |
378808 KB |
Output is correct |
15 |
Correct |
367 ms |
378028 KB |
Output is correct |
16 |
Correct |
456 ms |
380352 KB |
Output is correct |
17 |
Correct |
441 ms |
380276 KB |
Output is correct |
18 |
Correct |
520 ms |
380376 KB |
Output is correct |
19 |
Correct |
473 ms |
380380 KB |
Output is correct |
20 |
Correct |
485 ms |
381288 KB |
Output is correct |
21 |
Correct |
513 ms |
381232 KB |
Output is correct |
22 |
Correct |
495 ms |
381560 KB |
Output is correct |
23 |
Correct |
467 ms |
385040 KB |
Output is correct |
24 |
Correct |
520 ms |
385500 KB |
Output is correct |
25 |
Correct |
471 ms |
385280 KB |
Output is correct |
26 |
Correct |
471 ms |
385280 KB |
Output is correct |
27 |
Correct |
446 ms |
385160 KB |
Output is correct |
28 |
Correct |
170 ms |
374540 KB |
Output is correct |
29 |
Correct |
166 ms |
374496 KB |
Output is correct |
30 |
Correct |
167 ms |
374528 KB |
Output is correct |
31 |
Correct |
174 ms |
374584 KB |
Output is correct |
32 |
Correct |
168 ms |
374540 KB |
Output is correct |
33 |
Correct |
176 ms |
374604 KB |
Output is correct |
34 |
Correct |
166 ms |
374500 KB |
Output is correct |
35 |
Correct |
163 ms |
374516 KB |
Output is correct |
36 |
Correct |
187 ms |
374536 KB |
Output is correct |
37 |
Correct |
180 ms |
375024 KB |
Output is correct |
38 |
Correct |
427 ms |
378844 KB |
Output is correct |
39 |
Correct |
407 ms |
378864 KB |
Output is correct |
40 |
Correct |
414 ms |
378872 KB |
Output is correct |
41 |
Correct |
406 ms |
378752 KB |
Output is correct |
42 |
Correct |
400 ms |
378740 KB |
Output is correct |
43 |
Correct |
380 ms |
378392 KB |
Output is correct |
44 |
Correct |
478 ms |
378856 KB |
Output is correct |
45 |
Correct |
446 ms |
378928 KB |
Output is correct |
46 |
Correct |
427 ms |
378780 KB |
Output is correct |
47 |
Correct |
410 ms |
378808 KB |
Output is correct |
48 |
Correct |
992 ms |
375700 KB |
Output is correct |
49 |
Execution timed out |
2077 ms |
384584 KB |
Time limit exceeded |
50 |
Halted |
0 ms |
0 KB |
- |