Genealogical tree

Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6032 Accepted: 3973 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

//题意: 求出任意一个 1 -- n 的拓扑排列,第一行是 n ,然后 i 行,每行一些数 x ,代表 i 要求在 x 前,0代表结束该行输入

//拓扑排序模板题

DFS

 # include <cstring>
# include <cstdio>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <bitset>
# include <sstream>
# include <set>
# include <cmath>
# include <algorithm>
# pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
# define LL long long
# define pr pair
# define mkp make_pair
# define lowbit(x) ((x)&(-x))
# define PI acos(-1.0)
# define INF 0x3f3f3f3f3f3f3f3f
# define eps 1e-
# define MOD inline int scan() {
int x=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-; ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
}
inline void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N = ;
/**************************/ int n;
bool G[N][N];
int ans[N];
int vis[N];
int dex; bool dfs(int u)
{
vis[u]=-; //标记为正在遍历的
for (int v=;v<=n;v++)
{
if (G[u][v])
{
if (vis[v]==-) return ; //说明组成了环,不能拓扑排序
else if (!vis[v]&&!dfs(v)) return ; //继续遍历未遍历的
}
}
vis[u]=; //标记遍历过了
ans[dex--]=u; //输出的就是这个数组
return ;
} bool toposort()
{
dex=n;
for (int i=;i<=n;i++) //将所有数都遍历
if (!vis[i]&&!dfs(i)) return ;
return ;
}
int main()
{
while (scanf("%d",&n)!=EOF)
{
memset(G,,sizeof (G));
memset(vis,,sizeof(vis));
for (int i=;i<=n;i++)
{
int x;
while ()
{
x = scan();
if (x==) break;
G[i][x]=;
}
}
if (toposort())
{
for (int i=;i<n;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[n]);
}
}
return ;
}

Genealogical tree的更多相关文章

  1. timus 1022 Genealogical Tree(拓扑排序)

    Genealogical Tree Time limit: 1.0 secondMemory limit: 64 MB Background The system of Martians’ blood ...

  2. poj 2367 Genealogical tree

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

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

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

  4. Genealogical tree(拓扑结构+邻接表+优先队列)

    Genealogical tree Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) ...

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

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

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

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

  7. 【拓扑排序】Genealogical tree

    [POJ2367]Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5696   Accep ...

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

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

  9. poj——2367  Genealogical tree

    Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6025   Accepted: 3969 ...

随机推荐

  1. android logo设计

    应用程序图标 (Icon)应当是一个 Alpha 通道透明的32位 PNG 图片.由于安卓设备众多,一个应用程序图标需要设计几种不同大小,如: LDPI (Low Density Screen,120 ...

  2. SpringBoot Tips

    读取资源文件 @RestController @EnableAutoConfiguration public class ResourcesController { @Autowired privat ...

  3. JAVA的IO操作:内存操作流

    掌握内存操作流 输入和输出都是从文件中来的,当然,也可将输出的位置设置在内存上,这就需要ByteArrayInputStream和ByteArrayOutputStream ByteArrayInpu ...

  4. 简单模拟Spring的注入

    主要就是读XML技术和反射技术. 在xml中读出相关配置信息,然后利用反射将其实例化为对象,并调用其构造方法,在实例化的过程中将属性注入实例. 实例化和属性注入这些操作都交给了框架,不再需要自己的去n ...

  5. Java 遍历一个对象的属性 将非空属性赋值给另一个对象

    //将origin属性注入到destination中 public <T> void mergeObject(T origin, T destination) { if (origin = ...

  6. URL中的#号

    一.#的涵义 #代表网页中的一个位置.其右面的字符,就是该位置的标识符.比如, http://www.example.com/index.html#print 就代表网页index.html的prin ...

  7. 关于Azure Storage Blob Content-Disposition 使用学习

    概述 在常规的HTTP应答中,Content-Disposition 消息头指示回复的内容该以何种形式展示,是以内联的形式(即网页或者页面的一部分),还是以附件的形式下载并保存到本地.通俗的解释就是对 ...

  8. spring boot和thrift、zookeeper建立微服务

    原文地址:http://www.cnblogs.com/skyblog/p/5535418.html Spring cloud适应于云端服务,也适用于企业信息化SOA建设.spring boot也是r ...

  9. 【C#】重载重写重构

    前言 前几篇博客说了重写和重载.今天主要说重构,顺便比較一下三者. 重构.重写.重载 重构就是通过调整程序代码改善软件的质量.性能,使其程序的设计模式和架构更趋合理.提高软件的扩展性和维护性. 通俗点 ...

  10. SpringCloud系列十四:实现容错的手段

    1. 回顾 前面已用Eureka实现了微服务的注册与发现,Ribbon实现了客户端侧的负载均衡,Feign实现了声明式的API调用. 2. 实现容错的手段 如果服务提供者响应非常慢,那么消费者对提供者 ...