#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],lis;
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;
}
bool ck(ll k){
ll ti=0;
REP(i,SZ(lis)-1){
ll tmp=0;
for(auto j:v[lis[i]]){
if(j==lis[i+1]||(i&&j==lis[i-1]))continue;
if(dg[i]+dp[j]+1+(i==0?0:-1)>k)++tmp;
}
ti+=tmp;k-=tmp;
if(k<0||ti>i+1)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;
x=s;
while(x!=t)lis.pb(x),x=fa[x];
lis.pb(t);
for(int i=SZ(lis)-2;i>=0;i--)dg[i]=dg[i+1]+SZ(v[lis[i]])-1-(lis[i]==t?0:1);
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 |
17 ms |
23808 KB |
Output is correct |
3 |
Incorrect |
16 ms |
23788 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
338 ms |
79724 KB |
Output is correct |
2 |
Correct |
299 ms |
74220 KB |
Output is correct |
3 |
Correct |
803 ms |
78060 KB |
Output is correct |
4 |
Correct |
382 ms |
51564 KB |
Output is correct |
5 |
Correct |
806 ms |
78072 KB |
Output is correct |
6 |
Correct |
813 ms |
78108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23788 KB |
Output is correct |
2 |
Correct |
17 ms |
23808 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 |
17 ms |
23808 KB |
Output is correct |
3 |
Incorrect |
16 ms |
23788 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |