Tranvick

Untitled

Jun 7th, 2012
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.43 KB | None | 0 0
  1. #include <iostream>
  2. #define N 111111
  3.  
  4. using namespace std;
  5.  
  6. int es[N],next[N],c,n,first[N],f[N];
  7.  
  8. void add(int x,int y){
  9.     next[++c]=first[x];first[x]=c;
  10.     es[c]=y;
  11. }
  12.  
  13. void dfs(int v,int p){
  14.     for (int h=first[v];h;h=next[h]) if (es[h]!=p){
  15.         dfs(es[h],v);
  16.         f[v]=max(f[v],f[es[h]]);
  17.     }
  18.     ++f[v];
  19. }
  20.  
  21. int main(){
  22.     cin>>n;
  23.     for (int i=0;i<n-1;++i){
  24.         int x,y;
  25.         cin>>x>>y; 
  26.         add(x,y);
  27.         add(y,x);
  28.     }
  29.     dfs(1,-1);
  30. }
Advertisement
Add Comment
Please, Sign In to add comment