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

    Friend-Graph Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  2. CSS3和HTML5新增特性及使用(保留方便查看)

    CSS3 1.边框图片 border-image: url(test.png) 10/10px;  outline:10px solid #ff0;outline-offset:15px;边框的边框, ...

  3. dubbo 图片服务器(FastDFS) redis solr ActiveMQ等简单配置使用

    一.dubbo 项目基于soa的架构,表现层和服务层是不同的工程.所以要实现商品列表查询需要两个系统之间进行通信. 1.1如何实现远程通信? 1.Webservice:效率不高基于soap协议.项目中 ...

  4. Memcached+WebApi记录

    一.安装Memcached Memcached1.2.6 http://files.cnblogs.com/files/jasonduan/11465401756756.zip Memcached.C ...

  5. soapUI 再谈SoapUI接口测试--文件组织与接口“布局”管理

    再谈SoapUI接口测试--文件组织与接口“布局”管理 by:授客 QQ:1033553122 SoapUI-Pro-x64-5.1.2_576025(含破解文件),软件下载地址: http://pa ...

  6. idea 关联 jdk

    1.打开IntelliJ IDEA 2.选择 "File" 菜单 3.找到 "other settings" 4.选择 “Structure for new P ...

  7. [我的阿里云服务器] —— FTP配置

    前言: FTP是我们用来向服务器上传或者下载文件很重要的一个工具,特别是云服务器,无法使用外设传送文件. 所以下面我们就来配置一下FTP,但是FTP的21端口,通常也是黑客进攻的一个目标,所以需要小心 ...

  8. cuda中当数组数大于线程数的处理方法

    参考stackoverflow一篇帖子的处理方法:https://stackoverflow.com/questions/26913683/different-way-to-index-threads ...

  9. 【Python】生成词云

    import matplotlib.pyplot as plt from wordcloud import WordCloud import jieba text_from_file_with_apa ...

  10. eclipse中svn插件的工程不能与svn资源库同步的解决方法

    eclipse中svn插件的工程不能与svn资源库同步的解决办法 最近几天自己的工程与资源库同步总是出现问题,重启机器后发现资源库丢失了,无法进行同步. 解决办法如下: 1.右键工程---->选 ...