#include <bits/stdc++.h>
using namespace std; using ii = pair<int,int>; using vi = vector<int>;
#define rep(i,a,b) for (auto i = (a); i <= (b); ++i)
#define all(x) begin(x), end(x)
#define siz(x) int((x).size())
#define mup(x,y) x = min(x,y)
#define fi first
#define se second
const int N = 1e5+3;
vector<tuple<int,int,int>> edges;
vector<ii> group_log[N];
int swappable_moment[N];
int timer;
int deg[N];
struct dsu {
set<int> s; int name; bool swappable;
dsu(int i): s({i}), name(i), swappable(0) {}
void insert(int x) {
if (s.count(x)) swappable=1;
else {
s.insert(x);
if (group_log[x].back().fi == timer) group_log[x].back().se = name;
else group_log[x].emplace_back(timer, name);
}
}
} *ds[N];
dsu* merge(dsu* x, dsu* y) {
if (x == y) {
x->swappable = 1;
mup(swappable_moment[x->name], timer);
return x;
}
if (siz(x->s) < siz(y->s)) swap(x,y);
x->swappable |= y->swappable;
for (auto v : y->s) x->insert(v), ds[v] = x;
if (x->swappable) mup(swappable_moment[x->name], timer);
return x;
}
void init(int n, int m, vi u, vi v, vi w) {
rep(i,0,m-1) edges.emplace_back(u[i],v[i],w[i]);
sort(all(edges),[](auto&x, auto&y){ return get<2>(x) < get<2>(y); });
rep(i,0,n-1) {
ds[i] = new dsu(i);
group_log[i].emplace_back(0,i);
swappable_moment[i] = 1e9+1;
}
for (auto [u,v,w] : edges) {
timer = w;
if (++deg[u]>2) ds[u]->swappable=1;
if (++deg[v]>2) ds[v]->swappable=1;
ds[u] = ds[v] = merge(ds[u],ds[v]);
}
}
int getMinimumFuelCapacity(int x, int y) {
auto valid = [&](int t) {
int a = prev(lower_bound(all(group_log[x]),ii{t+1,-1}))->se;
int b = prev(lower_bound(all(group_log[y]),ii{t+1,-1}))->se;
if (a != b) return false;
return swappable_moment[a] <= t;
};
const int INF = 1e9+1;
int a = 0, b = INF, m;
while (a <= b) {
m = (a+b)/2;
if (valid(m)) b = m-1;
else a = m+1;
}
if (a >= INF) return -1;
return a;
}
#ifdef LOCAL
int main() {
int N, M;
assert(2 == scanf("%d %d", &N, &M));
std::vector<int> U(M), V(M), W(M);
for (int i = 0; i < M; ++i) {
assert(3 == scanf("%d %d %d", &U[i], &V[i], &W[i]));
}
int Q;
assert(1 == scanf("%d", &Q));
std::vector<int> X(Q), Y(Q);
for (int i = 0; i < Q; ++i) {
assert(2 == scanf("%d %d", &X[i], &Y[i]));
}
init(N, M, U, V, W);
std::vector<int> minimum_fuel_capacities(Q);
for (int i = 0; i < Q; ++i) {
minimum_fuel_capacities[i] = getMinimumFuelCapacity(X[i], Y[i]);
}
for (int i = 0; i < Q; ++i) {
printf("%d\n", minimum_fuel_capacities[i]);
}
return 0;
}
#endif
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3928 KB |
Output is correct |
2 |
Correct |
1 ms |
3928 KB |
Output is correct |
3 |
Correct |
1 ms |
3932 KB |
Output is correct |
4 |
Correct |
1 ms |
4188 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
158 ms |
46632 KB |
Output is correct |
10 |
Correct |
200 ms |
54980 KB |
Output is correct |
11 |
Correct |
201 ms |
54240 KB |
Output is correct |
12 |
Correct |
205 ms |
57540 KB |
Output is correct |
13 |
Correct |
105 ms |
31176 KB |
Output is correct |
14 |
Correct |
159 ms |
45952 KB |
Output is correct |
15 |
Correct |
389 ms |
58324 KB |
Output is correct |
16 |
Correct |
350 ms |
56548 KB |
Output is correct |
17 |
Correct |
372 ms |
60144 KB |
Output is correct |
18 |
Correct |
223 ms |
33068 KB |
Output is correct |
19 |
Correct |
133 ms |
15072 KB |
Output is correct |
20 |
Correct |
372 ms |
59568 KB |
Output is correct |
21 |
Correct |
367 ms |
57828 KB |
Output is correct |
22 |
Correct |
388 ms |
61216 KB |
Output is correct |
23 |
Correct |
242 ms |
34548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3928 KB |
Output is correct |
2 |
Correct |
1 ms |
3928 KB |
Output is correct |
3 |
Correct |
205 ms |
28272 KB |
Output is correct |
4 |
Correct |
216 ms |
29304 KB |
Output is correct |
5 |
Correct |
207 ms |
29164 KB |
Output is correct |
6 |
Correct |
207 ms |
29488 KB |
Output is correct |
7 |
Correct |
212 ms |
29100 KB |
Output is correct |
8 |
Correct |
219 ms |
28280 KB |
Output is correct |
9 |
Correct |
203 ms |
28996 KB |
Output is correct |
10 |
Correct |
217 ms |
27988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3928 KB |
Output is correct |
2 |
Correct |
1 ms |
3928 KB |
Output is correct |
3 |
Correct |
1 ms |
3932 KB |
Output is correct |
4 |
Correct |
1 ms |
4188 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
3932 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Correct |
2 ms |
4444 KB |
Output is correct |
12 |
Correct |
2 ms |
4444 KB |
Output is correct |
13 |
Correct |
2 ms |
4444 KB |
Output is correct |
14 |
Correct |
2 ms |
4444 KB |
Output is correct |
15 |
Correct |
2 ms |
4444 KB |
Output is correct |
16 |
Correct |
2 ms |
4444 KB |
Output is correct |
17 |
Correct |
2 ms |
4440 KB |
Output is correct |
18 |
Correct |
2 ms |
4444 KB |
Output is correct |
19 |
Correct |
3 ms |
4188 KB |
Output is correct |
20 |
Correct |
2 ms |
4444 KB |
Output is correct |
21 |
Correct |
2 ms |
4440 KB |
Output is correct |
22 |
Correct |
2 ms |
4184 KB |
Output is correct |
23 |
Correct |
2 ms |
4188 KB |
Output is correct |
24 |
Correct |
2 ms |
4444 KB |
Output is correct |
25 |
Correct |
2 ms |
4444 KB |
Output is correct |
26 |
Correct |
2 ms |
4440 KB |
Output is correct |
27 |
Correct |
2 ms |
4444 KB |
Output is correct |
28 |
Correct |
2 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3932 KB |
Output is correct |
2 |
Correct |
1 ms |
3928 KB |
Output is correct |
3 |
Correct |
1 ms |
3928 KB |
Output is correct |
4 |
Correct |
1 ms |
3932 KB |
Output is correct |
5 |
Correct |
1 ms |
4188 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
158 ms |
46632 KB |
Output is correct |
11 |
Correct |
200 ms |
54980 KB |
Output is correct |
12 |
Correct |
201 ms |
54240 KB |
Output is correct |
13 |
Correct |
205 ms |
57540 KB |
Output is correct |
14 |
Correct |
105 ms |
31176 KB |
Output is correct |
15 |
Correct |
2 ms |
4444 KB |
Output is correct |
16 |
Correct |
2 ms |
4444 KB |
Output is correct |
17 |
Correct |
2 ms |
4444 KB |
Output is correct |
18 |
Correct |
2 ms |
4444 KB |
Output is correct |
19 |
Correct |
2 ms |
4444 KB |
Output is correct |
20 |
Correct |
2 ms |
4444 KB |
Output is correct |
21 |
Correct |
2 ms |
4444 KB |
Output is correct |
22 |
Correct |
2 ms |
4440 KB |
Output is correct |
23 |
Correct |
2 ms |
4444 KB |
Output is correct |
24 |
Correct |
3 ms |
4188 KB |
Output is correct |
25 |
Correct |
2 ms |
4444 KB |
Output is correct |
26 |
Correct |
2 ms |
4440 KB |
Output is correct |
27 |
Correct |
2 ms |
4184 KB |
Output is correct |
28 |
Correct |
2 ms |
4188 KB |
Output is correct |
29 |
Correct |
2 ms |
4444 KB |
Output is correct |
30 |
Correct |
2 ms |
4444 KB |
Output is correct |
31 |
Correct |
2 ms |
4440 KB |
Output is correct |
32 |
Correct |
2 ms |
4444 KB |
Output is correct |
33 |
Correct |
2 ms |
4444 KB |
Output is correct |
34 |
Correct |
16 ms |
9756 KB |
Output is correct |
35 |
Correct |
220 ms |
57184 KB |
Output is correct |
36 |
Correct |
239 ms |
56060 KB |
Output is correct |
37 |
Correct |
202 ms |
53960 KB |
Output is correct |
38 |
Correct |
191 ms |
51144 KB |
Output is correct |
39 |
Correct |
198 ms |
50276 KB |
Output is correct |
40 |
Correct |
189 ms |
44740 KB |
Output is correct |
41 |
Correct |
222 ms |
58820 KB |
Output is correct |
42 |
Correct |
234 ms |
58856 KB |
Output is correct |
43 |
Correct |
106 ms |
29804 KB |
Output is correct |
44 |
Correct |
192 ms |
48716 KB |
Output is correct |
45 |
Correct |
128 ms |
31520 KB |
Output is correct |
46 |
Correct |
208 ms |
55828 KB |
Output is correct |
47 |
Correct |
168 ms |
44380 KB |
Output is correct |
48 |
Correct |
133 ms |
34900 KB |
Output is correct |
49 |
Correct |
47 ms |
11464 KB |
Output is correct |
50 |
Correct |
40 ms |
10436 KB |
Output is correct |
51 |
Correct |
92 ms |
26212 KB |
Output is correct |
52 |
Correct |
211 ms |
55956 KB |
Output is correct |
53 |
Correct |
176 ms |
41936 KB |
Output is correct |
54 |
Correct |
247 ms |
61836 KB |
Output is correct |
55 |
Correct |
106 ms |
30404 KB |
Output is correct |
56 |
Correct |
169 ms |
38436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3928 KB |
Output is correct |
2 |
Correct |
1 ms |
3928 KB |
Output is correct |
3 |
Correct |
1 ms |
3932 KB |
Output is correct |
4 |
Correct |
1 ms |
4188 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
158 ms |
46632 KB |
Output is correct |
10 |
Correct |
200 ms |
54980 KB |
Output is correct |
11 |
Correct |
201 ms |
54240 KB |
Output is correct |
12 |
Correct |
205 ms |
57540 KB |
Output is correct |
13 |
Correct |
105 ms |
31176 KB |
Output is correct |
14 |
Correct |
159 ms |
45952 KB |
Output is correct |
15 |
Correct |
389 ms |
58324 KB |
Output is correct |
16 |
Correct |
350 ms |
56548 KB |
Output is correct |
17 |
Correct |
372 ms |
60144 KB |
Output is correct |
18 |
Correct |
223 ms |
33068 KB |
Output is correct |
19 |
Correct |
205 ms |
28272 KB |
Output is correct |
20 |
Correct |
216 ms |
29304 KB |
Output is correct |
21 |
Correct |
207 ms |
29164 KB |
Output is correct |
22 |
Correct |
207 ms |
29488 KB |
Output is correct |
23 |
Correct |
212 ms |
29100 KB |
Output is correct |
24 |
Correct |
219 ms |
28280 KB |
Output is correct |
25 |
Correct |
203 ms |
28996 KB |
Output is correct |
26 |
Correct |
217 ms |
27988 KB |
Output is correct |
27 |
Correct |
2 ms |
4444 KB |
Output is correct |
28 |
Correct |
2 ms |
4444 KB |
Output is correct |
29 |
Correct |
2 ms |
4444 KB |
Output is correct |
30 |
Correct |
2 ms |
4444 KB |
Output is correct |
31 |
Correct |
2 ms |
4444 KB |
Output is correct |
32 |
Correct |
2 ms |
4444 KB |
Output is correct |
33 |
Correct |
2 ms |
4444 KB |
Output is correct |
34 |
Correct |
2 ms |
4440 KB |
Output is correct |
35 |
Correct |
2 ms |
4444 KB |
Output is correct |
36 |
Correct |
16 ms |
9756 KB |
Output is correct |
37 |
Correct |
220 ms |
57184 KB |
Output is correct |
38 |
Correct |
239 ms |
56060 KB |
Output is correct |
39 |
Correct |
202 ms |
53960 KB |
Output is correct |
40 |
Correct |
191 ms |
51144 KB |
Output is correct |
41 |
Correct |
198 ms |
50276 KB |
Output is correct |
42 |
Correct |
189 ms |
44740 KB |
Output is correct |
43 |
Correct |
222 ms |
58820 KB |
Output is correct |
44 |
Correct |
234 ms |
58856 KB |
Output is correct |
45 |
Correct |
106 ms |
29804 KB |
Output is correct |
46 |
Correct |
192 ms |
48716 KB |
Output is correct |
47 |
Correct |
29 ms |
9696 KB |
Output is correct |
48 |
Correct |
357 ms |
59784 KB |
Output is correct |
49 |
Correct |
348 ms |
57628 KB |
Output is correct |
50 |
Correct |
328 ms |
57136 KB |
Output is correct |
51 |
Correct |
331 ms |
54840 KB |
Output is correct |
52 |
Correct |
300 ms |
50036 KB |
Output is correct |
53 |
Correct |
277 ms |
36388 KB |
Output is correct |
54 |
Correct |
343 ms |
59952 KB |
Output is correct |
55 |
Correct |
356 ms |
61952 KB |
Output is correct |
56 |
Correct |
226 ms |
33584 KB |
Output is correct |
57 |
Correct |
336 ms |
50636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3932 KB |
Output is correct |
2 |
Correct |
1 ms |
3928 KB |
Output is correct |
3 |
Correct |
1 ms |
3928 KB |
Output is correct |
4 |
Correct |
1 ms |
3932 KB |
Output is correct |
5 |
Correct |
1 ms |
4188 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
158 ms |
46632 KB |
Output is correct |
11 |
Correct |
200 ms |
54980 KB |
Output is correct |
12 |
Correct |
201 ms |
54240 KB |
Output is correct |
13 |
Correct |
205 ms |
57540 KB |
Output is correct |
14 |
Correct |
105 ms |
31176 KB |
Output is correct |
15 |
Correct |
159 ms |
45952 KB |
Output is correct |
16 |
Correct |
389 ms |
58324 KB |
Output is correct |
17 |
Correct |
350 ms |
56548 KB |
Output is correct |
18 |
Correct |
372 ms |
60144 KB |
Output is correct |
19 |
Correct |
223 ms |
33068 KB |
Output is correct |
20 |
Correct |
205 ms |
28272 KB |
Output is correct |
21 |
Correct |
216 ms |
29304 KB |
Output is correct |
22 |
Correct |
207 ms |
29164 KB |
Output is correct |
23 |
Correct |
207 ms |
29488 KB |
Output is correct |
24 |
Correct |
212 ms |
29100 KB |
Output is correct |
25 |
Correct |
219 ms |
28280 KB |
Output is correct |
26 |
Correct |
203 ms |
28996 KB |
Output is correct |
27 |
Correct |
217 ms |
27988 KB |
Output is correct |
28 |
Correct |
2 ms |
4444 KB |
Output is correct |
29 |
Correct |
2 ms |
4444 KB |
Output is correct |
30 |
Correct |
2 ms |
4444 KB |
Output is correct |
31 |
Correct |
2 ms |
4444 KB |
Output is correct |
32 |
Correct |
2 ms |
4444 KB |
Output is correct |
33 |
Correct |
2 ms |
4444 KB |
Output is correct |
34 |
Correct |
2 ms |
4444 KB |
Output is correct |
35 |
Correct |
2 ms |
4440 KB |
Output is correct |
36 |
Correct |
2 ms |
4444 KB |
Output is correct |
37 |
Correct |
16 ms |
9756 KB |
Output is correct |
38 |
Correct |
220 ms |
57184 KB |
Output is correct |
39 |
Correct |
239 ms |
56060 KB |
Output is correct |
40 |
Correct |
202 ms |
53960 KB |
Output is correct |
41 |
Correct |
191 ms |
51144 KB |
Output is correct |
42 |
Correct |
198 ms |
50276 KB |
Output is correct |
43 |
Correct |
189 ms |
44740 KB |
Output is correct |
44 |
Correct |
222 ms |
58820 KB |
Output is correct |
45 |
Correct |
234 ms |
58856 KB |
Output is correct |
46 |
Correct |
106 ms |
29804 KB |
Output is correct |
47 |
Correct |
192 ms |
48716 KB |
Output is correct |
48 |
Correct |
29 ms |
9696 KB |
Output is correct |
49 |
Correct |
357 ms |
59784 KB |
Output is correct |
50 |
Correct |
348 ms |
57628 KB |
Output is correct |
51 |
Correct |
328 ms |
57136 KB |
Output is correct |
52 |
Correct |
331 ms |
54840 KB |
Output is correct |
53 |
Correct |
300 ms |
50036 KB |
Output is correct |
54 |
Correct |
277 ms |
36388 KB |
Output is correct |
55 |
Correct |
343 ms |
59952 KB |
Output is correct |
56 |
Correct |
356 ms |
61952 KB |
Output is correct |
57 |
Correct |
226 ms |
33584 KB |
Output is correct |
58 |
Correct |
336 ms |
50636 KB |
Output is correct |
59 |
Correct |
133 ms |
15072 KB |
Output is correct |
60 |
Correct |
372 ms |
59568 KB |
Output is correct |
61 |
Correct |
367 ms |
57828 KB |
Output is correct |
62 |
Correct |
388 ms |
61216 KB |
Output is correct |
63 |
Correct |
242 ms |
34548 KB |
Output is correct |
64 |
Correct |
3 ms |
4188 KB |
Output is correct |
65 |
Correct |
2 ms |
4444 KB |
Output is correct |
66 |
Correct |
2 ms |
4440 KB |
Output is correct |
67 |
Correct |
2 ms |
4184 KB |
Output is correct |
68 |
Correct |
2 ms |
4188 KB |
Output is correct |
69 |
Correct |
2 ms |
4444 KB |
Output is correct |
70 |
Correct |
2 ms |
4444 KB |
Output is correct |
71 |
Correct |
2 ms |
4440 KB |
Output is correct |
72 |
Correct |
2 ms |
4444 KB |
Output is correct |
73 |
Correct |
2 ms |
4444 KB |
Output is correct |
74 |
Correct |
128 ms |
31520 KB |
Output is correct |
75 |
Correct |
208 ms |
55828 KB |
Output is correct |
76 |
Correct |
168 ms |
44380 KB |
Output is correct |
77 |
Correct |
133 ms |
34900 KB |
Output is correct |
78 |
Correct |
47 ms |
11464 KB |
Output is correct |
79 |
Correct |
40 ms |
10436 KB |
Output is correct |
80 |
Correct |
92 ms |
26212 KB |
Output is correct |
81 |
Correct |
211 ms |
55956 KB |
Output is correct |
82 |
Correct |
176 ms |
41936 KB |
Output is correct |
83 |
Correct |
247 ms |
61836 KB |
Output is correct |
84 |
Correct |
106 ms |
30404 KB |
Output is correct |
85 |
Correct |
169 ms |
38436 KB |
Output is correct |
86 |
Correct |
68 ms |
15048 KB |
Output is correct |
87 |
Correct |
337 ms |
55580 KB |
Output is correct |
88 |
Correct |
339 ms |
56428 KB |
Output is correct |
89 |
Correct |
251 ms |
35816 KB |
Output is correct |
90 |
Correct |
166 ms |
14032 KB |
Output is correct |
91 |
Correct |
172 ms |
14816 KB |
Output is correct |
92 |
Correct |
224 ms |
29224 KB |
Output is correct |
93 |
Correct |
353 ms |
54904 KB |
Output is correct |
94 |
Correct |
326 ms |
45472 KB |
Output is correct |
95 |
Correct |
394 ms |
64700 KB |
Output is correct |
96 |
Correct |
222 ms |
33328 KB |
Output is correct |
97 |
Correct |
293 ms |
40324 KB |
Output is correct |