#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
struct DSU {
vector<int> f;
// vector<int> siz;
vector<int> deg;
vector<bool> ok;
DSU(int n) : f(n), deg(n, 0), ok(n, false) { iota(f.begin(), f.end(), 0); }
int leader(int u) {
while (u != f[u]) u = f[u] = f[f[u]];
return u;
}
bool unionize(int u0, int v0) {
int u = leader(u0);
int v = leader(v0);
if (u == v) {
ok[u] = true;
return false;
}
// if (siz[u] > siz[v]) swap(u, v);
// siz[v] += siz[u];
if (++deg[u0] >= 3 || ++deg[v0] >= 3) ok[v] = true;
ok[v] = ok[v] | ok[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)];
}
};
struct LiteDSU {
vector<int> f;
// vector<int> siz;
vector<bool> ok;
LiteDSU(int n) : f(n), ok(n, false) { iota(f.begin(), f.end(), 0); }
int leader(int u) {
while (u != f[u]) u = f[u] = f[f[u]];
return u;
}
bool unionize(int u, int v) {
u = leader(u);
v = leader(v);
if (u == v) return false;
// if (siz[u] > siz[v]) swap(u, v);
// siz[v] += siz[u];
ok[v] = ok[v] | ok[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)];
}
};
int n, m;
vector<array<int, 3>> edges;
const int B = 500;
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());
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;
int b0 = b - 1;
while (dsus[b0].same(X, Y) && dsus[b0].isOk(X)) b0--;
vector<int> dom;
for (int i = B * b0; i < min(m, B * b0 + B); i++) {
if (dsus[b0 + 1].same(X, edges[i][0]) || dsus[b0 + 1].same(X, edges[i][1])) {
dom.push_back(dsus[b0].leader(edges[i][0]));
if (!dsus[b0].same(edges[i][0], edges[i][1])) dom.push_back(dsus[b0].leader(edges[i][1]));
}
}
sort(dom.begin(), dom.end());
dom.resize(unique(dom.begin(), dom.end()) - dom.begin());
int c = dom.size();
int x = lower_bound(dom.begin(), dom.end(), dsus[b0].leader(X)) - dom.begin();
int y = lower_bound(dom.begin(), dom.end(), dsus[b0].leader(Y)) - dom.begin();
LiteDSU dsu(c);
int res = -2;
vector<int> defer;
for (int u = 0; u < c; u++) if (dsus[b0].isOk(dom[u])) dsu.ok[u] = true;
for (int i = B * b0; i < min(m, B * b0 + B); i++) {
if (dsus[b0 + 1].same(X, edges[i][0]) || dsus[b0 + 1].same(X, edges[i][1])) {
int u = lower_bound(dom.begin(), dom.end(), dsus[b0].leader(edges[i][0])) - dom.begin();
int v = dsus[b0].same(edges[i][0], edges[i][1]) ? u : lower_bound(dom.begin(), dom.end(), dsus[b0].leader(edges[i][1])) - dom.begin();
if (dsu.same(u, v)) {
dsu.ok[dsu.leader(u)] = true;
} else {
defer.push_back(edges[i][0]);
defer.push_back(edges[i][1]);
if (++dsus[b0].deg[edges[i][0]] >= 3) dsu.ok[dsu.leader(u)] = true;
if (++dsus[b0].deg[edges[i][1]] >= 3) dsu.ok[dsu.leader(v)] = true;
dsu.unionize(u, v);
}
if (dsu.same(x, y) && dsu.isOk(x)) {
res = edges[i][2];
break;
}
}
}
for (int u : defer) {
dsus[b0].deg[u]--;
}
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
504 KB |
Output is correct |
2 |
Correct |
1 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 |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
60 ms |
102228 KB |
Output is correct |
10 |
Correct |
89 ms |
152192 KB |
Output is correct |
11 |
Correct |
87 ms |
147540 KB |
Output is correct |
12 |
Correct |
100 ms |
164172 KB |
Output is correct |
13 |
Correct |
92 ms |
164320 KB |
Output is correct |
14 |
Correct |
68 ms |
102224 KB |
Output is correct |
15 |
Correct |
124 ms |
156380 KB |
Output is correct |
16 |
Correct |
135 ms |
147892 KB |
Output is correct |
17 |
Correct |
134 ms |
166076 KB |
Output is correct |
18 |
Correct |
150 ms |
166104 KB |
Output is correct |
19 |
Execution timed out |
2048 ms |
7004 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
504 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Execution timed out |
2065 ms |
149844 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
504 KB |
Output is correct |
2 |
Correct |
1 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 |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
344 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
504 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
3 |
Correct |
1 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 |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
60 ms |
102228 KB |
Output is correct |
11 |
Correct |
89 ms |
152192 KB |
Output is correct |
12 |
Correct |
87 ms |
147540 KB |
Output is correct |
13 |
Correct |
100 ms |
164172 KB |
Output is correct |
14 |
Correct |
92 ms |
164320 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
344 KB |
Output is correct |
20 |
Correct |
2 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
2 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
504 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
7 ms |
3928 KB |
Output is correct |
35 |
Correct |
105 ms |
164096 KB |
Output is correct |
36 |
Correct |
87 ms |
164252 KB |
Output is correct |
37 |
Correct |
91 ms |
164176 KB |
Output is correct |
38 |
Correct |
91 ms |
161276 KB |
Output is correct |
39 |
Correct |
83 ms |
158428 KB |
Output is correct |
40 |
Correct |
72 ms |
133272 KB |
Output is correct |
41 |
Correct |
89 ms |
164176 KB |
Output is correct |
42 |
Correct |
86 ms |
164224 KB |
Output is correct |
43 |
Correct |
199 ms |
164176 KB |
Output is correct |
44 |
Correct |
96 ms |
164212 KB |
Output is correct |
45 |
Correct |
122 ms |
190440 KB |
Output is correct |
46 |
Correct |
89 ms |
164148 KB |
Output is correct |
47 |
Correct |
92 ms |
164900 KB |
Output is correct |
48 |
Correct |
106 ms |
176724 KB |
Output is correct |
49 |
Correct |
58 ms |
8700 KB |
Output is correct |
50 |
Correct |
41 ms |
19008 KB |
Output is correct |
51 |
Correct |
103 ms |
137112 KB |
Output is correct |
52 |
Correct |
148 ms |
264604 KB |
Output is correct |
53 |
Correct |
157 ms |
293052 KB |
Output is correct |
54 |
Correct |
175 ms |
327476 KB |
Output is correct |
55 |
Correct |
136 ms |
164944 KB |
Output is correct |
56 |
Correct |
158 ms |
281680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
504 KB |
Output is correct |
2 |
Correct |
1 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 |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
60 ms |
102228 KB |
Output is correct |
10 |
Correct |
89 ms |
152192 KB |
Output is correct |
11 |
Correct |
87 ms |
147540 KB |
Output is correct |
12 |
Correct |
100 ms |
164172 KB |
Output is correct |
13 |
Correct |
92 ms |
164320 KB |
Output is correct |
14 |
Correct |
68 ms |
102224 KB |
Output is correct |
15 |
Correct |
124 ms |
156380 KB |
Output is correct |
16 |
Correct |
135 ms |
147892 KB |
Output is correct |
17 |
Correct |
134 ms |
166076 KB |
Output is correct |
18 |
Correct |
150 ms |
166104 KB |
Output is correct |
19 |
Execution timed out |
2065 ms |
149844 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 |
504 KB |
Output is correct |
3 |
Correct |
1 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 |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
60 ms |
102228 KB |
Output is correct |
11 |
Correct |
89 ms |
152192 KB |
Output is correct |
12 |
Correct |
87 ms |
147540 KB |
Output is correct |
13 |
Correct |
100 ms |
164172 KB |
Output is correct |
14 |
Correct |
92 ms |
164320 KB |
Output is correct |
15 |
Correct |
68 ms |
102224 KB |
Output is correct |
16 |
Correct |
124 ms |
156380 KB |
Output is correct |
17 |
Correct |
135 ms |
147892 KB |
Output is correct |
18 |
Correct |
134 ms |
166076 KB |
Output is correct |
19 |
Correct |
150 ms |
166104 KB |
Output is correct |
20 |
Execution timed out |
2065 ms |
149844 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |