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. DirectShow控制台输出和保存视频设备名称

    #include "windows.h" #include "TCHAR.h" #include <dshow.h> #include <ve ...

  2. Django——META内部类选项

    Django 模型类的Meta是一个内部类,它用于定义一些Django模型类的行为特性.以下对此作一总结: abstract      这个属性是定义当前的模型类是不是一个抽象类.所谓抽象类是不会对应 ...

  3. js 值和类型

    js中变量是没有类型的,只有值才有类型. 变量随时可以持有任何类型的值. <!DOCTYPE html> <html lang="zh"> <head ...

  4. 【Datastage】NULL VALUE TO A NOT NULL VALUE

    使用ds是报错如上图: 造成这个问题的原因是: 在写SQL时候没有给字段别名与DS中对应一致 例如有下表: CREATE TABLE DataInfo( ID_1 ), ID_2 ) ) CREATE ...

  5. top未加order by,结果出错

    1.查询第21-30条记录 select top 10 * from sys_Module where ID >(select max(ID) from (select top 20 * fro ...

  6. 牛散NO.2:MACD西施说风情,柳下惠高位勿迷情

    创业板日线“高位夺命勾魂枪” 话说在创业板的波段调整中,MACD的勾魂枪同样让多头“魂断蓝桥”.圈内图形又好比西施姑娘回眸一笑,吴王夫差便注定命赴黄泉了.范蠡的精心设计让西施 布了一个风情万种的局,被 ...

  7. 237. Delete Node in a Linked List【easy】

    237. Delete Node in a Linked List[easy] Write a function to delete a node (except the tail) in a sin ...

  8. FromHBITMAP 这个函数会丢失透明信息。

    在用 FromHBITMAP 你会发现出来的图是带有黑边的,这是因为这个函数有个 bug,解决的办法是用下列的函数进行转换,大体意思就是自己 memcpy 不要用 FromHBITMAP 函数. Bi ...

  9. java.lang.NoClassDefFoundError: org/junit/rules/TestRule

    错误原因:通过定位发现是找不到TestRule这个类,检查项目引用的Junit版本为4.7,发现TestRule是在Junit版本4.10后添加的新特性 解决方法:把junit版本由4.7改成4.10

  10. eclipse中mat插件使用

    http://smallnetvisitor.iteye.com/blog/1826434 User.java class User { private String id; private Stri ...