Advertisement
achulkov2

Untitled

Apr 15th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5. vector<vector<int>> g;
  6. vector<bool> used;
  7.  
  8. pair<int, int> dfs(int v) {
  9. used[v] = true;
  10. pair<int, int> best = {v, 0};
  11. for (const auto& u : g[v]) {
  12. if (!used[u]) {
  13. auto res = dfs(u);
  14. if (res.second > best.second) best = res;
  15. }
  16. }
  17. ++best.second;
  18. return best;
  19. }
  20.  
  21. int main() {
  22. int n;
  23. cin >> n;
  24. g.assign(n, vector<int>());
  25. used.assign(n, false);
  26. for (int i = 0; i < n - 1; ++i) {
  27. int a, b;
  28. cin >> a >> b;
  29. --a, --b;
  30. g[a].push_back(b);
  31. g[b].push_back(a);
  32. }
  33. auto res1 = dfs(0);
  34. used.assign(n, false);
  35. auto res2 = dfs(res1.first);
  36. cout << res2.second - 1<< "\n";
  37. return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement