Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. const int N = int(1e4) + 9;
  6. vector<int> adj[N];
  7. int n, d;
  8.  
  9. int dfs(int u = 1, int p = -1) {
  10.  
  11. int d1 = 0, d2 = 0;
  12.  
  13. for (auto v: adj[u]) {
  14. if (v == p) continue;
  15. int d = dfs(v, u) + 1;
  16. if (d > d1) d2 = d1, d1 = d;
  17. else if (d > d2) d2 = d;
  18. }
  19. d = max(d, d1 + d2);
  20. return d1;
  21. }
  22.  
  23. int main() {
  24. cin >> n;
  25. for (int i=0;i<n-1;++i) {
  26. int a, b; cin >> a >> b;
  27. adj[a].push_back(b);
  28. adj[b].push_back(a);
  29. }
  30.  
  31. dfs();
  32. cout << d << endl;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement