fork download
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. #define all(s) s.begin(), s.end()
  5. #define cin(vec) \
  6.   for (auto &i : vec) \
  7.   cin >> i
  8. #define int long long int
  9. #define ll long long
  10. #define ld long double
  11. #define PI acos(-1)
  12. #define no cout << "No\n";
  13. #define yes cout << "Yes\n";
  14. #define ordered_set tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update>
  15. using namespace std;
  16. using namespace __gnu_pbds;
  17. // #define ordered_set tree<int, null_type,less<>, rb_tree_tag,tree_order_statistics_node_update>// set
  18. typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset;
  19. const int N = 100+5, mod = 1000000007;
  20. const long long OO = 1e18;
  21.  
  22. void mousad()
  23. {
  24. ios_base::sync_with_stdio(0);
  25. cin.tie(0);
  26. cout.tie(0);
  27. // put input file if needed as input in problem
  28. // freopen("input.txt", "r", stdin);
  29. // freopen("output.txt", "w", stdout);
  30. #ifndef ONLINE_JUDGE
  31. #define cout cerr
  32. cerr.tie(0);
  33. freopen("input.txt", "r", stdin);
  34. freopen("output.txt", "w", stderr);
  35. // freopen("output.txt", "w", stdout);
  36. #endif
  37. }
  38. // Variadic print function for debugging
  39. template <typename... Args>
  40. void print(Args... args)
  41. {
  42. ((cout << args << " "), ...);
  43. cout << endl;
  44. }
  45. template <typename T = int>
  46. istream &operator>>(istream &in, vector<T> &v)
  47. {
  48. for (auto &x : v)
  49. in >> x;
  50. return in;
  51. }
  52. // Overload for vector
  53. template <typename T = int>
  54. ostream &operator<<(ostream &out, const vector<T> &v)
  55. {
  56. for (const T &x : v)
  57. out << x << ' ';
  58. return out;
  59. }
  60. //______________________________________________________________________________________//
  61. int n , turn = 0;
  62. pair<int,int> mx = make_pair(-1,-1);
  63. int vis[N];
  64. vector<int> adj[N];
  65. void dfs (int node = 1 ,int depth = 0) {
  66. vis[node]++;
  67. if (depth > mx.first)
  68. mx.first = depth , mx.second = node;
  69. for (auto &neighbour : adj[node]) {
  70. if (vis[neighbour] == turn)
  71. dfs(neighbour, depth + 1);
  72. }
  73. }
  74. void solve() {
  75. cin >> n ;
  76. for (int i = 0; i < n-1; i++) {
  77. int u , v; cin >> u >> v ;
  78. adj[u].push_back(v);
  79. adj[v].push_back(u);
  80. }
  81. dfs();
  82. turn++;
  83. dfs(mx.second);
  84. cout << mx.first << endl;
  85. }
  86. signed main() {
  87. mousad();
  88. int t = 1;
  89. //cin >> t;
  90. for (int i = 1; i <= t; i++) {
  91. //cerr << "Scenario #" << i << ":"<< endl;
  92. solve();
  93. }
  94. }
  95.  
Success #stdin #stdout 0.01s 5300KB
stdin
Standard input is empty
stdout
0