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. POJ1811(SummerTrainingDay04-G miller-rabin判断素性 && pollard-rho分解质因数)

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 35528   Accepted: 9479 Case ...

  2. Linux awk命令常见使用方法介绍

    Linux awk命令常见使用方法介绍 By:授客 QQ:1033553122   awk运行方式有三种,其中常用的为命令行方式 awk [-F  field_separator]  '{patter ...

  3. android--获取屏幕高宽度工具类

    //获得屏幕相关的辅助类 public class ScreenUtils { private ScreenUtils() { /* cannot be instantiated */ throw n ...

  4. cmd 命令总结

    1.windows 系统定时关机  定时关机:shutdown -s -t 300                 at 18:30 shutdown -s 取消定时:shutdown -a 注意:3 ...

  5. 使用 PowerShell 创建 Azure VM 的自定义映像

    自定义映像类似于应用商店映像,不同的是自定义映像的创建者是你自己. 自定义映像可用于启动配置,例如预加载应用程序.应用程序配置和其他 OS 配置. 在本教程中,你将创建自己的 Azure 虚拟机自定义 ...

  6. mysql processlist 线程状态

        Analyzing 线程是对MyISAM 表的统计信息做分析(例如, ANALYZE TABLE ).   checking permissions 线程是检查服务器是否具有所需的权限来执行该 ...

  7. InfoPath读取数据库

    public void LoadBtn_Clicked(object sender, ClickedEventArgs e) { // 配置连接字符串 using (SqlConnection con ...

  8. 清空数据库错误:因为该表正由 FOREIGN KEY 约束引用 解决办法

    如下解决了五个问题 1. 清空数据 2. 有外键也可以, 因为是逆向删除, 从最后一张表删除. 且使用的是delete, 因为truncate不能对有外键的表 3. 种子问题, 如果表存在种子重设为0 ...

  9. pt-heartbeat工具监控MySQL复制延迟

    pt-heartbeat工作原理: 1,在主库上的某个数据库A中创建一张heartbeat表,按照一定的时间频率更新该表的字段(把时间更新进去). 2,从主库连接到从上的这个数据库A中检查复制的时间记 ...

  10. 如何创建只读权限oracle账户

    1.创建角色 CREATE ROLE SELECT_ROLE 2.给角色分配权限 grant CREATE VIEW to SELECT_ROLE; grant CREATE SYNONYM to S ...