Genealogical tree(拓扑结构+邻接表+优先队列)

Genealogical tree

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 5   Accepted Submission(s) : 3
Special Judge
Problem Description
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.
 

 

Input
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.
 

 

Output
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.
 

 

Sample Input
5 0 4 5 1 0 1 0 5 3 0 3 0
 

 

Sample Output
2 4 5 3 1
 题解:虽然没太懂题意,但是画了个图发现是标准的拓扑结构;
就是好像是外星人生孩子吧;总共N个人;第i行的数据是第i个人的孩子;每组数据0结束;
代码:
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<queue>
 4 using namespace std;
 5 const int MAXN=110;
 6 struct Node{
 7     int to,next;
 8 };
 9 Node edg[MAXN*MAXN];
10 int head[MAXN],que[MAXN],ans[MAXN],top,N;
11 priority_queue<int,vector<int>,greater<int> >dl;
12 void topu(){
13     for(int i=1;i<=N;i++){
14         if(!que[i])dl.push(i);
15     }
16     while(!dl.empty()){
17         ans[top++]=dl.top();
18         int k=dl.top();
19         dl.pop();
20         for(int j=head[k];j!=-1;j=edg[j].next){
21             que[edg[j].to]--;
22             if(!que[edg[j].to])dl.push(edg[j].to);
23         }
24     }
25     for(int i=0;i<top;i++){
26         if(i)printf(" ");
27         printf("%d",ans[i]);
28     }
29     puts("");
30 }
31 void initial(){
32     memset(head,-1,sizeof(head));
33     memset(que,0,sizeof(que));
34     top=0;
35     while(!dl.empty())dl.pop();
36 }
37 int main(){
38     int a;
39     while(~scanf("%d",&N)){
40             initial();
41             int k=0;
42         for(int i=1;i<=N;i++){
43             while(scanf("%d",&a),a){
44                 edg[k].to=a;
45         edg[k].next=head[i];
46         head[i]=k;
47         k++;
48         que[a]++;
49             }
50         }
51         topu();
52     }
53     return 0;
54 }

 

posted @ 2015-08-14 16:37  handsomecui  阅读(428)  评论(0编辑  收藏  举报