#include <bits/stdc++.h>
using namespace std;
const int N = 10010;
int n,root;
vector<int> g[N];
int dist[N];
void dfs(int u,int fa)
{
for(int i = 0 ; i < g[u].size() ; i++)
{
int j = g[u][i];
if(j == fa) continue;
dist[j] = dist[u] + 1;
dfs(j,u);
}
}
signed main()
{
cin>>n>>root;
for(int i = 0 ; i < n - 1 ; i ++)
{
int a,b;
cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(root,-1);
int res = 0;
for(int i = 1 ; i <= n ; i++)
{
res = max(res , dist[i]);
}
cout<<res<<endl;
return 0;
}
3699. 树的高度 dfs vector存邻接表
最新推荐文章于 2024-08-29 16:37:54 发布