#include "swap.h"
#include<bits/stdc++.h>
using namespace std ;
const int LG = 30 , INF = 1e9 + 5 ;
struct query
{
int u = 0 , v = 0 , w = 0 ;
};
bool cmp(query &a , query &b)
{
return a.w < b.w ;
}
struct reachability_tree
{
int cur = 0 , timer = 0 ;
vector<vector<int> > adj , up , dp ;
vector<int> tin , tout , par , swp , cost , edg , deg ;
void init(int n , int m)
{
int siz = n + m + 5 ;
adj = vector<vector<int> > (siz) ;
up = vector<vector<int> > (siz , vector<int>(LG+1,0)) ;
dp = vector<vector<int> > (siz , vector<int>(LG+1,0)) ;
tin = vector<int> (siz , 0) ;
tout = vector<int> (siz , 0) ;
par = vector<int> (siz , 0) ;
cost = vector<int> (siz , INF) ;
swp = vector<int> (siz , 0) ;
edg = vector<int> (siz , 0) ;
deg = vector<int> (siz , 0) ;
for(int i = 1 ; i < siz ; ++i)
{
par[i] = i ;
}
cur = n + 1 ;
}
int find_root(int p)
{
if(par[p]==p) return p ;
return par[p] = find_root(par[p]) ;
}
void union_edge(int u , int v , int cst)
{
++deg[u] , ++deg[v] ;
if((deg[u]>=3) || (deg[v]>=3)) swp[cur] = 1 ;
int root_u = find_root(u) , root_v = find_root(v) ;
swp[cur] |= (swp[root_u]|swp[root_v]) ;
adj[cur].push_back(root_u) ;
if(root_u!=root_v) adj[cur].push_back(root_v) ;
else swp[cur] = 1 ;
par[root_u] = cur , par[root_v] = cur ;
edg[cur] = cst ;
++cur ;
}
void dfs(int s , int p)
{
dp[s][0] = p , up[s][0] = max(swp[s],swp[p]) , tin[s] = ++timer ;
for(int i = 1 ; i < LG ; ++i)
{
up[s][i] = max(up[s][i-1] , up[dp[s][i-1]][i-1]) ;
dp[s][i] = dp[dp[s][i-1]][i-1] ;
}
for(int t : adj[s])
{
if(t==p) continue ;
dfs(t,s) ;
}
tout[s] = ++timer ;
}
int is_ancestor(int u , int v)
{
return ((tin[u]<=tin[v]) && (tout[u]>=tout[v])) ;
}
int find_ancestor(int u , int v)
{
if(is_ancestor(u,v)) return u ;
if(is_ancestor(v,u)) return v ;
for(int i = LG - 1 ; i >= 0 ; --i)
{
if(!is_ancestor(dp[u][i],v))
{
u = dp[u][i] ;
}
}
return dp[u][0] ;
}
int find_nearest(int u)
{
if(swp[u]) return edg[u] ;
for(int i = LG - 1 ; i >= 0 ; --i)
{
if(!up[u][i])
{
u = dp[u][i] ;
}
}
if(!swp[dp[u][0]]) return -1 ;
return edg[dp[u][0]] ;
}
void process()
{
int root_1 = find_root(1) ;
dfs(root_1,root_1) ;
}
int query(int x , int y)
{
++x , ++y ;
int lca = find_ancestor(x,y) ;
int ret = find_nearest(lca) ;
if(ret==INF) ret = -1 ;
return ret ;
}
} R ;
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W)
{
R.init(N,M) ;
vector<query> v ;
for(int i = 0 ; i < M ; ++i)
{
v.push_back({U[i]+1,V[i]+1,W[i]}) ;
}
sort(v.begin(),v.end(),cmp) ;
for(query q : v)
{
R.union_edge(q.u,q.v,q.w) ;
}
R.process() ;
}
int getMinimumFuelCapacity(int X, int Y)
{
return R.query(X,Y) ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
980 KB |
Output is correct |
6 |
Correct |
2 ms |
980 KB |
Output is correct |
7 |
Correct |
2 ms |
1108 KB |
Output is correct |
8 |
Correct |
2 ms |
1108 KB |
Output is correct |
9 |
Correct |
172 ms |
65020 KB |
Output is correct |
10 |
Correct |
216 ms |
79552 KB |
Output is correct |
11 |
Correct |
220 ms |
78204 KB |
Output is correct |
12 |
Correct |
225 ms |
82884 KB |
Output is correct |
13 |
Correct |
290 ms |
84764 KB |
Output is correct |
14 |
Correct |
183 ms |
65016 KB |
Output is correct |
15 |
Correct |
345 ms |
81704 KB |
Output is correct |
16 |
Correct |
358 ms |
79488 KB |
Output is correct |
17 |
Correct |
367 ms |
84404 KB |
Output is correct |
18 |
Correct |
557 ms |
86376 KB |
Output is correct |
19 |
Correct |
105 ms |
16592 KB |
Output is correct |
20 |
Correct |
335 ms |
82388 KB |
Output is correct |
21 |
Correct |
332 ms |
79352 KB |
Output is correct |
22 |
Correct |
342 ms |
84828 KB |
Output is correct |
23 |
Correct |
612 ms |
86800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
502 ms |
83976 KB |
Output is correct |
4 |
Correct |
470 ms |
88324 KB |
Output is correct |
5 |
Correct |
489 ms |
85400 KB |
Output is correct |
6 |
Correct |
486 ms |
87948 KB |
Output is correct |
7 |
Correct |
531 ms |
86772 KB |
Output is correct |
8 |
Correct |
476 ms |
83280 KB |
Output is correct |
9 |
Correct |
493 ms |
86296 KB |
Output is correct |
10 |
Correct |
466 ms |
82408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
980 KB |
Output is correct |
6 |
Correct |
2 ms |
980 KB |
Output is correct |
7 |
Correct |
2 ms |
1108 KB |
Output is correct |
8 |
Correct |
2 ms |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
3 ms |
1108 KB |
Output is correct |
11 |
Correct |
2 ms |
1108 KB |
Output is correct |
12 |
Correct |
2 ms |
1108 KB |
Output is correct |
13 |
Correct |
2 ms |
968 KB |
Output is correct |
14 |
Correct |
2 ms |
980 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 KB |
Output is correct |
16 |
Correct |
2 ms |
1108 KB |
Output is correct |
17 |
Correct |
2 ms |
1108 KB |
Output is correct |
18 |
Correct |
2 ms |
1108 KB |
Output is correct |
19 |
Correct |
2 ms |
980 KB |
Output is correct |
20 |
Correct |
2 ms |
1108 KB |
Output is correct |
21 |
Correct |
2 ms |
1108 KB |
Output is correct |
22 |
Correct |
3 ms |
1172 KB |
Output is correct |
23 |
Correct |
2 ms |
852 KB |
Output is correct |
24 |
Correct |
3 ms |
1492 KB |
Output is correct |
25 |
Correct |
3 ms |
1492 KB |
Output is correct |
26 |
Correct |
3 ms |
1492 KB |
Output is correct |
27 |
Correct |
2 ms |
1108 KB |
Output is correct |
28 |
Correct |
4 ms |
1492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
980 KB |
Output is correct |
7 |
Correct |
2 ms |
980 KB |
Output is correct |
8 |
Correct |
2 ms |
1108 KB |
Output is correct |
9 |
Correct |
2 ms |
1108 KB |
Output is correct |
10 |
Correct |
172 ms |
65020 KB |
Output is correct |
11 |
Correct |
216 ms |
79552 KB |
Output is correct |
12 |
Correct |
220 ms |
78204 KB |
Output is correct |
13 |
Correct |
225 ms |
82884 KB |
Output is correct |
14 |
Correct |
290 ms |
84764 KB |
Output is correct |
15 |
Correct |
3 ms |
1108 KB |
Output is correct |
16 |
Correct |
2 ms |
1108 KB |
Output is correct |
17 |
Correct |
2 ms |
1108 KB |
Output is correct |
18 |
Correct |
2 ms |
968 KB |
Output is correct |
19 |
Correct |
2 ms |
980 KB |
Output is correct |
20 |
Correct |
2 ms |
1108 KB |
Output is correct |
21 |
Correct |
2 ms |
1108 KB |
Output is correct |
22 |
Correct |
2 ms |
1108 KB |
Output is correct |
23 |
Correct |
2 ms |
1108 KB |
Output is correct |
24 |
Correct |
2 ms |
980 KB |
Output is correct |
25 |
Correct |
2 ms |
1108 KB |
Output is correct |
26 |
Correct |
2 ms |
1108 KB |
Output is correct |
27 |
Correct |
3 ms |
1172 KB |
Output is correct |
28 |
Correct |
2 ms |
852 KB |
Output is correct |
29 |
Correct |
3 ms |
1492 KB |
Output is correct |
30 |
Correct |
3 ms |
1492 KB |
Output is correct |
31 |
Correct |
3 ms |
1492 KB |
Output is correct |
32 |
Correct |
2 ms |
1108 KB |
Output is correct |
33 |
Correct |
4 ms |
1492 KB |
Output is correct |
34 |
Correct |
27 ms |
11544 KB |
Output is correct |
35 |
Correct |
231 ms |
82840 KB |
Output is correct |
36 |
Correct |
213 ms |
82872 KB |
Output is correct |
37 |
Correct |
238 ms |
82756 KB |
Output is correct |
38 |
Correct |
230 ms |
81600 KB |
Output is correct |
39 |
Correct |
215 ms |
81184 KB |
Output is correct |
40 |
Correct |
206 ms |
74528 KB |
Output is correct |
41 |
Correct |
230 ms |
82868 KB |
Output is correct |
42 |
Correct |
233 ms |
82872 KB |
Output is correct |
43 |
Correct |
300 ms |
85396 KB |
Output is correct |
44 |
Correct |
224 ms |
82920 KB |
Output is correct |
45 |
Correct |
353 ms |
99276 KB |
Output is correct |
46 |
Correct |
230 ms |
82828 KB |
Output is correct |
47 |
Correct |
238 ms |
82820 KB |
Output is correct |
48 |
Correct |
284 ms |
88380 KB |
Output is correct |
49 |
Correct |
236 ms |
77288 KB |
Output is correct |
50 |
Correct |
200 ms |
61484 KB |
Output is correct |
51 |
Correct |
259 ms |
85016 KB |
Output is correct |
52 |
Correct |
332 ms |
112724 KB |
Output is correct |
53 |
Correct |
382 ms |
120380 KB |
Output is correct |
54 |
Correct |
437 ms |
131404 KB |
Output is correct |
55 |
Correct |
290 ms |
85172 KB |
Output is correct |
56 |
Correct |
387 ms |
118784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
980 KB |
Output is correct |
6 |
Correct |
2 ms |
980 KB |
Output is correct |
7 |
Correct |
2 ms |
1108 KB |
Output is correct |
8 |
Correct |
2 ms |
1108 KB |
Output is correct |
9 |
Correct |
172 ms |
65020 KB |
Output is correct |
10 |
Correct |
216 ms |
79552 KB |
Output is correct |
11 |
Correct |
220 ms |
78204 KB |
Output is correct |
12 |
Correct |
225 ms |
82884 KB |
Output is correct |
13 |
Correct |
290 ms |
84764 KB |
Output is correct |
14 |
Correct |
183 ms |
65016 KB |
Output is correct |
15 |
Correct |
345 ms |
81704 KB |
Output is correct |
16 |
Correct |
358 ms |
79488 KB |
Output is correct |
17 |
Correct |
367 ms |
84404 KB |
Output is correct |
18 |
Correct |
557 ms |
86376 KB |
Output is correct |
19 |
Correct |
502 ms |
83976 KB |
Output is correct |
20 |
Correct |
470 ms |
88324 KB |
Output is correct |
21 |
Correct |
489 ms |
85400 KB |
Output is correct |
22 |
Correct |
486 ms |
87948 KB |
Output is correct |
23 |
Correct |
531 ms |
86772 KB |
Output is correct |
24 |
Correct |
476 ms |
83280 KB |
Output is correct |
25 |
Correct |
493 ms |
86296 KB |
Output is correct |
26 |
Correct |
466 ms |
82408 KB |
Output is correct |
27 |
Correct |
3 ms |
1108 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
2 ms |
1108 KB |
Output is correct |
30 |
Correct |
2 ms |
968 KB |
Output is correct |
31 |
Correct |
2 ms |
980 KB |
Output is correct |
32 |
Correct |
2 ms |
1108 KB |
Output is correct |
33 |
Correct |
2 ms |
1108 KB |
Output is correct |
34 |
Correct |
2 ms |
1108 KB |
Output is correct |
35 |
Correct |
2 ms |
1108 KB |
Output is correct |
36 |
Correct |
27 ms |
11544 KB |
Output is correct |
37 |
Correct |
231 ms |
82840 KB |
Output is correct |
38 |
Correct |
213 ms |
82872 KB |
Output is correct |
39 |
Correct |
238 ms |
82756 KB |
Output is correct |
40 |
Correct |
230 ms |
81600 KB |
Output is correct |
41 |
Correct |
215 ms |
81184 KB |
Output is correct |
42 |
Correct |
206 ms |
74528 KB |
Output is correct |
43 |
Correct |
230 ms |
82868 KB |
Output is correct |
44 |
Correct |
233 ms |
82872 KB |
Output is correct |
45 |
Correct |
300 ms |
85396 KB |
Output is correct |
46 |
Correct |
224 ms |
82920 KB |
Output is correct |
47 |
Correct |
33 ms |
10620 KB |
Output is correct |
48 |
Correct |
358 ms |
84436 KB |
Output is correct |
49 |
Correct |
351 ms |
84412 KB |
Output is correct |
50 |
Correct |
337 ms |
84164 KB |
Output is correct |
51 |
Correct |
315 ms |
83776 KB |
Output is correct |
52 |
Correct |
362 ms |
78932 KB |
Output is correct |
53 |
Correct |
242 ms |
57728 KB |
Output is correct |
54 |
Correct |
381 ms |
84808 KB |
Output is correct |
55 |
Correct |
345 ms |
84432 KB |
Output is correct |
56 |
Correct |
496 ms |
86440 KB |
Output is correct |
57 |
Correct |
350 ms |
84872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
980 KB |
Output is correct |
7 |
Correct |
2 ms |
980 KB |
Output is correct |
8 |
Correct |
2 ms |
1108 KB |
Output is correct |
9 |
Correct |
2 ms |
1108 KB |
Output is correct |
10 |
Correct |
172 ms |
65020 KB |
Output is correct |
11 |
Correct |
216 ms |
79552 KB |
Output is correct |
12 |
Correct |
220 ms |
78204 KB |
Output is correct |
13 |
Correct |
225 ms |
82884 KB |
Output is correct |
14 |
Correct |
290 ms |
84764 KB |
Output is correct |
15 |
Correct |
183 ms |
65016 KB |
Output is correct |
16 |
Correct |
345 ms |
81704 KB |
Output is correct |
17 |
Correct |
358 ms |
79488 KB |
Output is correct |
18 |
Correct |
367 ms |
84404 KB |
Output is correct |
19 |
Correct |
557 ms |
86376 KB |
Output is correct |
20 |
Correct |
502 ms |
83976 KB |
Output is correct |
21 |
Correct |
470 ms |
88324 KB |
Output is correct |
22 |
Correct |
489 ms |
85400 KB |
Output is correct |
23 |
Correct |
486 ms |
87948 KB |
Output is correct |
24 |
Correct |
531 ms |
86772 KB |
Output is correct |
25 |
Correct |
476 ms |
83280 KB |
Output is correct |
26 |
Correct |
493 ms |
86296 KB |
Output is correct |
27 |
Correct |
466 ms |
82408 KB |
Output is correct |
28 |
Correct |
3 ms |
1108 KB |
Output is correct |
29 |
Correct |
2 ms |
1108 KB |
Output is correct |
30 |
Correct |
2 ms |
1108 KB |
Output is correct |
31 |
Correct |
2 ms |
968 KB |
Output is correct |
32 |
Correct |
2 ms |
980 KB |
Output is correct |
33 |
Correct |
2 ms |
1108 KB |
Output is correct |
34 |
Correct |
2 ms |
1108 KB |
Output is correct |
35 |
Correct |
2 ms |
1108 KB |
Output is correct |
36 |
Correct |
2 ms |
1108 KB |
Output is correct |
37 |
Correct |
27 ms |
11544 KB |
Output is correct |
38 |
Correct |
231 ms |
82840 KB |
Output is correct |
39 |
Correct |
213 ms |
82872 KB |
Output is correct |
40 |
Correct |
238 ms |
82756 KB |
Output is correct |
41 |
Correct |
230 ms |
81600 KB |
Output is correct |
42 |
Correct |
215 ms |
81184 KB |
Output is correct |
43 |
Correct |
206 ms |
74528 KB |
Output is correct |
44 |
Correct |
230 ms |
82868 KB |
Output is correct |
45 |
Correct |
233 ms |
82872 KB |
Output is correct |
46 |
Correct |
300 ms |
85396 KB |
Output is correct |
47 |
Correct |
224 ms |
82920 KB |
Output is correct |
48 |
Correct |
33 ms |
10620 KB |
Output is correct |
49 |
Correct |
358 ms |
84436 KB |
Output is correct |
50 |
Correct |
351 ms |
84412 KB |
Output is correct |
51 |
Correct |
337 ms |
84164 KB |
Output is correct |
52 |
Correct |
315 ms |
83776 KB |
Output is correct |
53 |
Correct |
362 ms |
78932 KB |
Output is correct |
54 |
Correct |
242 ms |
57728 KB |
Output is correct |
55 |
Correct |
381 ms |
84808 KB |
Output is correct |
56 |
Correct |
345 ms |
84432 KB |
Output is correct |
57 |
Correct |
496 ms |
86440 KB |
Output is correct |
58 |
Correct |
350 ms |
84872 KB |
Output is correct |
59 |
Correct |
105 ms |
16592 KB |
Output is correct |
60 |
Correct |
335 ms |
82388 KB |
Output is correct |
61 |
Correct |
332 ms |
79352 KB |
Output is correct |
62 |
Correct |
342 ms |
84828 KB |
Output is correct |
63 |
Correct |
612 ms |
86800 KB |
Output is correct |
64 |
Correct |
2 ms |
980 KB |
Output is correct |
65 |
Correct |
2 ms |
1108 KB |
Output is correct |
66 |
Correct |
2 ms |
1108 KB |
Output is correct |
67 |
Correct |
3 ms |
1172 KB |
Output is correct |
68 |
Correct |
2 ms |
852 KB |
Output is correct |
69 |
Correct |
3 ms |
1492 KB |
Output is correct |
70 |
Correct |
3 ms |
1492 KB |
Output is correct |
71 |
Correct |
3 ms |
1492 KB |
Output is correct |
72 |
Correct |
2 ms |
1108 KB |
Output is correct |
73 |
Correct |
4 ms |
1492 KB |
Output is correct |
74 |
Correct |
353 ms |
99276 KB |
Output is correct |
75 |
Correct |
230 ms |
82828 KB |
Output is correct |
76 |
Correct |
238 ms |
82820 KB |
Output is correct |
77 |
Correct |
284 ms |
88380 KB |
Output is correct |
78 |
Correct |
236 ms |
77288 KB |
Output is correct |
79 |
Correct |
200 ms |
61484 KB |
Output is correct |
80 |
Correct |
259 ms |
85016 KB |
Output is correct |
81 |
Correct |
332 ms |
112724 KB |
Output is correct |
82 |
Correct |
382 ms |
120380 KB |
Output is correct |
83 |
Correct |
437 ms |
131404 KB |
Output is correct |
84 |
Correct |
290 ms |
85172 KB |
Output is correct |
85 |
Correct |
387 ms |
118784 KB |
Output is correct |
86 |
Correct |
133 ms |
35924 KB |
Output is correct |
87 |
Correct |
334 ms |
84424 KB |
Output is correct |
88 |
Correct |
327 ms |
84360 KB |
Output is correct |
89 |
Correct |
469 ms |
85444 KB |
Output is correct |
90 |
Correct |
328 ms |
83404 KB |
Output is correct |
91 |
Correct |
346 ms |
77944 KB |
Output is correct |
92 |
Correct |
473 ms |
83672 KB |
Output is correct |
93 |
Correct |
458 ms |
112996 KB |
Output is correct |
94 |
Correct |
566 ms |
121152 KB |
Output is correct |
95 |
Correct |
501 ms |
132896 KB |
Output is correct |
96 |
Correct |
509 ms |
86600 KB |
Output is correct |
97 |
Correct |
540 ms |
102708 KB |
Output is correct |