#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
struct DSU {
vector<int> f;
vector<int> siz;
vector<int> deg;
vector<int> ok;
vector<vector<pair<int*, int>>> stk;
DSU(int n) : f(n), siz(n, 1), deg(n, 0), ok(n, false) { iota(f.begin(), f.end(), 0); }
int leader(int u) {
while (u != f[u]) u = f[u];
return u;
}
bool unionize(int u0, int v0) {
stk.emplace_back();
int u = leader(u0);
int v = leader(v0);
if (u == v) {
stk.back().push_back({&ok[u], ok[u]}); ok[u] = true;
return false;
}
if (siz[u] > siz[v]) swap(u, v);
stk.back().push_back({&siz[v], siz[v]}); siz[v] += siz[u];
stk.back().push_back({°[u0], deg[u0]}); deg[u0]++;
stk.back().push_back({°[v0], deg[v0]}); deg[v0]++;
stk.back().push_back({&ok[v], ok[v]}); if (deg[u0] >= 3 || deg[v0] >= 3) ok[v] = true; ok[v] = ok[v] | ok[u];
stk.back().push_back({&f[u], f[u]}); f[u] = v;
return true;
}
bool same(int u, int v) {
return leader(u) == leader(v);
}
bool isOk(int u) {
return ok[leader(u)];
}
void rollback() {
for (auto [ptr, val] : stk.back()) {
*ptr = val;
}
stk.pop_back();
}
};
int n, m;
vector<array<int, 3>> edges;
const int B = 2000;
vector<DSU> dsus;
int b;
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
n = N;
m = M;
edges.resize(m);
for (int i = 0; i < m; i++) {
edges[i] = {U[i], V[i], W[i]};
}
sort(edges.begin(), edges.end(), [&](array<int, 3> e0, array<int, 3> e1) { return e0[2] < e1[2]; });
dsus.emplace_back(n);
for (b = 0; B * b < m; b++) {
dsus.push_back(dsus.back());
dsus.back().stk.clear();
for (int i = B * b; i < min(m, B * b + B); i++) {
dsus.back().unionize(edges[i][0], edges[i][1]);
}
}
b++;
}
int getMinimumFuelCapacity(int X, int Y) {
if (!dsus.back().same(X, Y) || !dsus.back().isOk(X)) return -1;
for (int b0 = 0; ; b0++) {
if (dsus[b0 + 1].same(X, Y) && dsus[b0 + 1].isOk(X)) {
int cnt = 0;
int res;
for (int i = B * b0; ; i++) {
dsus[b0].unionize(edges[i][0], edges[i][1]);
cnt++;
if (dsus[b0].same(X, Y) && dsus[b0].isOk(X)) {
res = edges[i][2];
break;
}
}
while (cnt--) dsus[b0].rollback();
return res;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
64 ms |
66924 KB |
Output is correct |
10 |
Correct |
86 ms |
95056 KB |
Output is correct |
11 |
Correct |
78 ms |
92540 KB |
Output is correct |
12 |
Correct |
88 ms |
100280 KB |
Output is correct |
13 |
Correct |
83 ms |
100436 KB |
Output is correct |
14 |
Correct |
71 ms |
66844 KB |
Output is correct |
15 |
Correct |
163 ms |
97740 KB |
Output is correct |
16 |
Correct |
135 ms |
93440 KB |
Output is correct |
17 |
Correct |
141 ms |
102092 KB |
Output is correct |
18 |
Correct |
126 ms |
102064 KB |
Output is correct |
19 |
Execution timed out |
2017 ms |
7772 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Execution timed out |
2050 ms |
97848 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
600 KB |
Output is correct |
17 |
Correct |
2 ms |
600 KB |
Output is correct |
18 |
Correct |
2 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
724 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
2 ms |
860 KB |
Output is correct |
25 |
Correct |
2 ms |
860 KB |
Output is correct |
26 |
Correct |
2 ms |
860 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
64 ms |
66924 KB |
Output is correct |
11 |
Correct |
86 ms |
95056 KB |
Output is correct |
12 |
Correct |
78 ms |
92540 KB |
Output is correct |
13 |
Correct |
88 ms |
100280 KB |
Output is correct |
14 |
Correct |
83 ms |
100436 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
2 ms |
600 KB |
Output is correct |
22 |
Correct |
2 ms |
600 KB |
Output is correct |
23 |
Correct |
2 ms |
600 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
2 ms |
860 KB |
Output is correct |
30 |
Correct |
2 ms |
860 KB |
Output is correct |
31 |
Correct |
2 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
2 ms |
860 KB |
Output is correct |
34 |
Correct |
11 ms |
4952 KB |
Output is correct |
35 |
Correct |
85 ms |
100432 KB |
Output is correct |
36 |
Correct |
94 ms |
100416 KB |
Output is correct |
37 |
Correct |
82 ms |
100360 KB |
Output is correct |
38 |
Correct |
83 ms |
99392 KB |
Output is correct |
39 |
Correct |
82 ms |
98628 KB |
Output is correct |
40 |
Correct |
76 ms |
83028 KB |
Output is correct |
41 |
Correct |
93 ms |
100432 KB |
Output is correct |
42 |
Correct |
87 ms |
100432 KB |
Output is correct |
43 |
Correct |
90 ms |
100488 KB |
Output is correct |
44 |
Correct |
89 ms |
100516 KB |
Output is correct |
45 |
Correct |
114 ms |
114916 KB |
Output is correct |
46 |
Correct |
91 ms |
100436 KB |
Output is correct |
47 |
Correct |
88 ms |
102220 KB |
Output is correct |
48 |
Correct |
101 ms |
107856 KB |
Output is correct |
49 |
Correct |
67 ms |
15956 KB |
Output is correct |
50 |
Correct |
53 ms |
19260 KB |
Output is correct |
51 |
Correct |
99 ms |
85644 KB |
Output is correct |
52 |
Correct |
130 ms |
154552 KB |
Output is correct |
53 |
Correct |
158 ms |
170404 KB |
Output is correct |
54 |
Correct |
162 ms |
187996 KB |
Output is correct |
55 |
Correct |
87 ms |
102124 KB |
Output is correct |
56 |
Correct |
163 ms |
163540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
64 ms |
66924 KB |
Output is correct |
10 |
Correct |
86 ms |
95056 KB |
Output is correct |
11 |
Correct |
78 ms |
92540 KB |
Output is correct |
12 |
Correct |
88 ms |
100280 KB |
Output is correct |
13 |
Correct |
83 ms |
100436 KB |
Output is correct |
14 |
Correct |
71 ms |
66844 KB |
Output is correct |
15 |
Correct |
163 ms |
97740 KB |
Output is correct |
16 |
Correct |
135 ms |
93440 KB |
Output is correct |
17 |
Correct |
141 ms |
102092 KB |
Output is correct |
18 |
Correct |
126 ms |
102064 KB |
Output is correct |
19 |
Execution timed out |
2050 ms |
97848 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
64 ms |
66924 KB |
Output is correct |
11 |
Correct |
86 ms |
95056 KB |
Output is correct |
12 |
Correct |
78 ms |
92540 KB |
Output is correct |
13 |
Correct |
88 ms |
100280 KB |
Output is correct |
14 |
Correct |
83 ms |
100436 KB |
Output is correct |
15 |
Correct |
71 ms |
66844 KB |
Output is correct |
16 |
Correct |
163 ms |
97740 KB |
Output is correct |
17 |
Correct |
135 ms |
93440 KB |
Output is correct |
18 |
Correct |
141 ms |
102092 KB |
Output is correct |
19 |
Correct |
126 ms |
102064 KB |
Output is correct |
20 |
Execution timed out |
2050 ms |
97848 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |