2367:Genealogical tree

来源:互联网 发布:明治天皇 知乎 编辑:程序博客网 时间:2024/06/02 10:47
描述
The system of Martians' blood relations is confusing enough. Actually, Martians bud when they want and where they want. They gather together in different groups, so that a Martian can have one parent as well as ten. Nobody will be surprised by a hundred of children. Martians have got used to this and their style of life seems to them natural.
And in the Planetary Council the confusing genealogical system leads to some embarrassment. There meet the worthiest of Martians, and therefore in order to offend nobody in all of the discussions it is used first to give the floor to the old Martians, than to the younger ones and only than to the most young childless assessors. However, the maintenance of this order really is not a trivial task. Not always Martian knows all of his parents (and there's nothing to tell about his grandparents!). But if by a mistake first speak a grandson and only than his young appearing great-grandfather, this is a real scandal. 
Your task is to write a program, which would define once and for all, an order that would guarantee that every member of the Council takes the floor earlier than each of his descendants.
输入
The first line of the standard input contains an only number N, 1 <= N <= 100 — a number of members of the Martian Planetary Council. According to the centuries-old tradition members of the Council are enumerated with the natural numbers from 1 up to N. Further, there are exactly N lines, moreover, the I-th line contains a list of I-th member's children. The list of children is a sequence of serial numbers of children in a arbitrary order separated by spaces. The list of children may be empty. The list (even if it is empty) ends with 0.
输出

The standard output should contain in its only line a sequence of speakers' numbers, separated by spaces. If several sequences satisfy the conditions of the problem, you are to write to the standard output any of them. At least one such sequence always exists.

===================================================================================================

题意:

继承关系排序。辈分越长,继承权越靠前。

输入含义:

5              //5个人
0              //1号没有后代
4 5 1 0     //2号的后代是4,5,1(排名不分先后)
1 0           //3号点后代时1
5 3 0
3 0

==========================================================

基本的拓扑排序

1 建图,边的方向是 辈分高->辈分低

2 输出入度为0的点

3 将该点和从该点发出的边删除

4 回到第2步,直到全部输出 

==========================================================

AC代码

#include <iostream>#include <algorithm>#include <map>#include <vector>using namespace std;int degree[100]={0}; //每个点的入度int p[100]={0}; //每个点是否被打印输出过int main(){    map< int,vector<int> > graph;    int num;    cin>>num;    for(int i=1;i<num+1;i++){             //建图        int node;        cin>>node;        while(node!=0){            graph[node].push_back(i);            degree[node]++;            cin>>node;        }    }        for(int j=0;j<num;j++){        for(int i=1;i<num+1;i++){            if(degree[i]==0 && p[i]==0){   //度数为0且没被输出过                cout<<i<<" ";                p[i]=1;                map< int,vector<int> >::iterator it;                for(it=graph.begin();it!=graph.end();it++){      //删边                    if(find(it->second.begin(),it->second.end(),i)!=it->second.end())                        degree[it->first]--;                }            }        }    }    return 0;}


0 0
原创粉丝点击