#include<bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector,Ofast")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ll N=1e6+5;
const ld pi=acos(-1);
const ll INF=(1LL<<30);
#define SQ(i) ((i)*(i))
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(_a) (ll)_a.size()
ll n,t,s,dp[N],x,y,fa[N],dg[N];
vector<ll> v[N];
void DFS(ll nd,ll pa){
fa[nd]=pa;
ll ma=0,mb=0;
for(auto i:v[nd]){
if(i==pa)continue;
DFS(i,nd);
if(dp[i]>=ma)mb=ma,ma=dp[i];
else if(dp[i]>=mb)mb=dp[i];
}
dp[nd]=mb+SZ(v[nd])-1;
}
ll lis[N],now;
bool ck(ll k){
ll ti=0;
for(int i=1;i<now;i++){
ll tmp=0;
for(auto j:v[lis[i]]){
if(j==lis[i+1]||j==lis[i-1])continue;
if(dg[i]+dp[j]+1-(i!=1)>k)++tmp;
}
ti+=tmp;k-=tmp;
if(k<0||ti>i)return 0;
}
return 1;
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>t>>s;
REP(i,n-1)cin>>x>>y,v[x].pb(y),v[y].pb(x);
if(s==t){cout<<"0\n";return 0;}
DFS(t,0);
dp[t]=0;
for(int i=s;i;i=fa[i])lis[++now]=i;
for(int i=now-1;i>=0;i--)dg[i]=dg[i+1]+SZ(v[lis[i]])-1-(lis[i]!=t);
ll r=INF,l=0;
while(l<r-1){
ll mid=(r+l)>>1;
(ck(mid)?r=mid:l=mid);
}
cout<<r<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23788 KB |
Output is correct |
2 |
Correct |
16 ms |
23788 KB |
Output is correct |
3 |
Incorrect |
16 ms |
23788 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
327 ms |
78572 KB |
Output is correct |
2 |
Correct |
307 ms |
73324 KB |
Output is correct |
3 |
Correct |
805 ms |
77292 KB |
Output is correct |
4 |
Correct |
403 ms |
50412 KB |
Output is correct |
5 |
Correct |
813 ms |
77084 KB |
Output is correct |
6 |
Correct |
807 ms |
76996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23788 KB |
Output is correct |
2 |
Correct |
16 ms |
23788 KB |
Output is correct |
3 |
Incorrect |
16 ms |
23788 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23788 KB |
Output is correct |
2 |
Correct |
16 ms |
23788 KB |
Output is correct |
3 |
Incorrect |
16 ms |
23788 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |