fork(3) download
  1. /*Author: Rishul Aggarwal*/
  2.  
  3. #include<bits/stdc++.h>
  4.  
  5. #define mod 1000000007
  6. #define ll long long
  7. #define rep(i,a,b) for(int i=a;i<=b;i++)
  8. #define in(type,x) scanf("%" #type,&x)
  9. #define debug(args...) {dbg,args; cerr<<endl;}
  10. #define test int t;\
  11. in(d,t);\
  12. while(t--)
  13.  
  14. using namespace std;
  15.  
  16. struct debugger
  17. {template<typename T> debugger& operator,(const T& v)
  18. {cerr<< v <<" ";
  19. return *this;
  20. }
  21. }dbg;
  22.  
  23. ll gcd(ll a,ll b) {if(b==0) return a; return gcd(b,a%b);}
  24.  
  25. ll power(ll b,ll exp,ll m) {ll ans=1; b%=m; while(exp){if(exp&1) ans=(ans*b)%m; exp>>=1; b=(b*b)%m; } return ans; }
  26.  
  27. vector<vector<int> >g;
  28. int n,v[1000002],d[1000002];
  29.  
  30. int bfs(int start)
  31. {
  32. v[start]=1;
  33. d[start]=1;
  34. queue<int>q;
  35. q.push(start);
  36. while((!q.empty()))
  37. {
  38. int next=q.front();
  39. q.pop();
  40. for(int i=0;i<g[next].size();i++)
  41. {if(v[g[next][i]]==0)
  42. {
  43. v[g[next][i]]=1;
  44. d[g[next][i]]=d[next]+1;
  45. q.push(g[next][i]);
  46. }
  47. }
  48. }
  49. int ans=0;
  50. rep(i,1,n)
  51. cout<<d[i]<<" ";
  52. cout<<endl;
  53. rep(i,1,n)
  54. ans=max(ans,d[i]);
  55. return ans;
  56. }
  57.  
  58. int main()
  59. {
  60. in(d,n);
  61. g.resize(n+2);
  62. rep(i,1,n-1)
  63. {
  64. int x,y;
  65. in(d,x),in(d,y);
  66. g[x].push_back(y),g[y].push_back(x);
  67. }
  68. cout<<bfs(1);
  69.  
  70. return 0;
  71. }
  72.  
  73.  
  74.  
Success #stdin #stdout 0s 11296KB
stdin
Standard input is empty
stdout
0