Submission #374994

#TimeUsernameProblemLanguageResultExecution timeMemory
374994YJUMousetrap (CEOI17_mousetrap)C++14
25 / 100
813 ms78572 KiB
#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; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...