#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 per(i,a,b) for (auto i = (b); i >= (a); --i)
#define all(x) begin(x), end(x)
#define siz(x) int((x).size())
#define Mup(x,y) x = max(x,y)
#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];
void merge(dsu* &x, dsu* &y) {
if (x == y) {
x->swappable = 1;
mup(swappable_moment[x->name], timer);
return;
}
// cout << siz(x->s) << ' ' << siz(y->s) << endl;
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);
y = 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;
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
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3932 KB |
Output is correct |
2 |
Correct |
1 ms |
3932 KB |
Output is correct |
3 |
Correct |
1 ms |
3928 KB |
Output is correct |
4 |
Correct |
2 ms |
4188 KB |
Output is correct |
5 |
Correct |
3 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4440 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
170 ms |
46516 KB |
Output is correct |
10 |
Correct |
206 ms |
55204 KB |
Output is correct |
11 |
Correct |
205 ms |
53952 KB |
Output is correct |
12 |
Correct |
219 ms |
57540 KB |
Output is correct |
13 |
Correct |
111 ms |
31176 KB |
Output is correct |
14 |
Correct |
198 ms |
45912 KB |
Output is correct |
15 |
Correct |
363 ms |
58320 KB |
Output is correct |
16 |
Correct |
382 ms |
56692 KB |
Output is correct |
17 |
Correct |
399 ms |
60340 KB |
Output is correct |
18 |
Correct |
228 ms |
33324 KB |
Output is correct |
19 |
Correct |
125 ms |
15124 KB |
Output is correct |
20 |
Correct |
369 ms |
63816 KB |
Output is correct |
21 |
Correct |
364 ms |
62084 KB |
Output is correct |
22 |
Correct |
411 ms |
65836 KB |
Output is correct |
23 |
Correct |
233 ms |
38764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3932 KB |
Output is correct |
2 |
Correct |
1 ms |
3932 KB |
Output is correct |
3 |
Correct |
203 ms |
28264 KB |
Output is correct |
4 |
Correct |
204 ms |
29296 KB |
Output is correct |
5 |
Correct |
210 ms |
28892 KB |
Output is correct |
6 |
Correct |
205 ms |
29388 KB |
Output is correct |
7 |
Correct |
210 ms |
29096 KB |
Output is correct |
8 |
Correct |
209 ms |
28280 KB |
Output is correct |
9 |
Correct |
213 ms |
29260 KB |
Output is correct |
10 |
Correct |
205 ms |
28016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3932 KB |
Output is correct |
2 |
Correct |
1 ms |
3932 KB |
Output is correct |
3 |
Correct |
1 ms |
3928 KB |
Output is correct |
4 |
Correct |
2 ms |
4188 KB |
Output is correct |
5 |
Correct |
3 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4440 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
3928 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Correct |
2 ms |
4440 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 |
4440 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 |
4188 KB |
Output is correct |
20 |
Correct |
3 ms |
4440 KB |
Output is correct |
21 |
Correct |
2 ms |
4444 KB |
Output is correct |
22 |
Correct |
2 ms |
4188 KB |
Output is correct |
23 |
Correct |
2 ms |
4444 KB |
Output is correct |
24 |
Correct |
2 ms |
4448 KB |
Output is correct |
25 |
Correct |
2 ms |
4444 KB |
Output is correct |
26 |
Correct |
2 ms |
4444 KB |
Output is correct |
27 |
Correct |
2 ms |
4444 KB |
Output is correct |
28 |
Correct |
2 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3928 KB |
Output is correct |
2 |
Correct |
1 ms |
3932 KB |
Output is correct |
3 |
Correct |
1 ms |
3932 KB |
Output is correct |
4 |
Correct |
1 ms |
3928 KB |
Output is correct |
5 |
Correct |
2 ms |
4188 KB |
Output is correct |
6 |
Correct |
3 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4440 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
170 ms |
46516 KB |
Output is correct |
11 |
Correct |
206 ms |
55204 KB |
Output is correct |
12 |
Correct |
205 ms |
53952 KB |
Output is correct |
13 |
Correct |
219 ms |
57540 KB |
Output is correct |
14 |
Correct |
111 ms |
31176 KB |
Output is correct |
15 |
Correct |
2 ms |
4444 KB |
Output is correct |
16 |
Correct |
2 ms |
4440 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 |
4440 KB |
Output is correct |
22 |
Correct |
2 ms |
4444 KB |
Output is correct |
23 |
Correct |
2 ms |
4444 KB |
Output is correct |
24 |
Correct |
2 ms |
4188 KB |
Output is correct |
25 |
Correct |
3 ms |
4440 KB |
Output is correct |
26 |
Correct |
2 ms |
4444 KB |
Output is correct |
27 |
Correct |
2 ms |
4188 KB |
Output is correct |
28 |
Correct |
2 ms |
4444 KB |
Output is correct |
29 |
Correct |
2 ms |
4448 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 |
19 ms |
10008 KB |
Output is correct |
35 |
Correct |
237 ms |
58820 KB |
Output is correct |
36 |
Correct |
216 ms |
57796 KB |
Output is correct |
37 |
Correct |
218 ms |
56140 KB |
Output is correct |
38 |
Correct |
203 ms |
53116 KB |
Output is correct |
39 |
Correct |
217 ms |
51812 KB |
Output is correct |
40 |
Correct |
218 ms |
46316 KB |
Output is correct |
41 |
Correct |
261 ms |
60796 KB |
Output is correct |
42 |
Correct |
275 ms |
60520 KB |
Output is correct |
43 |
Correct |
117 ms |
31576 KB |
Output is correct |
44 |
Correct |
229 ms |
50764 KB |
Output is correct |
45 |
Correct |
155 ms |
34084 KB |
Output is correct |
46 |
Correct |
261 ms |
57624 KB |
Output is correct |
47 |
Correct |
231 ms |
46148 KB |
Output is correct |
48 |
Correct |
164 ms |
37064 KB |
Output is correct |
49 |
Correct |
64 ms |
13816 KB |
Output is correct |
50 |
Correct |
44 ms |
12744 KB |
Output is correct |
51 |
Correct |
110 ms |
28872 KB |
Output is correct |
52 |
Correct |
308 ms |
59216 KB |
Output is correct |
53 |
Correct |
218 ms |
46264 KB |
Output is correct |
54 |
Correct |
319 ms |
65764 KB |
Output is correct |
55 |
Correct |
120 ms |
32032 KB |
Output is correct |
56 |
Correct |
187 ms |
42620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3932 KB |
Output is correct |
2 |
Correct |
1 ms |
3932 KB |
Output is correct |
3 |
Correct |
1 ms |
3928 KB |
Output is correct |
4 |
Correct |
2 ms |
4188 KB |
Output is correct |
5 |
Correct |
3 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4440 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
170 ms |
46516 KB |
Output is correct |
10 |
Correct |
206 ms |
55204 KB |
Output is correct |
11 |
Correct |
205 ms |
53952 KB |
Output is correct |
12 |
Correct |
219 ms |
57540 KB |
Output is correct |
13 |
Correct |
111 ms |
31176 KB |
Output is correct |
14 |
Correct |
198 ms |
45912 KB |
Output is correct |
15 |
Correct |
363 ms |
58320 KB |
Output is correct |
16 |
Correct |
382 ms |
56692 KB |
Output is correct |
17 |
Correct |
399 ms |
60340 KB |
Output is correct |
18 |
Correct |
228 ms |
33324 KB |
Output is correct |
19 |
Correct |
203 ms |
28264 KB |
Output is correct |
20 |
Correct |
204 ms |
29296 KB |
Output is correct |
21 |
Correct |
210 ms |
28892 KB |
Output is correct |
22 |
Correct |
205 ms |
29388 KB |
Output is correct |
23 |
Correct |
210 ms |
29096 KB |
Output is correct |
24 |
Correct |
209 ms |
28280 KB |
Output is correct |
25 |
Correct |
213 ms |
29260 KB |
Output is correct |
26 |
Correct |
205 ms |
28016 KB |
Output is correct |
27 |
Correct |
2 ms |
4444 KB |
Output is correct |
28 |
Correct |
2 ms |
4440 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 |
4440 KB |
Output is correct |
34 |
Correct |
2 ms |
4444 KB |
Output is correct |
35 |
Correct |
2 ms |
4444 KB |
Output is correct |
36 |
Correct |
19 ms |
10008 KB |
Output is correct |
37 |
Correct |
237 ms |
58820 KB |
Output is correct |
38 |
Correct |
216 ms |
57796 KB |
Output is correct |
39 |
Correct |
218 ms |
56140 KB |
Output is correct |
40 |
Correct |
203 ms |
53116 KB |
Output is correct |
41 |
Correct |
217 ms |
51812 KB |
Output is correct |
42 |
Correct |
218 ms |
46316 KB |
Output is correct |
43 |
Correct |
261 ms |
60796 KB |
Output is correct |
44 |
Correct |
275 ms |
60520 KB |
Output is correct |
45 |
Correct |
117 ms |
31576 KB |
Output is correct |
46 |
Correct |
229 ms |
50764 KB |
Output is correct |
47 |
Correct |
31 ms |
10232 KB |
Output is correct |
48 |
Correct |
392 ms |
63816 KB |
Output is correct |
49 |
Correct |
392 ms |
61768 KB |
Output is correct |
50 |
Correct |
384 ms |
61480 KB |
Output is correct |
51 |
Correct |
410 ms |
59044 KB |
Output is correct |
52 |
Correct |
386 ms |
54004 KB |
Output is correct |
53 |
Correct |
313 ms |
40032 KB |
Output is correct |
54 |
Correct |
412 ms |
64584 KB |
Output is correct |
55 |
Correct |
411 ms |
66092 KB |
Output is correct |
56 |
Correct |
267 ms |
37692 KB |
Output is correct |
57 |
Correct |
379 ms |
55080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3928 KB |
Output is correct |
2 |
Correct |
1 ms |
3932 KB |
Output is correct |
3 |
Correct |
1 ms |
3932 KB |
Output is correct |
4 |
Correct |
1 ms |
3928 KB |
Output is correct |
5 |
Correct |
2 ms |
4188 KB |
Output is correct |
6 |
Correct |
3 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4440 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
170 ms |
46516 KB |
Output is correct |
11 |
Correct |
206 ms |
55204 KB |
Output is correct |
12 |
Correct |
205 ms |
53952 KB |
Output is correct |
13 |
Correct |
219 ms |
57540 KB |
Output is correct |
14 |
Correct |
111 ms |
31176 KB |
Output is correct |
15 |
Correct |
198 ms |
45912 KB |
Output is correct |
16 |
Correct |
363 ms |
58320 KB |
Output is correct |
17 |
Correct |
382 ms |
56692 KB |
Output is correct |
18 |
Correct |
399 ms |
60340 KB |
Output is correct |
19 |
Correct |
228 ms |
33324 KB |
Output is correct |
20 |
Correct |
203 ms |
28264 KB |
Output is correct |
21 |
Correct |
204 ms |
29296 KB |
Output is correct |
22 |
Correct |
210 ms |
28892 KB |
Output is correct |
23 |
Correct |
205 ms |
29388 KB |
Output is correct |
24 |
Correct |
210 ms |
29096 KB |
Output is correct |
25 |
Correct |
209 ms |
28280 KB |
Output is correct |
26 |
Correct |
213 ms |
29260 KB |
Output is correct |
27 |
Correct |
205 ms |
28016 KB |
Output is correct |
28 |
Correct |
2 ms |
4444 KB |
Output is correct |
29 |
Correct |
2 ms |
4440 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 |
2 ms |
4444 KB |
Output is correct |
37 |
Correct |
19 ms |
10008 KB |
Output is correct |
38 |
Correct |
237 ms |
58820 KB |
Output is correct |
39 |
Correct |
216 ms |
57796 KB |
Output is correct |
40 |
Correct |
218 ms |
56140 KB |
Output is correct |
41 |
Correct |
203 ms |
53116 KB |
Output is correct |
42 |
Correct |
217 ms |
51812 KB |
Output is correct |
43 |
Correct |
218 ms |
46316 KB |
Output is correct |
44 |
Correct |
261 ms |
60796 KB |
Output is correct |
45 |
Correct |
275 ms |
60520 KB |
Output is correct |
46 |
Correct |
117 ms |
31576 KB |
Output is correct |
47 |
Correct |
229 ms |
50764 KB |
Output is correct |
48 |
Correct |
31 ms |
10232 KB |
Output is correct |
49 |
Correct |
392 ms |
63816 KB |
Output is correct |
50 |
Correct |
392 ms |
61768 KB |
Output is correct |
51 |
Correct |
384 ms |
61480 KB |
Output is correct |
52 |
Correct |
410 ms |
59044 KB |
Output is correct |
53 |
Correct |
386 ms |
54004 KB |
Output is correct |
54 |
Correct |
313 ms |
40032 KB |
Output is correct |
55 |
Correct |
412 ms |
64584 KB |
Output is correct |
56 |
Correct |
411 ms |
66092 KB |
Output is correct |
57 |
Correct |
267 ms |
37692 KB |
Output is correct |
58 |
Correct |
379 ms |
55080 KB |
Output is correct |
59 |
Correct |
125 ms |
15124 KB |
Output is correct |
60 |
Correct |
369 ms |
63816 KB |
Output is correct |
61 |
Correct |
364 ms |
62084 KB |
Output is correct |
62 |
Correct |
411 ms |
65836 KB |
Output is correct |
63 |
Correct |
233 ms |
38764 KB |
Output is correct |
64 |
Correct |
2 ms |
4188 KB |
Output is correct |
65 |
Correct |
3 ms |
4440 KB |
Output is correct |
66 |
Correct |
2 ms |
4444 KB |
Output is correct |
67 |
Correct |
2 ms |
4188 KB |
Output is correct |
68 |
Correct |
2 ms |
4444 KB |
Output is correct |
69 |
Correct |
2 ms |
4448 KB |
Output is correct |
70 |
Correct |
2 ms |
4444 KB |
Output is correct |
71 |
Correct |
2 ms |
4444 KB |
Output is correct |
72 |
Correct |
2 ms |
4444 KB |
Output is correct |
73 |
Correct |
2 ms |
4444 KB |
Output is correct |
74 |
Correct |
155 ms |
34084 KB |
Output is correct |
75 |
Correct |
261 ms |
57624 KB |
Output is correct |
76 |
Correct |
231 ms |
46148 KB |
Output is correct |
77 |
Correct |
164 ms |
37064 KB |
Output is correct |
78 |
Correct |
64 ms |
13816 KB |
Output is correct |
79 |
Correct |
44 ms |
12744 KB |
Output is correct |
80 |
Correct |
110 ms |
28872 KB |
Output is correct |
81 |
Correct |
308 ms |
59216 KB |
Output is correct |
82 |
Correct |
218 ms |
46264 KB |
Output is correct |
83 |
Correct |
319 ms |
65764 KB |
Output is correct |
84 |
Correct |
120 ms |
32032 KB |
Output is correct |
85 |
Correct |
187 ms |
42620 KB |
Output is correct |
86 |
Correct |
84 ms |
16700 KB |
Output is correct |
87 |
Correct |
385 ms |
59712 KB |
Output is correct |
88 |
Correct |
383 ms |
60596 KB |
Output is correct |
89 |
Correct |
294 ms |
39976 KB |
Output is correct |
90 |
Correct |
180 ms |
18900 KB |
Output is correct |
91 |
Correct |
185 ms |
19648 KB |
Output is correct |
92 |
Correct |
270 ms |
33872 KB |
Output is correct |
93 |
Correct |
459 ms |
60740 KB |
Output is correct |
94 |
Correct |
392 ms |
51396 KB |
Output is correct |
95 |
Correct |
490 ms |
71256 KB |
Output is correct |
96 |
Correct |
304 ms |
37424 KB |
Output is correct |
97 |
Correct |
346 ms |
47012 KB |
Output is correct |