This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define ppb pop_back
#define umax(x,y) x=max(x,y)
#define umin(x,y) x=min(x,y)
#define ll long long
#define ii pair<int,int>
#define iii pair<ii,int>
#define iiii pair<ii,ii>
#define sz(x) ((int) x.size())
#define orta ((bas+son)>>1)
#define all(x) x.begin(),x.end()
#define dbgs(x) cerr<<(#x)<<" --> "<<(x)<<" "
#define dbg(x) cerr<<(#x)<<" --> "<<(x)<<endl;getchar()
#define pw(x) (1<<(x))
#define inf 60000000000000000
#define MOD 1000000009
#define N 300005
#define MAX 5032117
#define LOG 30
#define KOK 333
using namespace std;
int tut,n,a,b,x,y;
vector<int> tr,alle,v[N];
int dfs(int node,int ata,bool state) {
int mx=-1,cnt=0;
for(int to:v[node]) {
if(to==ata || (!state && to==alle[tut+1]) || (state && to==alle[tut])) continue ;
int res=dfs(to,node,state);
if(res>mx) {
mx=res;
cnt=1;
}
else if(res==mx) cnt++;
}
if(mx==-1) mx=cnt=0;
return mx+cnt;
}
int solve(int upto) {
tut=upto;
return max(dfs(a,0,0),dfs(b,0,1));
}
void process() {
for(int i:tr) alle.pb(i);
}
void dfs1(int node,int ata) {
tr.pb(node);
if(node==b) {
process();
return ;
}
for(int i:v[node]) {
if(i==ata) continue ;
dfs1(i,node);
}
tr.ppb();
}
int main() {
//freopen("input.txt","r",stdin);
scanf("%d %d %d",&n,&a,&b);
for(int i=1;i<n;i++) {
scanf("%d %d",&x,&y);
v[x].pb(y);
v[y].pb(x);
}
dfs1(a,0);
/*int bas=0,son=sz(alle)-2;
while(bas+1<=son) {
int res1=solve(orta);
int res2=solve(orta+1);
if(res1<res2) son=orta;
else bas=orta+1;
}
printf("%d",min(min(solve(0),solve(sz(alle)-2)),solve(orta)));*/
int ans=101010101;
for(int i=0;i<sz(alle)-2;i++) umin(ans,solve(i));
printf("%d",ans);
}
Compilation message (stderr)
torrent.cpp: In function 'int main()':
torrent.cpp:97:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d",&n,&a,&b);
~~~~~^~~~~~~~~~~~~~~~~~~~~
torrent.cpp:101:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&x,&y);
~~~~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |