Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<queue>
  4. #define maxn 5005
  5. using namespace std;
  6. int nod[maxn], tata[maxn],x,y,n,alb[maxn],fact[100],l;
  7. vector <int> v[maxn];
  8. queue <int> q;
  9.  
  10. int dfs(int nod){
  11.     int nr=0;
  12.     for(int i=0; i<v[nod].size(); i++)
  13.       if(!alb[v[nod][i]])
  14.         nr+=dfs(v[nod][i]);
  15.       else nr+=1;
  16.     return nr;
  17. }
  18.  
  19. int main(){
  20.   cin>>n;
  21.   for(int i=1; i<n; i++){
  22.   cin>>x>>y;
  23.   tata[y]=x;
  24.   v[x].push_back(y);
  25.   }
  26.   for(int i=1; i<=n; i++)
  27.     if(v[i].size()==0)
  28.       q.push(i);
  29.   while(!q.empty()){
  30.     x=q.front();
  31.     q.pop();
  32.     l=v[x].size();
  33.     cout<<x<<' ';
  34.     if(nod[x]==0){
  35.     for(int i=0; i<l; i++)
  36.       nod[x]+=nod[v[x][i]]+1;
  37.     if(l>1){
  38.       alb[x]=1;
  39.       nod[x]+=(l*l-1)/2;
  40.     }else
  41.       nod[x]+=dfs(x);
  42.     q.push(tata[x]);
  43.   }
  44.   }
  45.   cout<<nod[1];
  46.   return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement