Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int vertex;
  4. list<int > v[10002];
  5. int vis[10002];
  6. int len[10003];
  7. void read()
  8. {
  9. cin>>vertex;
  10. for(int i=1;i<vertex;i++)
  11. {
  12. int x,y;
  13. cin>>x>>y;
  14. v[x].push_back(y);
  15. v[y].push_back(x);
  16. }
  17. }
  18.  
  19. int dfs(int x,int val=0)
  20. {
  21. vis[x]=true;
  22. int ans=0,ans1=0;
  23. for(auto it=v[x].begin();it!=v[x].end();it++)
  24. {
  25. if(!vis[*it])
  26. {
  27. int x=dfs(*it,val+1);
  28. if(x>ans)
  29. {
  30. ans1=ans;
  31. ans=x;
  32. }
  33. else if(x>ans1)
  34. ans1=x;
  35. }
  36. }
  37. len[x]=ans+ans1;
  38. return ans+1;
  39. }
  40. int main()
  41. {
  42. ios_base::sync_with_stdio(0);
  43. cin.tie(0);cout.tie(0);
  44. //freopen("input.txt","r",stdin);
  45. read();
  46. dfs(1);
  47. cout<<*max_element(len,len+vertex+1)<<endl;
  48. return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement