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. 10.javaweb核心标签库详解

    一.JSTL简介及在项目中安装配置 1,  简介 使用JSTL标签的目的就是不希望jsp中出现java逻辑代码 分类 2,  JSTL的安装配置 首先将jar包中的各个标签库配置文件拷贝到项目WEB- ...

  2. "use strict"详解

    参考:http://www.ruanyifeng.com/blog/2013/01/javascript_strict_mode.html 目的: 消除JavaScript语法的一些不合理.不严谨之处 ...

  3. 解决mssql localdb 中文乱码问题

    使用以下查询语句即可. alter database "E:\.Net Core\Database\hm.mdf" set single_user with rollback im ...

  4. 【python】内置函数总结(一)

    1.判断真假的函数:bool()2.Python中所谓的迭代协议就是next方法的对象会前进到下一个结果,在一系列结果的末尾会引发StopIteration异常.在Python中,任何类型的对象都被认 ...

  5. wealoha thrift-client-pool 总结

    DefaultEvictionPolicy类是EvictionPolicy接口的实现主要描述,pool中那些idel对象会被Evict,回收.关键代码如下: public boolean evict( ...

  6. Javaweb学习(一):tomcat服务器配置与启动

    目前所使用的集成开发环境为myeclipse10.7,tomcat版本为apache-tomcat-7.0.82,部分编写地方可能有所不同,但是工具不是最主要的,重要的是掌握的知识. tomcat在m ...

  7. HTTP协议类

    本文从以下几方面介绍HTTP协议 HTTP协议的主要特点 HTTP报文的组成部分 HTTP方法 HTTP状态码 POST和GET的区别 什么是持久连接 什么是管线化 主要特点: http协议的特点: ...

  8. 巧学DBhelper

    这几天在教我很重要的人学习,她属于那种超级小白,很超级的那种. 教她的过程中 发现有的知识点 不管这么教都不会.DBhelper就是不知道怎么记. 当时我就想到 杰哥(程杰)的出的大话系列,和他写书的 ...

  9. Vue.js简单的应用

    1:一个简单实现 下面代码部分: <body> <div id="myDiv1"> {{userName}} </div> </body& ...

  10. TFboy养成记 CNN

    1/先解释下CNN的过程: 首先对一张图片进行卷积,可以有多个卷积核,卷积过后,对每一卷积核对应一个chanel,也就是一张新的图片,图片尺寸可能会变小也可能会不变,然后对这个chanel进行一些po ...