Genealogical tree

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 2920 Accepted: 1962 Special Judge

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

Source



Ural State University Internal Contest October'2000 Junior Session

 
/**********************************************

      author   :    Grant Yuan
time : 2014.7.29
algorithm: topological_sort
source : POJ 2367 **********************************************/ #include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define MAX 101
using namespace std; int mat[MAX][MAX],num[MAX];
int n,m,ans,sum;
int lu[MAX]; int main()
{ int a,b;
while(~scanf("%d",&n)){
memset(mat,0,sizeof(mat));
memset(num,0,sizeof(num));
memset(lu,0,sizeof(lu));
for(int i=1;i<=n;i++)
{ while(1){
scanf("%d",&a);
if(a==0) break;
if(mat[i][a]==0)
{
mat[i][a]=1;
num[i]++;
}
}}
int i,p=n;
while(1){
for(i=n;i>0;i--)
{
if(num[i]==0)
break;
}
if(i==0)
break;
num[i]=-1; lu[p--]=i;
for(int j=1;j<=n;j++)
{
if(mat[j][i])
num[j]--;
}
} for(int j=1;j<=n;j++)
if(j<n)printf("%d ",lu[j]);
else printf("%d",lu[j]);
printf("\n");
}
return 0;
}

POJ 2367 topological_sort的更多相关文章

  1. POJ 2367 (裸拓扑排序)

    http://poj.org/problem?id=2367 题意:给你n个数,从第一个数到第n个数,每一行的数字代表排在这个行数的后面的数字,直到0. 这是一个特别裸的拓扑排序的一个题目,拓扑排序我 ...

  2. Poj(2367),拓扑排序

    题目链接:http://poj.org/problem?id=2367 题意: 知道一个数n, 然后n行,编号1到n, 每行输入几个数,该行的编号排在这几个数前面,输出一种符合要求的编号名次排序. 拓 ...

  3. poj 2367 Genealogical tree

    题目连接 http://poj.org/problem?id=2367 Genealogical tree Description The system of Martians' blood rela ...

  4. 图论之拓扑排序 poj 2367 Genealogical tree

    题目链接 http://poj.org/problem?id=2367 题意就是给定一系列关系,按这些关系拓扑排序. #include<cstdio> #include<cstrin ...

  5. 拓扑排序 POJ 2367

    今天网易的笔试,妹的,算法题没能A掉,虽然按照思路写了出来,但是尼玛好歹给个测试用例的格式呀,吐槽一下网易的笔试出的太烂了. 就一道算法题,比较石子重量,个人以为解法应该是拓扑排序. 就去POJ找了道 ...

  6. poj 2367 Genealogical tree (拓扑排序)

    火星人的血缘关系很奇怪,一个人可以有很多父亲,当然一个人也可以有很多孩子.有些时候分不清辈分会产生一些尴尬.所以写个程序来让n个人排序,长辈排在晚辈前面. 输入:N 代表n个人 1~n 接下来n行 第 ...

  7. poj 2367 Genealogical tree【拓扑排序输出可行解】

    Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3674   Accepted: 2445 ...

  8. POJ 2367 Genealogical tree 拓扑排序入门题

    Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8003   Accepted: 5184 ...

  9. POJ 2367:Genealogical tree(拓扑排序模板)

    Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7285   Accepted: 4704 ...

随机推荐

  1. Minix3信号处理分析

    进程的信号处理的相关结构 PM中存放着所有进程的进程描述符,在一个进程描述符中,有一个指针,指向一个sigaction结构二维数组中的一项,表示这个进程所有信号的操作.一个sigaction结构包含信 ...

  2. CSS系列------选择器和选择器的优先级

    1.1.基本选择器 通配符选择器(*)      通配符选择器的使用方法如下 *{margin:0px; padding:0px;} //*代表所有的 ID选择器(#) ID选择器的使用方式如下: * ...

  3. JavaScript系列----事件机制

    1.事件流 1.1.标准事件流 所谓的标准事件流指的的:EMCAScript标准规定事件流包含三个阶段,分别为事件捕获阶段,处于目标阶段,事件冒泡阶段. 下面是一段html代码,根据代码来说明标准事件 ...

  4. QT中槽的使用

    一.建立槽和按钮之间的连接 connect(信号发送者,发送的信号,信号接收者,信号接收者的槽函数) 1.例子 connect(ui->pushButton,SIGNAL(clicked(boo ...

  5. Kotlin编码----var和val的区别

    var是一个可变变量,这是一个可以通过重新分配来更改为另一个值的变量.这种声明变量的方式和Java中常规的变量的声明方式一样. val是一个只读变量,这种声明变量的方式相当于java中的final变量 ...

  6. spring配置文件一般结构

         xml schema:schema在文档根节点当中通过xmlns对文档当中的命名空间进行申明,第三行代码定义了默认命名空间用于spring bean的定义.xsi命名空间用于为每个文档中指定 ...

  7. asp.net web api 版本控制

    版本控制   版本控制的方法有很多,这里提供一种将Odata与普通web api版本控制机制统一的方法,但也可以单独控制,整合控制与单独控制主要的不同是:整合控制通过VersionController ...

  8. Request 和 Response 区别

    Request 和 Response 对象起到了服务器与客户机之间的信息传递作用.Request 对象用于接收客户端浏览器提交的数据,而 Response 对象的功能则是将服务器端的数据发送到客户端浏 ...

  9. C#中四步轻松使用log4net记录本地日志

    在这里,记录我在项目中使用log4net记录本地日志的步骤.在不会之前感觉很难,很神秘,一旦会了之后其实没那么难.其实所有的事情都是一样的,下面我就分享一下我使用log4Net的经验. 第一步:首先从 ...

  10. mysql步骤详解

    一.配置MySQL数据库 1.解压绿色版mysql,并改名为mysql5.7,如下图 对比一下下图5.6以前的版本,少data目录(存放数据)和my-default.ini文件(配置信息) 二.安装服 ...