fork(1) download
  1. #include <bits/stdc++.h>
  2. #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
  3. #define ll long long int
  4. #define ld long double
  5. using namespace std;
  6. const int N = 1e6 + 5;
  7. const int MOD = 1e9 + 7;
  8. vector<int> graph[N];
  9. bool vis[N];
  10. int mx = 0, node;
  11.  
  12. void dfs(int source, int far){
  13. vis[source] = 1;
  14. if(far > mx){
  15. mx = far;
  16. node = source;
  17. }
  18. for(auto k : graph[source])
  19. if(!vis[k])
  20. dfs(k, far + 1);
  21. }
  22.  
  23. int main(){
  24. fast;
  25. int n;
  26. cin >> n;
  27. for(int i = 0; i < n - 1; ++i){
  28. int u, v;
  29. cin >> u >> v;
  30. graph[u].push_back(v);
  31. graph[v].push_back(u);
  32. }
  33. dfs(1, 0);
  34. memset(vis, 0, sizeof(vis));
  35. dfs(node, 0);
  36. cout << 3 * mx << '\n';
  37. return 0;
  38. }
Runtime error #stdin #stdout 0.01s 26784KB
stdin
Standard input is empty
stdout
Standard output is empty