Advertisement
mohammedehab2002

Untitled

Apr 3rd, 2017
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. using namespace std;
  5. vector<int> v[100005];
  6. int dp[100005],ans=0;
  7. void dfs(int node,int pnode)
  8. {
  9.     vector<int> tmp;
  10.     for (int i=0;i<v[node].size();i++)
  11.     {
  12.         if (v[node][i]!=pnode)
  13.         {
  14.             dfs(v[node][i],node);
  15.             tmp.push_back(dp[v[node][i]]);
  16.         }
  17.     }
  18.     sort(tmp.begin(),tmp.end());
  19.     if (tmp.size()==0)
  20.     return;
  21.     dp[node]=tmp[tmp.size()-1]+1;
  22.     if (tmp.size()==1)
  23.     ans=max(ans,dp[node]);
  24.     else
  25.     ans=max(ans,tmp[tmp.size()-1]+tmp[tmp.size()-2]+2);
  26. }
  27. int main()
  28. {
  29.     int n;
  30.     cin >> n;
  31.     for (int i=1;i<n;i++)
  32.     {
  33.         int a,b;
  34.         cin >> a >> b;
  35.         v[a].push_back(b);
  36.         v[b].push_back(a);
  37.     }
  38.     dfs(1,0);
  39.     cout << ans;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement