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. MyEclipse8.5集成Tomcat7

    我最近需要在MyEclipse中使用Tomcat7,已经在Servers中配置了本地的Tomcat路径,之后发布项,在MyEclipse启动Tomcat服务则出现如下错误提示: Exception i ...

  2. ByteArray

    ByteArray:属性endian:String == Endian.BIG_ENDIAN/Endian.LITTLE_ENDIAN.length:uint ByteArray的字节数positio ...

  3. Android ViewPager 里有子ViewPager的事件冲突

    在Android应用中有时候要用到类似网易新闻左右滑动页面且页面里又有左右滑动的图片功能,我不知道网易是怎么实现的,本人的做法是外面的BaseFragmentActivity布局就是TabViewPa ...

  4. git命令学习用

  5. Oracle Enterprise Metadata Management (简称OEMM,Oracle元数据管理)12.1.3.0.1已经发布

    在数据处理及数据仓库建设中,元数据管理是必不可少的,OEMM可以解决元数据管理过程中各种关键业务问题和技术挑战,其中包括如何元数据的统计信息,了解变更数据之后对下游的影响范围,而且OEMM站在业务的角 ...

  6. joinfetch之意义

    既然被join的对象早晚都要用到,为什么要先从A表取这边的独享,再根据关联关系取B表中的对象,分两次或者多次进行,增加数据库的负载呢? 为什么不把A表和B表join成一张表,从这个组合表中把要取的对象 ...

  7. xcode 5.0 以上去掉icon高亮方法&iOS5白图标问题

    之前的建议方法是把在xxx.info.plist文件中把 icon already includes gloss and bevel effects 设置YES 在Xcode5下,反复实现不成功,今天 ...

  8. 码表由来:ascll码-Gbk2312-GBK-Unicode-UTF-8

    码表ascll码-Gbk2312-GBK-Unicode-UTF-8, ascll是基本的标准码表,GB2312是中文码表,GBK是扩展之后的码表,Unicode是国际通用码表,UTF-8是优化后的U ...

  9. RAID-4与模2和

    在网络传输和磁盘数据管理中经常涉及到的所谓奇偶校验:每N个bit之后加上一个bit保证这N + 1bit的模2和为0(也叫异或,一个意思) 而如果这其中出现了单bit错, 直接导致校验出差,出现偶数b ...

  10. lower_bound和upper_bound算法

    参考:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html ForwardIter lower_bound(ForwardIte ...