AcWing 3699. 树的高度
原题链接
简单
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
const int N = 1e6+10;
int t,n,m,k,l,r,op,x,y,u,v;
int f[N];
unordered_map<int,vector<int>>mp;
bool vis[N];
bool flag;
struct nd{
int v;
int l;
};
queue<nd> q;
void solve(){
cin>>n>>m;
q.push({m,0});
vis[m] = true;
for(int i = 1;i<=n-1;i++){
cin>>u>>v;
mp[u].push_back(v);
mp[v].push_back(u);
}
while(!q.empty()){
auto [t,l] = q.front();
q.pop();
// cout<<t<<" "<<l<<"\n";
for(int num:mp[t]){
if(vis[num])continue;
q.push({num,l+1});
vis[num] = true;
}
if(q.empty()){
cout<<l<<"\n";
return ;
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
solve();
return 0;
}