Genealogical tree
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8003   Accepted: 5184   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

 
code:
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
int mon1[]= {,,,,,,,,,,,,};
int mon2[]= {,,,,,,,,,,,,};
int dir[][]= {{,},{,-},{,},{-,}}; int getval()
{
int ret();
char c;
while((c=getchar())==' '||c=='\n'||c=='\r');
ret=c-'';
while((c=getchar())!=' '&&c!='\n'&&c!='\r')
ret=ret*+c-'';
return ret;
} #define max_v 105
priority_queue<int,vector<int>,greater<int> > q;
int indegree[max_v];
int G[max_v][max_v];
int n,m;
void inittp()
{
for(int i=;i<=n;i++)
if(indegree[i]==)
q.push(i);//入度为0的点放入优先队列,优先队列是为了考虑字典序输出
}
void tpsort()
{
int temp;
int c=;
while(!q.empty())
{
temp=q.top();
q.pop();
if(c!=n)
{
printf("%d ",temp);
c++;
}else
{
printf("%d\n",temp);
}
for(int i=;i<=n;i++)
{
if(G[temp][i])//存在边
{
indegree[i]--;//i点入度--
if(indegree[i]==)//若入度为0则放入队列
q.push(i);
}
}
}
}
int main()
{
int x;
while(~scanf("%d",&n))
{
memset(indegree,,sizeof(indegree));
memset(G,,sizeof(G));
for(int i=;i<=n;i++)
{
while()
{
scanf("%d",&x);
if(x==)
break;
if(G[i][x]==)//防止重边增加入度
{
G[i][x]=;
indegree[x]++;
}
}
}
inittp();
tpsort();
}
return ;
}

POJ 2367 Genealogical tree 拓扑排序入门题的更多相关文章

  1. Poj 2367 Genealogical tree(拓扑排序)

    题目:火星人的血缘关系,简单拓扑排序.很久没用邻接表了,这里复习一下. import java.util.Scanner; class edge { int val; edge next; } pub ...

  2. POJ 2367 Genealogical tree 拓扑题解

    一条标准的拓扑题解. 我这里的做法就是: 保存单亲节点作为邻接表的邻接点,这样就非常方便能够查找到那些点是没有单亲的节点,那么就能够输出该节点了. 详细实现的方法有非常多种的,比方记录每一个节点的入度 ...

  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 Genealogical tree (拓扑排序)

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

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

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

  7. POJ 2367 Genealogical tree【拓扑排序】

    题意:大概意思是--有一个家族聚集在一起,现在由家族里面的人讲话,辈分高的人先讲话.现在给出n,然后再给出n行数 第i行输入的数表示的意思是第i行的子孙是哪些数,然后这些数排在i的后面. 比如样例 5 ...

  8. POJ 2367 Genealogical tree【拓扑排序/记录路径】

    Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7101 Accepted: 4585 Spe ...

  9. POJ 2367 (裸拓扑排序)

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

随机推荐

  1. pycharm虚拟环境

    pycharm虚拟环境 1. 选择一个本地的空目录,---该目录就作为python虚拟环境目录, 2. 选择本地python安装目录: 3. 勾选该选项后则可以使用base interpreter中的 ...

  2. 使用ThinkPHP实现附件上传

    刚学的使用ThinkPHP框架简单上传附件(图片,文档,视频等文件) 首先需要了解tp框架中Upload.class.php(ThinkPHP/Library/Think/Upload,class,p ...

  3. drupal 去掉视图中字段默认的HTML标签

    1.格式--设置 去掉复选框 2.具体字段:

  4. android 解决连接电视机顶盒失败的方法

    今天在开发过程中,需要连接海美迪的电视盒子,这个盒子是基于android6.0的版本,之前连接其它电视盒子都正常,当输入 adb -s xxxx shell后,盒子连接失败,日志如下: error: ...

  5. pytest进阶

    参考文章 使用 pytest pytest 这个 库是一个第三方库,严格来说,它的设计思路不属于 xUnit 系列.但它使用起来比较方便,同时他又兼容 unittest 的用例:用 unittest ...

  6. WOE和IV

    woe全称是"Weight of Evidence",即证据权重,是对原始自变量的一种编码形式. 进行WOE编码前,需要先把这个变量进行分组处理(离散化) 其中,pyi是这个组中响 ...

  7. 联想ThinkPadE455实现FN禁用(F1-F12标准功能与特殊功能切换)

    系统:Win7 64 位     机型:联想ThinkPadE455笔记本 方法一:键盘Fn热键切换功能(亲测可用) Fn+Esc   FnLk  组合键方法启用或禁用Fn锁定功能 具体说明(这个逻辑 ...

  8. EFCore中SQLSERVER 2008 的分页问题

    自SQLSERVER 2012起新增了 Offset Fetch 语法,因此EFCore默认是以此语法生成相应的分页语句的. 如果我们的目标数据库低于 2012,那么EFCore默认生成的语句在执行的 ...

  9. python爬虫_入门_翻页

    写出来的爬虫,肯定不能只在一个页面爬,只要要爬几个页面,甚至一个网站,这时候就需要用到翻页了 其实翻页很简单,还是这个页面http://bbs.fengniao.com/forum/10384633. ...

  10. [luogu P4230]连环病原体

    [luogu P4230] 连环病原体 题意 给定一个长度为 \(n\) 的边序列, 当这个序列的一个子区间内的边都加入图中时产生了环则称其为"加强区间", 求序列中的每条边在多少 ...