---恢复内容开始---

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


题意:给出孩子列表,求一个序列父亲在孩子前

裸的拓扑排序
 vis-1 正在访问
 
只有DAG才有
//
// main.cpp
// poj2367
//
// Created by Candy on 9/10/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=;
int n,ch[N][N],t;
int topo[N],vis[N];
bool dfs(int u){
vis[u]=-;int cnt=ch[u][];
for(int i=;i<=cnt;i++){
int v=ch[u][i];
if(vis[v]==-) return false;
if(!vis[v]&&!dfs(v)) return false;
}
vis[u]=;topo[t--]=u;
return true;
}
bool toposort(){
t=n;
for(int i=;i<=n;i++) if(!vis[i])
if(!dfs(i)) return false;
return true;
}
int main(int argc, const char * argv[]) {
scanf("%d",&n);
for(int i=;i<=n;i++){
int a,cnt=;
while(true){
scanf("%d",&a);if(a==) break;
ch[i][++cnt]=a;
}
ch[i][]=cnt;
}
toposort();
for(int i=;i<=n;i++) printf("%d ",topo[i]);
return ;
}

拓扑排序 POJ2367Genealogical tree[topo-sort]的更多相关文章

  1. 简单选择排序 Selection Sort 和树形选择排序 Tree Selection Sort

    选择排序 Selection Sort 选择排序的基本思想是:每一趟在剩余未排序的若干记录中选取关键字最小的(也可以是最大的,本文中均考虑排升序)记录作为有序序列中下一个记录. 如第i趟选择排序就是在 ...

  2. 拓扑排序(topo sort)之 最大食物链计数( 洛谷P4017)

    前言: 复习复习拓扑排序,自己把自己弄没了/kk 题目传送门 简化题意: 在一个DAG中,求从所有入度为0的点到所有出度为0的点路径的条数 md理解错题意把自己卡了半天,生物学的好的就可以直接理解为求 ...

  3. [MIT6.006] 14. Depth-First Search (DFS), Topological Sort 深度优先搜索,拓扑排序

    一.深度优先搜索 它的定义是:递归探索图,必要时要回溯,同时避免重复. 关于深度优先搜索的伪代码如下: 左边DFS-Visit(V, Adj.s)是只实现visit所有连接某个特定点(例如s)的其他点 ...

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

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

  5. 【拓扑排序】【线段树】Gym - 101102K - Topological Sort

    Consider a directed graph G of N nodes and all edges (u→v) such that u < v. It is clear that this ...

  6. 拓扑排序(Topological Sort)

    Graph 拓扑排序(Topological Sort) 假设一个应用场景:你用 C 编写了一个爬虫工具,其中有很多自定义的库:queue.c.queue.h.stack.c.stack.h.heap ...

  7. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) E. Tree Folding 拓扑排序

    E. Tree Folding 题目连接: http://codeforces.com/contest/765/problem/E Description Vanya wants to minimiz ...

  8. 拓扑排序 Topological Sort

    2018-05-02 16:26:07 在计算机科学领域,有向图的拓扑排序或拓扑排序是其顶点的线性排序,使得对于从顶点u到顶点v的每个有向边uv,u在排序中都在v前.例如,图形的顶点可以表示要执行的任 ...

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

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

随机推荐

  1. RT-Thread入门和模拟器的配置生成

    RT-Thread是一个国产开源的实时操作系统,支持MCU多,外设丰富.值得学习 下载地址:http://www.rt-thread.org/page/31.html ,解压可以得到一下目录结构:|- ...

  2. ng-cordova 手机拍照或从相册选择图片

    1.需求描述 实现一个调用摄像头拍照,或者直接打开本地图库选择照片,然后替换App中图片的功能 2.准备 1) 安装ng-cordova 进入到ionic工程目录,使用bower工具安装, bower ...

  3. arcgis批量处理mxd定义服务中的路径

     >>> from arcpy import env... env.workspace=r"c:\165mxd"... out = r"c:\166mx ...

  4. FIM2010同步用户

    在需要进行同步的来源MA进行同步 在需要进行导入的来源进行导入

  5. Android Studio常用快捷键

    Ctrl+U :如果你的光标在重写父类的一个方法内(如:Activity#onCreate()),这个将会跳到父类的实现上. 如果你的光标在类名上,它将会跳到父类. Ctrl+Alt+Home:它可以 ...

  6. 高级SQL语句

    改变数据 insert record 插入一行 insert into foods (name, type_id) values ('Cinnamon Bobka', 1); 如果在insert语句中 ...

  7. Android简单的ListViewDemo及每个控件的点击事件

    ListView是什么? ListView是一个 数据控件,可以展示从数据库中读取的数据.是.net3.5的新控件. 它比gridview更灵活,而且支持多种模板,支持分页. 文章地址 http:// ...

  8. C语言32个关键字

    auto   局部变量(自动储存) break无条件退出程序最内层循环 case   switch语句中选择项 char单字节整型数据 const定义不可更改的常量值 continue中断本次循环,并 ...

  9. 深入.net(数据类型)

    C#究竟为我们提供了哪些“数据类型”供我们使用?这些类型有什么样的“特征”? 数据类型的分类: --- 数据类型是存放数据的容器.那么我们就以它们“存放数据的方式”分类! 1.值类型:变量中直接存放着 ...

  10. GET和POST请求

    GET与POST请求 简介 GET请求解释及语法格式 POST请求简介及语法 GET请求代码 POST请求代码 GET请求解释及语法格式: 网络请求默认是get 网络请求有很多种:GET查 POST改 ...