#include<bits/stdc++.h>
//#include<graphics.h>
using namespace std;
/*
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<='0'||ch>'9'){
if(ch=='-') w=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
*/
struct node{
int fa,dep;
}tree[105];
int cnt[105];
int lca(int x,int y){
while(x!=y){
if(tree[x].dep<tree[y].dep){
y=tree[y].fa;
}else if(tree[x].dep>tree[y].dep){
x=tree[x].fa;
}else{
x=tree[x].fa;
y=tree[y].fa;
}
}
return x;
}
int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
//std::ios::sync_with_stdio(false);
int n;
cin>>n;
tree[1].dep=1;
cnt[1]++;
tree[1].fa=1;
int d=0,b=0;
for(int i=1;i<n;i++){
int u,v;
cin>>u>>v;
tree[v].fa=u;
tree[v].dep=tree[u].dep+1;
cnt[tree[v].dep]++;
d=max(d,tree[v].dep);
}
int u,v;
cin>>u>>v;
for(int i=1;i<=102;i++) b=max(b,cnt[i]);
cout<<d<<endl<<b<<endl;
//cout<<lca(u,v)<<endl;
cout<<(tree[u].dep-tree[lca(u,v)].dep)*2+tree[v].dep-tree[lca(u,v)].dep;
return 0;
}
在46行cin>>u>>v后面加上了if(u>v)swap(u,v);后这份代码会WA的一个点离奇TLE,这是为什么?
原来的代码(不加swap)会WA第一个点,为什么?
谢谢dalao