fork download
  1. #include <bits/stdc++.h>
  2. #define fi first
  3. #define se second
  4. #define ll long long
  5. #define el <<"\n"
  6. #define pll pair< ll, ll >
  7. #define pb push_back
  8. using namespace std;
  9. long long n, d[100009];
  10. vector< ll > a[100009];
  11. void dfs(ll k, ll cha){
  12. bool kt = 0;
  13. ll res = -2, sl;
  14. for(ll i : a[k])
  15. if(i != cha){
  16. kt = 1;
  17. dfs(i, k);
  18. if(res == d[i])
  19. sl++;
  20. if(res < d[i]){
  21. res = d[i];
  22. sl = 1;
  23.  
  24. }
  25. }
  26. if(!kt){
  27. d[k] = 0;
  28. return;
  29. }
  30. if(sl > 1)
  31. d[k] = res + 1;
  32. else
  33. d[k] = res;
  34.  
  35. }
  36. void lm(){
  37. ll i;
  38. dfs(1, 0);
  39. cout << d[1] el;
  40. }
  41. int main()
  42. {
  43. ios_base::sync_with_stdio(false);
  44. cin.tie(NULL);cout.tie(NULL);
  45.  
  46. freopen("PINETREE.inp","r",stdin);
  47. freopen("PINETREE.out","w",stdout);
  48. ///------- MINH KHANG ----------///
  49. ll i;
  50. cin >> n;
  51. for(i = 1; i < n ;++i){
  52. ll x, y;
  53. cin >> x >> y;
  54. a[x].pb(y);
  55. a[y].pb(x);
  56. }
  57. lm();
  58. return 0;
  59. }
  60.  
Success #stdin #stdout 0.01s 6760KB
stdin
Standard input is empty
stdout
Standard output is empty