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. 踏着前人的脚印学Hadoop——序列化,Writerable

    package org.apache.hadoop.io; import java.io.DataOutput;import java.io.DataInput;import java.io.IOEx ...

  2. Android 获取网络状态

    1.检测网络是否可用 public boolean isNetWorkConnected() { ConnectivityManager cm = (ConnectivityManager)getSy ...

  3. (kate)win8-64位系统下opencv-2.4.3的安装以及在visual_studio2010中配置

    环境: 操作系统:window8.1 64bit Opencv版本:OPencv-2.4.3 VS版本:vs 2010 一.安装Opencv 1.Opencv官网http://opencv.org/ ...

  4. HTML--2图片热点,网页划区,拼接

    图片热点: 规划出图片上的一个区域,可以做出超链接,直接点击图片区域就可以完成跳转的效果. 示例: 网页划区: 在一个网页里,规划出一个区域用来展示另一个网页的内容. 示例: 网页的拼接: 在一个网络 ...

  5. unity3d基础02

    调试: 在MonoDevelop里可以断点调试,注意绑定unity进程 使用Debug.Log()打印信息 创建游戏对象: GameObject test = GameObject.CreatePri ...

  6. Note_Master-Detail Application(iOS template)_04_ YJYMasterViewController.h

    //YJYMasterViewController.h #import <UIKit/UIKit.h> @classYJYDetailViewController; #import < ...

  7. 黑马程序员——【Java基础】——面向对象(二)异常机制、包(Package)

    ---------- android培训.java培训.期待与您交流! ---------- 一.异常机制 (一)异常概述 1.异常:就是程序在运行时出现不正常情况. 2.异常类:程序在运行时,出现的 ...

  8. Python 温习

    关于Python内置函数的示例 Type "copyright", "credits" or "license()" f重写or more ...

  9. BZOJ 2241 打地鼠

    暴力. 这怎么这么快.... #include<iostream> #include<cstdio> #include<cstring> #include<a ...

  10. Linux怎么使用添加的新硬盘

    一.磁盘分区 装过系统后第一块磁盘的设备号是/dev/sda,在你添加一个新的磁盘后一般情况下是/dev/sdb *******进入fdisk界面***** # fdisk /dev/sdbDevic ...