Genealogical Tree

Time limit: 1.0 second
Memory limit: 64 MB

Background

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.

Problem

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 integers 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 output
5
0
4 5 1 0
1 0
5 3 0
3 0
2 4 5 3 1
Problem Author: Leonid Volkov
【分析】英文渣渣,一开始看了半天都不知道在说什么,后来才知道题意。是这样的,火星人血统非常乱,乱到无法理解,好像是开会的时候,要把议员席给祖先,
 也就是说每个人都得在自己的长辈都坐下之后才能坐下。很明显就是个拓扑排序。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 10000000
#define mod 10000
typedef long long ll;
using namespace std;
const int N=;
const int M=;
int power(int a,int b,int c){int ans=;while(b){if(b%==){ans=(ans*a)%c;b--;}b/=;a=a*a%c;}return ans;}
int in[N],vis[N];
int n,m,k;
vector<int>vec[N];
void dfs(int x)
{
printf("%d ",x);
int f=;
vis[x]=;
for(int i=;i<vec[x].size();i++){
int v=vec[x][i];
if(in[v])in[v]--;
}
for(int i=;i<=n;i++)if(!in[i]&&!vis[i])f=,dfs(i);
if(!f) return;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++){
while(~scanf("%d",&m)&&m){
in[m]++;
vec[i].push_back(m);
}
}
for(int i=;i<=n;i++){
if(!in[i]&&!vis[i]){
dfs(i);
}
}
printf("\n");
return ;
}

timus 1022 Genealogical Tree(拓扑排序)的更多相关文章

  1. POJ2367 Genealogical tree (拓扑排序)

    裸拓扑排序. 拓扑排序 用一个队列实现,先把入度为0的点放入队列.然后考虑不断在图中删除队列中的点,每次删除一个点会产生一些新的入度为0的点.把这些点插入队列. 注意:有向无环图 g[] : g[i] ...

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

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

  3. Poj 2367 Genealogical tree(拓扑排序)

    题目:火星人的血缘关系,简单拓扑排序.很久没用邻接表了,这里复习一下. import java.util.Scanner; class edge { int val; edge next; } pub ...

  4. [poj2367]Genealogical tree_拓扑排序

    Genealogical tree poj-2367 题目大意:给你一个n个点关系网,求任意一个满足这个关系网的序列,使得前者是后者的上级. 注释:1<=n<=100. 想法:刚刚学习to ...

  5. 1022. Genealogical Tree(topo)

    1022 简单拓扑 不能直接dfs 可能有不联通的 #include <iostream> #include<cstdio> #include<cstring> # ...

  6. POJ 2367 Genealogical tree 拓扑题解

    一条标准的拓扑题解. 我这里的做法就是: 保存单亲节点作为邻接表的邻接点,这样就非常方便能够查找到那些点是没有单亲的节点,那么就能够输出该节点了. 详细实现的方法有非常多种的,比方记录每一个节点的入度 ...

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

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

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

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

  9. 【拓扑排序】Genealogical tree

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

随机推荐

  1. Oracle Database 11g express edition

    commands : show sys connect sys as sysdba or connect system as sysdba logout or disc clear screen or ...

  2. JSON基础使用

    1)JSON概念 JSON 是纯文本 JSON 具有“自我描述性”(人类可读) JSON 具有层级结构(值中存在值) JSON 可通过 JavaScript 进行解析 JSON 数据可使用 AJAX ...

  3. java批量插入数据进数据库中

    方式1: for循环,每一次进行一次插入数据. 方式2: jdbc的preparedStatement的batch操作 PreparedStatement.addBatch(); ...... Pre ...

  4. javascript中创建对象的几种方式

    1. 使用Object构造函数来创建一个对象,下面代码创建了一个person对象,并用两种方式打印出了Name的值. var person = new Object(); person.name=&q ...

  5. access手工注入

    1.判断数据类型 and exists (select * from msysobjects) >0 and exists (select * from sysobjects) >0 一般 ...

  6. HttpWebRequest与HttpWebResponse使用例子(转)

    转自:http://www.jb51.net/article/28401.htm 在每个系统出写入报告错误代码(找个合理的理由,比如系统免费升级) -> 自家服务器接收并处理错误报告 -> ...

  7. (转)HTML5 本地存储

    原文:http://www.cnblogs.com/rainman/archive/2011/06/22/2086069.html HTML5 本地存储 1.sessionStorage 2.loca ...

  8. 产生n位元的所有格雷码

    原文链接:http://blog.csdn.net/beiyeqingteng/article/details/7044471 问题:产生n位元的所有格雷码. 格雷码(Gray Code)是一个数列集 ...

  9. Java 集合深入理解(12):古老的 Vector

    点击查看 Java 集合框架深入理解 系列, - ( ゜- ゜)つロ 乾杯~ 今天刮台风,躲屋里看看 Vector ! 都说 Vector 是线程安全的 ArrayList,今天来根据源码看看是不是这 ...

  10. UNION语句查询(转载)

    联合查询   在对数据信息进行操作时,有时需要将不同数据表中的数据信息组合在一起,这时需要使用联合查询.联合查询指的是将多表中的行数据组合在一个数据集中进行显示.本节将讲解有关联合查询方面的相关知识. ...