#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll int
#define llinf 100000000000000000LL // 10^17
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) (ll)(a.size())
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {cerr<<#a<<": ";for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define cer(a) {cerr<<#a<<": ";for(ll x_ : a) cerr<<x_<< " ";cerr<<endl;}
#define daj_mi_malo_vremena ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
using namespace std;
#define maxn 1000005
#define lg 31
ll n,m;
vector<ll> g[maxn];
struct gr{
ll x,y,w;
};
bool cmp(gr a,gr b){return a.w<b.w;}
vector<gr> edge;
ll c[maxn];
bool ok[maxn];
ll dsu[maxn];
ll deg[maxn];
ll in[maxn];
ll out[maxn];
ll it;
ll root(ll x){
while(x!=dsu[x]){
dsu[x] = dsu[dsu[x]];
x = dsu[x];
}
return x;
}
ll st[maxn][lg];
ll oke[maxn][lg];
vector<pll> v;
void upd(ll x,ll y,ll w){
deg[x]++; deg[y]++;
bool f = (deg[x]>=3)||(deg[y]>=3);
x = root(x); y = root(y);
++it;
if(x==y){
g[x].pb(it);
g[it].pb(x);
v.pb({it,x});
ok[it] = 1;
dsu[x] = it;
}else{
g[x].pb(it);
g[it].pb(x);
g[y].pb(it);
g[it].pb(y);
v.pb({it,x});
v.pb({it,y});
ok[it] = ok[x]|ok[y]|f;
dsu[x] = dsu[y] = it;
}
c[it] = w;
}
ll itr = 0;
void dfs(ll u,ll p){
st[u][0] = p;
oke[u][0] = ok[p];
in[u] = ++itr;
for(ll s : g[u]){
if(s==p) continue;
dfs(s,u);
}
out[u] = itr;
}
bool intree(ll x,ll y){
return in[y]<=in[x]&&out[x]<=out[y];
}
ll lca(ll x,ll y){
if(intree(y,x)) return x;
if(intree(x,y)) return y;
for(ll j = lg-1;j>=0;j--){
if(!intree(x,st[y][j])) y = st[y][j];
}
return st[y][0];
}
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
n = N; m = M;
it = n;
iota(dsu+1,dsu+maxn,1);
for(ll i = 0;i<m;i++){
ll x = U[i]; ll y = V[i]; ll w = W[i];
++x; ++y;
edge.pb({x,y,w});
}
sort(all(edge),cmp);
for(gr e : edge){
ll x = e.x; ll y = e.y; ll w = e.w;
upd(x,y,w);
}
dfs(it,it);
for(ll j = 1;j<lg;j++){
for(ll i = 1;i<=it;i++) st[i][j] = st[st[i][j-1]][j-1];
}
for(ll j = 1;j<lg;j++){
for(ll i = 1;i<=it;i++) oke[i][j] = oke[i][j-1]|oke[st[i][j-1]][j-1];
}
}
int getMinimumFuelCapacity(int X, int Y) {
ll x = X; ll y = Y;
++x; ++y;
ll z = lca(x,y);
if(ok[z]) return c[z];
for(ll j = lg-1;j>=0;j--){
if(!oke[z][j]) z = st[z][j];
}
z = st[z][0];
ll ans = c[z];
if(ok[z]==0) ans = -1;
return ans;
}
/*
5 6
0 1 4
0 2 4
1 2 1
1 3 2
1 4 10
2 3 3
3
1 2
2 4
0 1
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
27732 KB |
Output is correct |
2 |
Correct |
14 ms |
27644 KB |
Output is correct |
3 |
Correct |
18 ms |
27748 KB |
Output is correct |
4 |
Correct |
15 ms |
28024 KB |
Output is correct |
5 |
Correct |
14 ms |
28244 KB |
Output is correct |
6 |
Correct |
15 ms |
28212 KB |
Output is correct |
7 |
Correct |
16 ms |
28372 KB |
Output is correct |
8 |
Correct |
17 ms |
28280 KB |
Output is correct |
9 |
Correct |
237 ms |
76684 KB |
Output is correct |
10 |
Correct |
280 ms |
87660 KB |
Output is correct |
11 |
Correct |
262 ms |
86712 KB |
Output is correct |
12 |
Correct |
300 ms |
90176 KB |
Output is correct |
13 |
Correct |
266 ms |
92428 KB |
Output is correct |
14 |
Correct |
224 ms |
76656 KB |
Output is correct |
15 |
Correct |
381 ms |
89984 KB |
Output is correct |
16 |
Correct |
402 ms |
88160 KB |
Output is correct |
17 |
Correct |
415 ms |
91944 KB |
Output is correct |
18 |
Correct |
534 ms |
94256 KB |
Output is correct |
19 |
Correct |
120 ms |
41024 KB |
Output is correct |
20 |
Correct |
384 ms |
91256 KB |
Output is correct |
21 |
Correct |
380 ms |
89008 KB |
Output is correct |
22 |
Correct |
393 ms |
93220 KB |
Output is correct |
23 |
Correct |
515 ms |
95612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
27732 KB |
Output is correct |
2 |
Correct |
14 ms |
27644 KB |
Output is correct |
3 |
Correct |
418 ms |
94104 KB |
Output is correct |
4 |
Correct |
446 ms |
97528 KB |
Output is correct |
5 |
Correct |
421 ms |
95316 KB |
Output is correct |
6 |
Correct |
440 ms |
97304 KB |
Output is correct |
7 |
Correct |
417 ms |
96360 KB |
Output is correct |
8 |
Correct |
423 ms |
93632 KB |
Output is correct |
9 |
Correct |
434 ms |
95896 KB |
Output is correct |
10 |
Correct |
441 ms |
92992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
27732 KB |
Output is correct |
2 |
Correct |
14 ms |
27644 KB |
Output is correct |
3 |
Correct |
18 ms |
27748 KB |
Output is correct |
4 |
Correct |
15 ms |
28024 KB |
Output is correct |
5 |
Correct |
14 ms |
28244 KB |
Output is correct |
6 |
Correct |
15 ms |
28212 KB |
Output is correct |
7 |
Correct |
16 ms |
28372 KB |
Output is correct |
8 |
Correct |
17 ms |
28280 KB |
Output is correct |
9 |
Correct |
14 ms |
27732 KB |
Output is correct |
10 |
Correct |
15 ms |
28344 KB |
Output is correct |
11 |
Correct |
15 ms |
28380 KB |
Output is correct |
12 |
Correct |
15 ms |
28340 KB |
Output is correct |
13 |
Correct |
16 ms |
28272 KB |
Output is correct |
14 |
Correct |
20 ms |
28396 KB |
Output is correct |
15 |
Correct |
18 ms |
28316 KB |
Output is correct |
16 |
Correct |
15 ms |
28372 KB |
Output is correct |
17 |
Correct |
15 ms |
28372 KB |
Output is correct |
18 |
Correct |
17 ms |
28400 KB |
Output is correct |
19 |
Correct |
15 ms |
28244 KB |
Output is correct |
20 |
Correct |
15 ms |
28372 KB |
Output is correct |
21 |
Correct |
16 ms |
28372 KB |
Output is correct |
22 |
Correct |
16 ms |
28392 KB |
Output is correct |
23 |
Correct |
15 ms |
28188 KB |
Output is correct |
24 |
Correct |
16 ms |
28628 KB |
Output is correct |
25 |
Correct |
16 ms |
28628 KB |
Output is correct |
26 |
Correct |
17 ms |
28756 KB |
Output is correct |
27 |
Correct |
18 ms |
28428 KB |
Output is correct |
28 |
Correct |
19 ms |
28736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
27732 KB |
Output is correct |
2 |
Correct |
16 ms |
27732 KB |
Output is correct |
3 |
Correct |
14 ms |
27644 KB |
Output is correct |
4 |
Correct |
18 ms |
27748 KB |
Output is correct |
5 |
Correct |
15 ms |
28024 KB |
Output is correct |
6 |
Correct |
14 ms |
28244 KB |
Output is correct |
7 |
Correct |
15 ms |
28212 KB |
Output is correct |
8 |
Correct |
16 ms |
28372 KB |
Output is correct |
9 |
Correct |
17 ms |
28280 KB |
Output is correct |
10 |
Correct |
237 ms |
76684 KB |
Output is correct |
11 |
Correct |
280 ms |
87660 KB |
Output is correct |
12 |
Correct |
262 ms |
86712 KB |
Output is correct |
13 |
Correct |
300 ms |
90176 KB |
Output is correct |
14 |
Correct |
266 ms |
92428 KB |
Output is correct |
15 |
Correct |
15 ms |
28344 KB |
Output is correct |
16 |
Correct |
15 ms |
28380 KB |
Output is correct |
17 |
Correct |
15 ms |
28340 KB |
Output is correct |
18 |
Correct |
16 ms |
28272 KB |
Output is correct |
19 |
Correct |
20 ms |
28396 KB |
Output is correct |
20 |
Correct |
18 ms |
28316 KB |
Output is correct |
21 |
Correct |
15 ms |
28372 KB |
Output is correct |
22 |
Correct |
15 ms |
28372 KB |
Output is correct |
23 |
Correct |
17 ms |
28400 KB |
Output is correct |
24 |
Correct |
15 ms |
28244 KB |
Output is correct |
25 |
Correct |
15 ms |
28372 KB |
Output is correct |
26 |
Correct |
16 ms |
28372 KB |
Output is correct |
27 |
Correct |
16 ms |
28392 KB |
Output is correct |
28 |
Correct |
15 ms |
28188 KB |
Output is correct |
29 |
Correct |
16 ms |
28628 KB |
Output is correct |
30 |
Correct |
16 ms |
28628 KB |
Output is correct |
31 |
Correct |
17 ms |
28756 KB |
Output is correct |
32 |
Correct |
18 ms |
28428 KB |
Output is correct |
33 |
Correct |
19 ms |
28736 KB |
Output is correct |
34 |
Correct |
38 ms |
36136 KB |
Output is correct |
35 |
Correct |
270 ms |
90080 KB |
Output is correct |
36 |
Correct |
273 ms |
90148 KB |
Output is correct |
37 |
Correct |
289 ms |
90104 KB |
Output is correct |
38 |
Correct |
264 ms |
89168 KB |
Output is correct |
39 |
Correct |
293 ms |
88868 KB |
Output is correct |
40 |
Correct |
244 ms |
83780 KB |
Output is correct |
41 |
Correct |
275 ms |
90084 KB |
Output is correct |
42 |
Correct |
286 ms |
90132 KB |
Output is correct |
43 |
Correct |
262 ms |
93180 KB |
Output is correct |
44 |
Correct |
288 ms |
90004 KB |
Output is correct |
45 |
Correct |
317 ms |
103696 KB |
Output is correct |
46 |
Correct |
262 ms |
90076 KB |
Output is correct |
47 |
Correct |
287 ms |
90080 KB |
Output is correct |
48 |
Correct |
297 ms |
94720 KB |
Output is correct |
49 |
Correct |
248 ms |
87608 KB |
Output is correct |
50 |
Correct |
182 ms |
75020 KB |
Output is correct |
51 |
Correct |
311 ms |
92776 KB |
Output is correct |
52 |
Correct |
371 ms |
113268 KB |
Output is correct |
53 |
Correct |
401 ms |
118768 KB |
Output is correct |
54 |
Correct |
423 ms |
127776 KB |
Output is correct |
55 |
Correct |
291 ms |
92860 KB |
Output is correct |
56 |
Correct |
389 ms |
118516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
27732 KB |
Output is correct |
2 |
Correct |
14 ms |
27644 KB |
Output is correct |
3 |
Correct |
18 ms |
27748 KB |
Output is correct |
4 |
Correct |
15 ms |
28024 KB |
Output is correct |
5 |
Correct |
14 ms |
28244 KB |
Output is correct |
6 |
Correct |
15 ms |
28212 KB |
Output is correct |
7 |
Correct |
16 ms |
28372 KB |
Output is correct |
8 |
Correct |
17 ms |
28280 KB |
Output is correct |
9 |
Correct |
237 ms |
76684 KB |
Output is correct |
10 |
Correct |
280 ms |
87660 KB |
Output is correct |
11 |
Correct |
262 ms |
86712 KB |
Output is correct |
12 |
Correct |
300 ms |
90176 KB |
Output is correct |
13 |
Correct |
266 ms |
92428 KB |
Output is correct |
14 |
Correct |
224 ms |
76656 KB |
Output is correct |
15 |
Correct |
381 ms |
89984 KB |
Output is correct |
16 |
Correct |
402 ms |
88160 KB |
Output is correct |
17 |
Correct |
415 ms |
91944 KB |
Output is correct |
18 |
Correct |
534 ms |
94256 KB |
Output is correct |
19 |
Correct |
418 ms |
94104 KB |
Output is correct |
20 |
Correct |
446 ms |
97528 KB |
Output is correct |
21 |
Correct |
421 ms |
95316 KB |
Output is correct |
22 |
Correct |
440 ms |
97304 KB |
Output is correct |
23 |
Correct |
417 ms |
96360 KB |
Output is correct |
24 |
Correct |
423 ms |
93632 KB |
Output is correct |
25 |
Correct |
434 ms |
95896 KB |
Output is correct |
26 |
Correct |
441 ms |
92992 KB |
Output is correct |
27 |
Correct |
15 ms |
28344 KB |
Output is correct |
28 |
Correct |
15 ms |
28380 KB |
Output is correct |
29 |
Correct |
15 ms |
28340 KB |
Output is correct |
30 |
Correct |
16 ms |
28272 KB |
Output is correct |
31 |
Correct |
20 ms |
28396 KB |
Output is correct |
32 |
Correct |
18 ms |
28316 KB |
Output is correct |
33 |
Correct |
15 ms |
28372 KB |
Output is correct |
34 |
Correct |
15 ms |
28372 KB |
Output is correct |
35 |
Correct |
17 ms |
28400 KB |
Output is correct |
36 |
Correct |
38 ms |
36136 KB |
Output is correct |
37 |
Correct |
270 ms |
90080 KB |
Output is correct |
38 |
Correct |
273 ms |
90148 KB |
Output is correct |
39 |
Correct |
289 ms |
90104 KB |
Output is correct |
40 |
Correct |
264 ms |
89168 KB |
Output is correct |
41 |
Correct |
293 ms |
88868 KB |
Output is correct |
42 |
Correct |
244 ms |
83780 KB |
Output is correct |
43 |
Correct |
275 ms |
90084 KB |
Output is correct |
44 |
Correct |
286 ms |
90132 KB |
Output is correct |
45 |
Correct |
262 ms |
93180 KB |
Output is correct |
46 |
Correct |
288 ms |
90004 KB |
Output is correct |
47 |
Correct |
40 ms |
35724 KB |
Output is correct |
48 |
Correct |
387 ms |
92544 KB |
Output is correct |
49 |
Correct |
397 ms |
92624 KB |
Output is correct |
50 |
Correct |
398 ms |
92376 KB |
Output is correct |
51 |
Correct |
388 ms |
92124 KB |
Output is correct |
52 |
Correct |
366 ms |
88552 KB |
Output is correct |
53 |
Correct |
286 ms |
72568 KB |
Output is correct |
54 |
Correct |
412 ms |
93164 KB |
Output is correct |
55 |
Correct |
377 ms |
92640 KB |
Output is correct |
56 |
Correct |
454 ms |
95012 KB |
Output is correct |
57 |
Correct |
399 ms |
93220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
27732 KB |
Output is correct |
2 |
Correct |
16 ms |
27732 KB |
Output is correct |
3 |
Correct |
14 ms |
27644 KB |
Output is correct |
4 |
Correct |
18 ms |
27748 KB |
Output is correct |
5 |
Correct |
15 ms |
28024 KB |
Output is correct |
6 |
Correct |
14 ms |
28244 KB |
Output is correct |
7 |
Correct |
15 ms |
28212 KB |
Output is correct |
8 |
Correct |
16 ms |
28372 KB |
Output is correct |
9 |
Correct |
17 ms |
28280 KB |
Output is correct |
10 |
Correct |
237 ms |
76684 KB |
Output is correct |
11 |
Correct |
280 ms |
87660 KB |
Output is correct |
12 |
Correct |
262 ms |
86712 KB |
Output is correct |
13 |
Correct |
300 ms |
90176 KB |
Output is correct |
14 |
Correct |
266 ms |
92428 KB |
Output is correct |
15 |
Correct |
224 ms |
76656 KB |
Output is correct |
16 |
Correct |
381 ms |
89984 KB |
Output is correct |
17 |
Correct |
402 ms |
88160 KB |
Output is correct |
18 |
Correct |
415 ms |
91944 KB |
Output is correct |
19 |
Correct |
534 ms |
94256 KB |
Output is correct |
20 |
Correct |
418 ms |
94104 KB |
Output is correct |
21 |
Correct |
446 ms |
97528 KB |
Output is correct |
22 |
Correct |
421 ms |
95316 KB |
Output is correct |
23 |
Correct |
440 ms |
97304 KB |
Output is correct |
24 |
Correct |
417 ms |
96360 KB |
Output is correct |
25 |
Correct |
423 ms |
93632 KB |
Output is correct |
26 |
Correct |
434 ms |
95896 KB |
Output is correct |
27 |
Correct |
441 ms |
92992 KB |
Output is correct |
28 |
Correct |
15 ms |
28344 KB |
Output is correct |
29 |
Correct |
15 ms |
28380 KB |
Output is correct |
30 |
Correct |
15 ms |
28340 KB |
Output is correct |
31 |
Correct |
16 ms |
28272 KB |
Output is correct |
32 |
Correct |
20 ms |
28396 KB |
Output is correct |
33 |
Correct |
18 ms |
28316 KB |
Output is correct |
34 |
Correct |
15 ms |
28372 KB |
Output is correct |
35 |
Correct |
15 ms |
28372 KB |
Output is correct |
36 |
Correct |
17 ms |
28400 KB |
Output is correct |
37 |
Correct |
38 ms |
36136 KB |
Output is correct |
38 |
Correct |
270 ms |
90080 KB |
Output is correct |
39 |
Correct |
273 ms |
90148 KB |
Output is correct |
40 |
Correct |
289 ms |
90104 KB |
Output is correct |
41 |
Correct |
264 ms |
89168 KB |
Output is correct |
42 |
Correct |
293 ms |
88868 KB |
Output is correct |
43 |
Correct |
244 ms |
83780 KB |
Output is correct |
44 |
Correct |
275 ms |
90084 KB |
Output is correct |
45 |
Correct |
286 ms |
90132 KB |
Output is correct |
46 |
Correct |
262 ms |
93180 KB |
Output is correct |
47 |
Correct |
288 ms |
90004 KB |
Output is correct |
48 |
Correct |
40 ms |
35724 KB |
Output is correct |
49 |
Correct |
387 ms |
92544 KB |
Output is correct |
50 |
Correct |
397 ms |
92624 KB |
Output is correct |
51 |
Correct |
398 ms |
92376 KB |
Output is correct |
52 |
Correct |
388 ms |
92124 KB |
Output is correct |
53 |
Correct |
366 ms |
88552 KB |
Output is correct |
54 |
Correct |
286 ms |
72568 KB |
Output is correct |
55 |
Correct |
412 ms |
93164 KB |
Output is correct |
56 |
Correct |
377 ms |
92640 KB |
Output is correct |
57 |
Correct |
454 ms |
95012 KB |
Output is correct |
58 |
Correct |
399 ms |
93220 KB |
Output is correct |
59 |
Correct |
120 ms |
41024 KB |
Output is correct |
60 |
Correct |
384 ms |
91256 KB |
Output is correct |
61 |
Correct |
380 ms |
89008 KB |
Output is correct |
62 |
Correct |
393 ms |
93220 KB |
Output is correct |
63 |
Correct |
515 ms |
95612 KB |
Output is correct |
64 |
Correct |
15 ms |
28244 KB |
Output is correct |
65 |
Correct |
15 ms |
28372 KB |
Output is correct |
66 |
Correct |
16 ms |
28372 KB |
Output is correct |
67 |
Correct |
16 ms |
28392 KB |
Output is correct |
68 |
Correct |
15 ms |
28188 KB |
Output is correct |
69 |
Correct |
16 ms |
28628 KB |
Output is correct |
70 |
Correct |
16 ms |
28628 KB |
Output is correct |
71 |
Correct |
17 ms |
28756 KB |
Output is correct |
72 |
Correct |
18 ms |
28428 KB |
Output is correct |
73 |
Correct |
19 ms |
28736 KB |
Output is correct |
74 |
Correct |
317 ms |
103696 KB |
Output is correct |
75 |
Correct |
262 ms |
90076 KB |
Output is correct |
76 |
Correct |
287 ms |
90080 KB |
Output is correct |
77 |
Correct |
297 ms |
94720 KB |
Output is correct |
78 |
Correct |
248 ms |
87608 KB |
Output is correct |
79 |
Correct |
182 ms |
75020 KB |
Output is correct |
80 |
Correct |
311 ms |
92776 KB |
Output is correct |
81 |
Correct |
371 ms |
113268 KB |
Output is correct |
82 |
Correct |
401 ms |
118768 KB |
Output is correct |
83 |
Correct |
423 ms |
127776 KB |
Output is correct |
84 |
Correct |
291 ms |
92860 KB |
Output is correct |
85 |
Correct |
389 ms |
118516 KB |
Output is correct |
86 |
Correct |
160 ms |
55156 KB |
Output is correct |
87 |
Correct |
378 ms |
92560 KB |
Output is correct |
88 |
Correct |
383 ms |
92664 KB |
Output is correct |
89 |
Correct |
492 ms |
93884 KB |
Output is correct |
90 |
Correct |
340 ms |
93128 KB |
Output is correct |
91 |
Correct |
356 ms |
89236 KB |
Output is correct |
92 |
Correct |
443 ms |
93280 KB |
Output is correct |
93 |
Correct |
508 ms |
114552 KB |
Output is correct |
94 |
Correct |
622 ms |
120536 KB |
Output is correct |
95 |
Correct |
566 ms |
129464 KB |
Output is correct |
96 |
Correct |
442 ms |
95268 KB |
Output is correct |
97 |
Correct |
527 ms |
107400 KB |
Output is correct |