[ABC148F] Playing Tag on Tree
题目传送门
思路
考虑记录所有点到
献上
code
#include<bits/stdc++.h>
using namespace std;
int n,u,v,x,y,v2[114514],v1[114514],ma;
vector<int>w[100010];
void dfs(int xx,int yy,int z){
if(z) v1[xx] = yy;
else v2[xx] = yy;
for(int i = 0;i < w[xx].size();i++){
if(z && !v1[w[xx][i]] && w[xx][i] != v) dfs(w[xx][i],yy + 1,z);
else if(!z && !v2[w[xx][i]] && w[xx][i] != u) dfs(w[xx][i],yy + 1,z);
}
}
int main(){
scanf("%d%d%d",&n,&u,&v);
for(int i = 1;i < n;i++) scanf("%d%d",&x,&y),w[x].push_back(y),w[y].push_back(x);
dfs(v,0,1); dfs(u,0,0);
for(int i = 1;i <= n;i++) if(v1[i] > v2[i]) ma = max(ma,v1[i]);
printf("%d",ma - 1);
return 0;
}