Advertisement
YEZAELP

TUMSO-17: Network

Dec 7th, 2021
902
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. const int N = 1e5;
  5. vector <int> g[N + 10];
  6. bool vs[N + 10];
  7. int ans;
  8.  
  9. int dfs(int u, int p){
  10.     vs[u] = true;
  11.     vector <int> nxt;
  12.     for(auto v: g[u]){
  13.         if(v != p) nxt.push_back(dfs(v, u));
  14.     }
  15.     sort(nxt.begin(), nxt.end(), greater <int> ());
  16.     int mx = 0, sz = nxt.size();
  17.     for(int i=0;i<sz;i++)
  18.         mx = max(mx, nxt[i] + i + 1);
  19.     return mx;
  20. }
  21.  
  22. int main(){
  23.  
  24.     int n, st;
  25.     scanf("%d %d", &n, &st);
  26.  
  27.     for(int i=1;i<=n-1;i++){
  28.         int u, v;
  29.         scanf("%d %d", &u, &v);
  30.         g[u].push_back(v);
  31.         g[v].push_back(u);
  32.     }
  33.  
  34.     cout << dfs(st, -1);
  35.  
  36.     return 0;
  37. }
Advertisement
RAW Paste Data Copied
Advertisement