timus 1022 Genealogical Tree(拓扑排序)
Genealogical Tree
Memory limit: 64 MB
Background
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.
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
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
≤ 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
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 |
2 4 5 3 1 |
#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(拓扑排序)的更多相关文章
- POJ2367 Genealogical tree (拓扑排序)
裸拓扑排序. 拓扑排序 用一个队列实现,先把入度为0的点放入队列.然后考虑不断在图中删除队列中的点,每次删除一个点会产生一些新的入度为0的点.把这些点插入队列. 注意:有向无环图 g[] : g[i] ...
- POJ 2367 Genealogical tree 拓扑排序入门题
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8003 Accepted: 5184 ...
- Poj 2367 Genealogical tree(拓扑排序)
题目:火星人的血缘关系,简单拓扑排序.很久没用邻接表了,这里复习一下. import java.util.Scanner; class edge { int val; edge next; } pub ...
- [poj2367]Genealogical tree_拓扑排序
Genealogical tree poj-2367 题目大意:给你一个n个点关系网,求任意一个满足这个关系网的序列,使得前者是后者的上级. 注释:1<=n<=100. 想法:刚刚学习to ...
- 1022. Genealogical Tree(topo)
1022 简单拓扑 不能直接dfs 可能有不联通的 #include <iostream> #include<cstdio> #include<cstring> # ...
- POJ 2367 Genealogical tree 拓扑题解
一条标准的拓扑题解. 我这里的做法就是: 保存单亲节点作为邻接表的邻接点,这样就非常方便能够查找到那些点是没有单亲的节点,那么就能够输出该节点了. 详细实现的方法有非常多种的,比方记录每一个节点的入度 ...
- POJ 2367:Genealogical tree(拓扑排序模板)
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7285 Accepted: 4704 ...
- poj 2367 Genealogical tree【拓扑排序输出可行解】
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3674 Accepted: 2445 ...
- 【拓扑排序】Genealogical tree
[POJ2367]Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5696 Accep ...
随机推荐
- [Windows驱动]驱动包(Driver Packages)
在windows下安装一个驱动,我们你需要把所有需要的软件打包-称为驱动包.驱动包里包括系统提供的给所有设备类使用的一般安装工具,还包括了设备商提供的设备特定的组件.下面我们就来看看驱动包里具体需要哪 ...
- osgearth+vs2010安装
转自:http://www.cnblogs.com/eaglezhao/archive/2011/09/26/2192389.html OSGEARTH + VS2010 安装 *VS 平台不重要,本 ...
- SVG 2D入门8 - 文档结构
前面介绍了很多的基本元素,包括结构相关的组合和重用元素,这里先对SVG的文档结构中剩下的相关元素简单总结一下,然后继续向前领略SVG的其他特性. SVG文档的元素基本可以分为以下几类: 动画元素:an ...
- javascript笔记5-BOM
Javascript应用的平台很多,不仅仅针对Web.在Web中使用Javascript,BOM(browser object model,浏览器对象模型)是核心. BOM提供了很多对象,用于访问浏览 ...
- UIImageView异步加载网络图片
在iOS开发过程中,经常会遇到使用UIImageView展现来自网络的图片的情况,最简单的做法如下: 去下载https://github.com/rs/SDWebImage放进你的工程里,加入头文件# ...
- Python OpenCV —— Border
关于border的部分,边缘处理. # -*- coding: utf-8 -*- """ Created on Wed Sep 28 00:58:51 2016 @au ...
- CoreData的使用入门到精通
源码下载地址: http://download.csdn.net/download/huntaiji/6664567 一,创建项目文件--选择Empty Application 起名:CoreDat ...
- codeforces #Round354-div2-D(BFS)
题目链接:题目链接 题意:一个n*m的区域,每个格子都有上下左右四个门,相邻的两个格子A可以通向B当且仅当A对B的门和B对A的门都打开,问从起点S到终点T需要的最短时间 #include<bit ...
- Magento控制器
提到模型-视图-控制器这种MVC架构,要追溯到Smalltalk编程语言和Xerox Parc.从那个时候开始,就有许多系统将自己描述为MVC架构.这些系统虽然在某些地方有细微差别,但都实现了数据层, ...
- 归档 Archive、解档Unchive、 XML(一)
1 使用归档的方式保存文件 1.1 问题 归档是任何对象都可以实现的更常规的方式,可以进行归档的对象需要实现NSCoding协议,而且每个实例变量应该是基本数据类型或者是实现NSCoding协议的某个 ...