Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include<queue>
- #define maxn 5005
- using namespace std;
- int nod[maxn], tata[maxn],x,y,n,alb[maxn],fact[100],l;
- vector <int> v[maxn];
- queue <int> q;
- int dfs(int nod){
- int nr=0;
- for(int i=0; i<v[nod].size(); i++)
- if(!alb[v[nod][i]])
- nr+=dfs(v[nod][i]);
- else nr+=1;
- return nr;
- }
- int main(){
- cin>>n;
- for(int i=1; i<n; i++){
- cin>>x>>y;
- tata[y]=x;
- v[x].push_back(y);
- }
- for(int i=1; i<=n; i++)
- if(v[i].size()==0)
- q.push(i);
- while(!q.empty()){
- x=q.front();
- q.pop();
- l=v[x].size();
- cout<<x<<' ';
- if(nod[x]==0){
- for(int i=0; i<l; i++)
- nod[x]+=nod[v[x][i]]+1;
- if(l>1){
- alb[x]=1;
- nod[x]+=(l*l-1)/2;
- }else
- nod[x]+=dfs(x);
- q.push(tata[x]);
- }
- }
- cout<<nod[1];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement