Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int INF=1e9;
- using pii=pair<int,int>;
- int n;
- vector <int> g[300001];
- int main(){
- scanf("%d",&n);
- vector <int> dis(n+1,-INF);
- for(int i=1;i<n;i++){
- int u,v;
- scanf("%d %d",&u,&v);
- g[u].push_back(v);
- g[v].push_back(u);
- sort(g[u].begin(),g[u].end(),greater<int>());
- sort(g[v].begin(),g[v].end(),greater<int>());
- }
- int max_node=0;
- for(int i=1;i<=n;i++){
- if(dis[i]!=-INF)
- continue;
- queue <pii> q;
- q.push({1,i});
- dis[i]=1;
- while(q.size()>0){
- int u,d;
- d=q.front().first;
- u=q.front().second;
- q.pop();
- max_node = max(max_node,d);
- for(auto v:g[u]){
- if(v>u && dis[u]+1>dis[v]) {
- dis[v]=dis[u]+1;
- q.push({d+1,v});
- }
- else
- break;
- }
- }
- }
- printf("%d",max_node);
- return 0;
- }
Add Comment
Please, Sign In to add comment