#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
#define TL ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define rall(s) s.rbegin(),s.rend()
#define all(s) s.begin(),s.end()
#define pb push_back
#define se second
#define fi first
#define ll long long
#define ld long double
#define YES cout<<"YES\n"
#define Yes cout<<"Yes\n"
#define yes cout<<"yes\n"
#define NO cout<<"NO\n"
#define No cout<<"No\n"
#define no cout<<"no\n"
const int N =2e5 + 9 , mod = 1e9 + 7;
ll p[N], sz[N] , a[N] , b[N] , dp[N] , dp1[N] , c[N][25] , d[N] , par[N][25] , mx[N][25] , us[N] , us1[N];
vector<pair<ll,ll>>v[N] , v1[N] , vc[N] , vv[N];
int get(int x){
return (p[x] == x ? x : get(p[x]));
}
void join(int X , int Y , ll k){
int x = get(X) , y = get(Y);
if(x != y){
if(sz[x] < sz[y])
swap(x , y);
p[y] = x;
sz[x] += sz[y];
vv[X].pb({k , Y});
vv[Y].pb({k , X});
}else {
v1[X].pb({k , Y});
v1[Y].pb({k , X});
}
}
ll timer = 0;
void dd(int n ,int p = 0 , int k = 0){
if(p)
v[p].pb({k , n});
for(auto to : vv[n])
if(to.se != p)
dd(to.se , n , to.fi);
}
void dfs(int n , int p = 0 , ll k = 1e18){
par[n][0] = p;
if(v[p].size())
c[n][0] = v[p][0].fi;
else
c[n][0] = 1e18;
if(v[p].size() && v[p][0].se == n){
if(v[p].size() > 1)
c[n][0] = v[p][1].fi;
else
c[n][0] = 1e18;
}
if(v1[p].size())
c[n][0] = min(c[n][0] , v1[p][0].fi);
if(v1[n].size())
c[n][0] = min(c[n][0] , v1[n][0].fi);
mx[n][0] = k;
us[n] = ++timer;
for(int i = 1; i <= 20; i++){
par[n][i] = par[par[n][i - 1]][i - 1] ;
mx[n][i] = max(mx[n][i - 1] , mx[par[n][i - 1]][i - 1]);
c[n][i] = min(c[n][i - 1] , c[par[n][i - 1]][i - 1]);
}
for(auto to : v[n])
dfs(to.se , n , to.fi);
if(v1[n].size())
dp[n] = min(dp[n] , v1[n][0].fi);
if(v[n].size() > 1)
dp[n] = min(dp[n] , v[n][1].fi);
dp[p] = min(dp[p] , max(k , dp[n]));
us1[n] = ++timer;
}
void dfs1(int n , int p = 0 , ll k = 1e18 , ll ans = 1e18){
if(v1[n].size())
ans = min(ans , v1[n][0].fi);
dp1[n] = ans;
v[n].pb({k , p});
for(auto to : v[n])
if(to.se != p)
vc[n].pb({max(to.fi , dp[to.se]) , to.se});
sort(all(v[n]));
sort(all(vc[n]));
for(auto to : v[n]){
if(to.se != p){
ll ans1 = ans;
if(v[n].size() >= 3){
if(v[n][1].se == to.se || v[n][0].se == to.se)
ans1 = min(ans1, v[n][2].fi);
else
ans1 = min(ans1 , v[n][1].fi);
}
if(vc[n][0].se != to.se)
ans1 = min(ans1 ,vc[n][0].fi);
else if(vc[n].size() > 1)
ans1 = min(ans1 , vc[n][1].fi);
dfs1(to.se , n , to.fi , max(ans1 , to.fi));
}
}
}
bool ok(int u, int v)
{
return (u == 0 || (us[u] <= us[v] && us1[u] >= us1[v]));
}
ll get(int x , int y){
ll mn = 1e18 , ans = 0;
mn = min({mn , dp[y]});
for(int i = 18; i >= 0; i--){
if(!ok(par[y][i] , x)){
mn = min(mn , c[y][i]);
ans = max(ans , mx[y][i]);
y = par[y][i];
}
}
ans = max(ans , mx[y][0]);
mn = min({mn , dp1[y]});
ans = max(ans , mn);
if(ans > 1e9)
ans = -1;
return ans;
}
ll mm = 0;
ll lca(int x , int y){
if(ok(x , y)){
return get(x , y);
}
if(ok(y , x))
return get(y , x);
ll mn = 1e18 , ans = 0;
mn = min({mn , dp[x] , dp[y]});
for(int i = 18; i >= 0; i--){
if(!ok(par[y][i] , x)){
mn = min(mn , c[y][i]);
ans = max(ans , mx[y][i]);
y = par[y][i];
}
}
for(int i = 18; i >= 0; i--){
if(!ok(par[x][i] , y)){
mn = min(mn , c[x][i]);
ans = max(ans , mx[x][i]);
x = par[x][i];
}
}
ans = max(ans , mx[x][0]);
ans = max(ans , mx[y][0]);
for(int i = 0; i < 3 && i < v[par[x][0]].size(); i++)
if(v[par[x][0]][i].se != x && v[par[x][0]][i].se != y)
mn = min(mn ,v[par[x][0]][i].fi);
x = par[x][0];
if(v1[x].size())
mn = min(mn , v1[x][0].fi);
ans = max(ans , mn);
if(ans > 1e9)
ans = -1;
return ans;
}
void init(int n , int m , vector<int>v , vector<int>u , vector<int>c)
{
vector<pair<ll,pair<ll,ll>>>vc;
for(int i = 0; i <= n; i++)
dp[i] = 1e18 , p[i] = i , sz[i] = 1;
for(int i = 0; i < m; i++)
vc.pb({c[i] , {v[i] + 1, u[i] + 1}});
sort(all(vc));
for(auto to : vc)
join(to.se.fi , to.se.se , to.fi);
dd(1);
for(int i = 1; i <= n ; i++)
sort(all(::v[i])) , sort(rall(::v1[i]));
dfs(1);
dfs1(1);
}
int getMinimumFuelCapacity(int l, int r) {
l++ , r++;
return lca(l , r);
}
/*
int main(){
init(4, 4, {0, 0, 1 , 2}, {1 , 3 , 2 , 3}, {1 , 3 , 2 , 4});
cout<<getMinimumFuelCapacity(0,1)<<"\n";
cout<<getMinimumFuelCapacity(0,2)<<"\n";
cout<<getMinimumFuelCapacity(0,3)<<"\n";
cout<<getMinimumFuelCapacity(1,2)<<"\n";
cout<<getMinimumFuelCapacity(1,3)<<"\n";
cout<<getMinimumFuelCapacity(2,3)<<"\n";
}*/
Compilation message
swap.cpp: In function 'long long int lca(int, int)':
swap.cpp:161:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
161 | for(int i = 0; i < 3 && i < v[par[x][0]].size(); i++)
| ~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
35416 KB |
Output is correct |
2 |
Correct |
6 ms |
35416 KB |
Output is correct |
3 |
Correct |
6 ms |
35420 KB |
Output is correct |
4 |
Correct |
7 ms |
35420 KB |
Output is correct |
5 |
Correct |
8 ms |
35672 KB |
Output is correct |
6 |
Correct |
7 ms |
35716 KB |
Output is correct |
7 |
Correct |
8 ms |
35676 KB |
Output is correct |
8 |
Correct |
8 ms |
35676 KB |
Output is correct |
9 |
Correct |
132 ms |
102804 KB |
Output is correct |
10 |
Correct |
175 ms |
116976 KB |
Output is correct |
11 |
Correct |
184 ms |
116400 KB |
Output is correct |
12 |
Correct |
176 ms |
123840 KB |
Output is correct |
13 |
Correct |
174 ms |
126516 KB |
Output is correct |
14 |
Correct |
150 ms |
101976 KB |
Output is correct |
15 |
Correct |
401 ms |
118112 KB |
Output is correct |
16 |
Correct |
390 ms |
115732 KB |
Output is correct |
17 |
Correct |
432 ms |
128192 KB |
Output is correct |
18 |
Correct |
395 ms |
126128 KB |
Output is correct |
19 |
Correct |
104 ms |
55516 KB |
Output is correct |
20 |
Correct |
392 ms |
117076 KB |
Output is correct |
21 |
Correct |
374 ms |
114908 KB |
Output is correct |
22 |
Correct |
426 ms |
124712 KB |
Output is correct |
23 |
Correct |
389 ms |
125072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
35416 KB |
Output is correct |
2 |
Correct |
6 ms |
35416 KB |
Output is correct |
3 |
Correct |
190 ms |
107200 KB |
Output is correct |
4 |
Correct |
196 ms |
113892 KB |
Output is correct |
5 |
Correct |
206 ms |
107700 KB |
Output is correct |
6 |
Correct |
192 ms |
111948 KB |
Output is correct |
7 |
Correct |
193 ms |
107624 KB |
Output is correct |
8 |
Correct |
190 ms |
107388 KB |
Output is correct |
9 |
Correct |
201 ms |
107456 KB |
Output is correct |
10 |
Correct |
188 ms |
107564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
35416 KB |
Output is correct |
2 |
Correct |
6 ms |
35416 KB |
Output is correct |
3 |
Correct |
6 ms |
35420 KB |
Output is correct |
4 |
Correct |
7 ms |
35420 KB |
Output is correct |
5 |
Correct |
8 ms |
35672 KB |
Output is correct |
6 |
Correct |
7 ms |
35716 KB |
Output is correct |
7 |
Correct |
8 ms |
35676 KB |
Output is correct |
8 |
Correct |
8 ms |
35676 KB |
Output is correct |
9 |
Correct |
7 ms |
35420 KB |
Output is correct |
10 |
Correct |
8 ms |
35664 KB |
Output is correct |
11 |
Correct |
7 ms |
35676 KB |
Output is correct |
12 |
Correct |
7 ms |
35676 KB |
Output is correct |
13 |
Correct |
7 ms |
35420 KB |
Output is correct |
14 |
Correct |
8 ms |
35420 KB |
Output is correct |
15 |
Correct |
7 ms |
35672 KB |
Output is correct |
16 |
Correct |
7 ms |
35676 KB |
Output is correct |
17 |
Correct |
7 ms |
35772 KB |
Output is correct |
18 |
Correct |
7 ms |
35420 KB |
Output is correct |
19 |
Correct |
7 ms |
35592 KB |
Output is correct |
20 |
Correct |
8 ms |
35676 KB |
Output is correct |
21 |
Correct |
7 ms |
35420 KB |
Output is correct |
22 |
Correct |
7 ms |
35672 KB |
Output is correct |
23 |
Correct |
7 ms |
35420 KB |
Output is correct |
24 |
Incorrect |
8 ms |
35676 KB |
Output isn't correct |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
35420 KB |
Output is correct |
2 |
Correct |
7 ms |
35416 KB |
Output is correct |
3 |
Correct |
6 ms |
35416 KB |
Output is correct |
4 |
Correct |
6 ms |
35420 KB |
Output is correct |
5 |
Correct |
7 ms |
35420 KB |
Output is correct |
6 |
Correct |
8 ms |
35672 KB |
Output is correct |
7 |
Correct |
7 ms |
35716 KB |
Output is correct |
8 |
Correct |
8 ms |
35676 KB |
Output is correct |
9 |
Correct |
8 ms |
35676 KB |
Output is correct |
10 |
Correct |
132 ms |
102804 KB |
Output is correct |
11 |
Correct |
175 ms |
116976 KB |
Output is correct |
12 |
Correct |
184 ms |
116400 KB |
Output is correct |
13 |
Correct |
176 ms |
123840 KB |
Output is correct |
14 |
Correct |
174 ms |
126516 KB |
Output is correct |
15 |
Correct |
8 ms |
35664 KB |
Output is correct |
16 |
Correct |
7 ms |
35676 KB |
Output is correct |
17 |
Correct |
7 ms |
35676 KB |
Output is correct |
18 |
Correct |
7 ms |
35420 KB |
Output is correct |
19 |
Correct |
8 ms |
35420 KB |
Output is correct |
20 |
Correct |
7 ms |
35672 KB |
Output is correct |
21 |
Correct |
7 ms |
35676 KB |
Output is correct |
22 |
Correct |
7 ms |
35772 KB |
Output is correct |
23 |
Correct |
7 ms |
35420 KB |
Output is correct |
24 |
Correct |
7 ms |
35592 KB |
Output is correct |
25 |
Correct |
8 ms |
35676 KB |
Output is correct |
26 |
Correct |
7 ms |
35420 KB |
Output is correct |
27 |
Correct |
7 ms |
35672 KB |
Output is correct |
28 |
Correct |
7 ms |
35420 KB |
Output is correct |
29 |
Incorrect |
8 ms |
35676 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
35416 KB |
Output is correct |
2 |
Correct |
6 ms |
35416 KB |
Output is correct |
3 |
Correct |
6 ms |
35420 KB |
Output is correct |
4 |
Correct |
7 ms |
35420 KB |
Output is correct |
5 |
Correct |
8 ms |
35672 KB |
Output is correct |
6 |
Correct |
7 ms |
35716 KB |
Output is correct |
7 |
Correct |
8 ms |
35676 KB |
Output is correct |
8 |
Correct |
8 ms |
35676 KB |
Output is correct |
9 |
Correct |
132 ms |
102804 KB |
Output is correct |
10 |
Correct |
175 ms |
116976 KB |
Output is correct |
11 |
Correct |
184 ms |
116400 KB |
Output is correct |
12 |
Correct |
176 ms |
123840 KB |
Output is correct |
13 |
Correct |
174 ms |
126516 KB |
Output is correct |
14 |
Correct |
150 ms |
101976 KB |
Output is correct |
15 |
Correct |
401 ms |
118112 KB |
Output is correct |
16 |
Correct |
390 ms |
115732 KB |
Output is correct |
17 |
Correct |
432 ms |
128192 KB |
Output is correct |
18 |
Correct |
395 ms |
126128 KB |
Output is correct |
19 |
Correct |
190 ms |
107200 KB |
Output is correct |
20 |
Correct |
196 ms |
113892 KB |
Output is correct |
21 |
Correct |
206 ms |
107700 KB |
Output is correct |
22 |
Correct |
192 ms |
111948 KB |
Output is correct |
23 |
Correct |
193 ms |
107624 KB |
Output is correct |
24 |
Correct |
190 ms |
107388 KB |
Output is correct |
25 |
Correct |
201 ms |
107456 KB |
Output is correct |
26 |
Correct |
188 ms |
107564 KB |
Output is correct |
27 |
Correct |
8 ms |
35664 KB |
Output is correct |
28 |
Correct |
7 ms |
35676 KB |
Output is correct |
29 |
Correct |
7 ms |
35676 KB |
Output is correct |
30 |
Correct |
7 ms |
35420 KB |
Output is correct |
31 |
Correct |
8 ms |
35420 KB |
Output is correct |
32 |
Correct |
7 ms |
35672 KB |
Output is correct |
33 |
Correct |
7 ms |
35676 KB |
Output is correct |
34 |
Correct |
7 ms |
35772 KB |
Output is correct |
35 |
Correct |
7 ms |
35420 KB |
Output is correct |
36 |
Correct |
19 ms |
43864 KB |
Output is correct |
37 |
Correct |
179 ms |
123540 KB |
Output is correct |
38 |
Correct |
164 ms |
119196 KB |
Output is correct |
39 |
Correct |
146 ms |
115908 KB |
Output is correct |
40 |
Correct |
174 ms |
110884 KB |
Output is correct |
41 |
Correct |
134 ms |
110016 KB |
Output is correct |
42 |
Correct |
124 ms |
106568 KB |
Output is correct |
43 |
Correct |
172 ms |
120468 KB |
Output is correct |
44 |
Correct |
185 ms |
122048 KB |
Output is correct |
45 |
Correct |
157 ms |
123772 KB |
Output is correct |
46 |
Correct |
137 ms |
114896 KB |
Output is correct |
47 |
Correct |
28 ms |
43860 KB |
Output is correct |
48 |
Correct |
395 ms |
122296 KB |
Output is correct |
49 |
Correct |
420 ms |
119204 KB |
Output is correct |
50 |
Correct |
363 ms |
117696 KB |
Output is correct |
51 |
Correct |
328 ms |
115344 KB |
Output is correct |
52 |
Correct |
297 ms |
108904 KB |
Output is correct |
53 |
Correct |
237 ms |
85876 KB |
Output is correct |
54 |
Correct |
387 ms |
120376 KB |
Output is correct |
55 |
Correct |
395 ms |
122896 KB |
Output is correct |
56 |
Correct |
422 ms |
126392 KB |
Output is correct |
57 |
Correct |
300 ms |
116008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
35420 KB |
Output is correct |
2 |
Correct |
7 ms |
35416 KB |
Output is correct |
3 |
Correct |
6 ms |
35416 KB |
Output is correct |
4 |
Correct |
6 ms |
35420 KB |
Output is correct |
5 |
Correct |
7 ms |
35420 KB |
Output is correct |
6 |
Correct |
8 ms |
35672 KB |
Output is correct |
7 |
Correct |
7 ms |
35716 KB |
Output is correct |
8 |
Correct |
8 ms |
35676 KB |
Output is correct |
9 |
Correct |
8 ms |
35676 KB |
Output is correct |
10 |
Correct |
132 ms |
102804 KB |
Output is correct |
11 |
Correct |
175 ms |
116976 KB |
Output is correct |
12 |
Correct |
184 ms |
116400 KB |
Output is correct |
13 |
Correct |
176 ms |
123840 KB |
Output is correct |
14 |
Correct |
174 ms |
126516 KB |
Output is correct |
15 |
Correct |
150 ms |
101976 KB |
Output is correct |
16 |
Correct |
401 ms |
118112 KB |
Output is correct |
17 |
Correct |
390 ms |
115732 KB |
Output is correct |
18 |
Correct |
432 ms |
128192 KB |
Output is correct |
19 |
Correct |
395 ms |
126128 KB |
Output is correct |
20 |
Correct |
190 ms |
107200 KB |
Output is correct |
21 |
Correct |
196 ms |
113892 KB |
Output is correct |
22 |
Correct |
206 ms |
107700 KB |
Output is correct |
23 |
Correct |
192 ms |
111948 KB |
Output is correct |
24 |
Correct |
193 ms |
107624 KB |
Output is correct |
25 |
Correct |
190 ms |
107388 KB |
Output is correct |
26 |
Correct |
201 ms |
107456 KB |
Output is correct |
27 |
Correct |
188 ms |
107564 KB |
Output is correct |
28 |
Correct |
8 ms |
35664 KB |
Output is correct |
29 |
Correct |
7 ms |
35676 KB |
Output is correct |
30 |
Correct |
7 ms |
35676 KB |
Output is correct |
31 |
Correct |
7 ms |
35420 KB |
Output is correct |
32 |
Correct |
8 ms |
35420 KB |
Output is correct |
33 |
Correct |
7 ms |
35672 KB |
Output is correct |
34 |
Correct |
7 ms |
35676 KB |
Output is correct |
35 |
Correct |
7 ms |
35772 KB |
Output is correct |
36 |
Correct |
7 ms |
35420 KB |
Output is correct |
37 |
Correct |
19 ms |
43864 KB |
Output is correct |
38 |
Correct |
179 ms |
123540 KB |
Output is correct |
39 |
Correct |
164 ms |
119196 KB |
Output is correct |
40 |
Correct |
146 ms |
115908 KB |
Output is correct |
41 |
Correct |
174 ms |
110884 KB |
Output is correct |
42 |
Correct |
134 ms |
110016 KB |
Output is correct |
43 |
Correct |
124 ms |
106568 KB |
Output is correct |
44 |
Correct |
172 ms |
120468 KB |
Output is correct |
45 |
Correct |
185 ms |
122048 KB |
Output is correct |
46 |
Correct |
157 ms |
123772 KB |
Output is correct |
47 |
Correct |
137 ms |
114896 KB |
Output is correct |
48 |
Correct |
28 ms |
43860 KB |
Output is correct |
49 |
Correct |
395 ms |
122296 KB |
Output is correct |
50 |
Correct |
420 ms |
119204 KB |
Output is correct |
51 |
Correct |
363 ms |
117696 KB |
Output is correct |
52 |
Correct |
328 ms |
115344 KB |
Output is correct |
53 |
Correct |
297 ms |
108904 KB |
Output is correct |
54 |
Correct |
237 ms |
85876 KB |
Output is correct |
55 |
Correct |
387 ms |
120376 KB |
Output is correct |
56 |
Correct |
395 ms |
122896 KB |
Output is correct |
57 |
Correct |
422 ms |
126392 KB |
Output is correct |
58 |
Correct |
300 ms |
116008 KB |
Output is correct |
59 |
Correct |
104 ms |
55516 KB |
Output is correct |
60 |
Correct |
392 ms |
117076 KB |
Output is correct |
61 |
Correct |
374 ms |
114908 KB |
Output is correct |
62 |
Correct |
426 ms |
124712 KB |
Output is correct |
63 |
Correct |
389 ms |
125072 KB |
Output is correct |
64 |
Correct |
7 ms |
35592 KB |
Output is correct |
65 |
Correct |
8 ms |
35676 KB |
Output is correct |
66 |
Correct |
7 ms |
35420 KB |
Output is correct |
67 |
Correct |
7 ms |
35672 KB |
Output is correct |
68 |
Correct |
7 ms |
35420 KB |
Output is correct |
69 |
Incorrect |
8 ms |
35676 KB |
Output isn't correct |
70 |
Halted |
0 ms |
0 KB |
- |