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

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. JavaScript 事件入门

    一.事件介绍 JavaScript 有三种事件模型:内联模型.脚本模型和 DOM2 模型. 二.内联模型 //在 HTML 中把事件处理函数作为属性执行 JS 代码 <input type=&q ...

  2. 从零开始,做一个NodeJS博客(一):Heroku上的最简NodeJS服务器

    标签:NodeJS,Heroku 0 这里是这个伪系列的第一篇,因为我也不知道自己能不能做完,然后到底能做成什么样子.总之,尽力而为吧,加油. 1 Heroku App 的构成 Heroku 所谓的 ...

  3. 给SHP文件定义投影

    #!/usr/bin/env python # -*- coding: utf-8 -*- import urllib.request import os def get_epsg_code(epsg ...

  4. JS魔法堂:jsDeferred源码剖析

    一.前言 最近在研究Promises/A+规范及实现,而Promise/A+规范的制定则很大程度地参考了由日本geek cho45发起的jsDeferred项目(<JavaScript框架设计& ...

  5. 动态改变actionbar上menu的图标

    工作中遇到了一个需求是,在滚动的时候让actionbar上的图标进行变色.实现后在这里总结下思路. 一.先在主题中定义好Actionbar的style <style name="App ...

  6. 【代码笔记】iOS-单项选择框

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...

  7. HTML5设计网页熔岩灯导航(navigation bar)插件 已经加上完整源代码

    导航栏(navigation bar): 1.指位于页眉区域的,在页眉横幅图片上边或下边的一排水平导航按钮,它起着链接博客的各个页面的作用. 2.网页设计中不可缺少的部分,它是指通过一定的技术手段,为 ...

  8. JSP与Servelt的区别

    相同点: 两者都是服务端的技术,而JSP本质上就是Servelt: 都可以处理来自客户端的请求,都可以对请求作出响应: 都可以生成HTML页面返回. 不同点: 在实际开发中,对JSP编程成响应的HTM ...

  9. od

    $od [-t type]查看非文本文件 a 使用默认字符输出 c 使用ASC II字符输出 d[size] 使用十进制来输出数据,每个整数占用size byte o ..八 x ..十六 f ..浮 ...

  10. Ambari工具之认识学习01

    随着大数据的越发流行,大大小小的互联网公司都开始参与一些大数据相关的业务,都想从中分一杯羹.Hadoop是大数据的典型代表,也可以说现在的领头大哥,很多互联网巨头都在使用hadoop,包括BAT等大公 ...