POJ 2367:Genealogical tree(拓扑排序)
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 2738 | Accepted: 1838 | Special Judge |
Description
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
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
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
题意:大意就是给你一个N个点的图,而且给你图中的有向边,要你输出一个可行的点拓扑序列就可以.输入格式为,第一行点数N,下面接着有N行,每行以0结尾.第i行包括了以i点为起点的有向边所指的全部节点.
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector> using namespace std; const int M = 100 + 50;
int n;
int in[M];
int out[M];
vector<int> map[M]; void toposort()
{
queue<int>Q;
for(int i=1; i<=n; i++)
if( !in[i] ) //未出现的数
Q.push( i );
int cnt = 0;
while( !Q.empty() )
{
int u = Q.front();
//printf( "%d\n", u );
Q.pop();
out[ ++cnt ] = u; //用数组存下结果
for(int i=0; i<map[u].size(); i++) //每行记录的数字分别入队
{
int m = map[u][i];
if( --in[m] == 0 ) //假设这个数出现多次则先不如队
Q.push( m );
}
}
} int main()
{
while( scanf( "%d", &n )==1 && n )
{
for(int i=1; i<=n; i++)
{
map[i].clear(); //vector数组的初始化
in[i] = 0;
int m;
for( ; ; )
{
scanf( "%d", &m );
if( m==0 )
break;
map[i].push_back( m );
in[m]++;
}
}
toposort();
for(int i=1; i<=n; i++)
{
if( i<n )
printf( "%d ", out[i] );
else
printf( "%d\n", out[i] );
}
} return 0;
}
POJ 2367:Genealogical tree(拓扑排序)的更多相关文章
- 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 ...
- POJ 2367 Genealogical tree 拓扑题解
一条标准的拓扑题解. 我这里的做法就是: 保存单亲节点作为邻接表的邻接点,这样就非常方便能够查找到那些点是没有单亲的节点,那么就能够输出该节点了. 详细实现的方法有非常多种的,比方记录每一个节点的入度 ...
- poj 2367 Genealogical tree
题目连接 http://poj.org/problem?id=2367 Genealogical tree Description The system of Martians' blood rela ...
- 图论之拓扑排序 poj 2367 Genealogical tree
题目链接 http://poj.org/problem?id=2367 题意就是给定一系列关系,按这些关系拓扑排序. #include<cstdio> #include<cstrin ...
- poj 2367 Genealogical tree (拓扑排序)
火星人的血缘关系很奇怪,一个人可以有很多父亲,当然一个人也可以有很多孩子.有些时候分不清辈分会产生一些尴尬.所以写个程序来让n个人排序,长辈排在晚辈前面. 输入:N 代表n个人 1~n 接下来n行 第 ...
- poj 2367 Genealogical tree【拓扑排序输出可行解】
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3674 Accepted: 2445 ...
- POJ 2367 Genealogical tree【拓扑排序/记录路径】
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7101 Accepted: 4585 Spe ...
- POJ 2367 Genealogical tree【拓扑排序】
题意:大概意思是--有一个家族聚集在一起,现在由家族里面的人讲话,辈分高的人先讲话.现在给出n,然后再给出n行数 第i行输入的数表示的意思是第i行的子孙是哪些数,然后这些数排在i的后面. 比如样例 5 ...
- POJ 2367 (裸拓扑排序)
http://poj.org/problem?id=2367 题意:给你n个数,从第一个数到第n个数,每一行的数字代表排在这个行数的后面的数字,直到0. 这是一个特别裸的拓扑排序的一个题目,拓扑排序我 ...
随机推荐
- 【PostgreSQL-9.6.3】LOG: unrecognized configuration parameter "dynamic_shared_memory_type"
报错如下: 输入如下命令启动PG数据库时,报错: [postgres@drz ~]$ pg_ctl -D /opt/postgresql/data/ start server starting FAT ...
- sort 及lambda表达式定制排序
void CDrawCircle::OnBnClickedBtnSelectinfo() { // TODO: 在此添加控件通知处理程序代码 UpdateData(TRUE); BeginEditor ...
- java.lang.NoClassDefFoundError: org/hibernate/validator/internal/engine/DefaultClockProvider
①在springboot的spring-boot-starter-web默认引入了以下依赖: <dependency> <groupId>com.fasterxml.jacks ...
- HTML `capture` 属性
file 类型的 <input> 除了调起系统的文件选择框外,还可通过指定 capture 属性来现场拍照或录制.配合 accept 属性,可实现更加便捷的文件获取. 比如想要录制一段视频 ...
- ThinkPHP---TP功能类之邮件
[一]概论 (1)简介: 这里说的邮件不是平时说的email邮件(邮件地址带有@符号的),而是指的一般论坛网站的站内信息,也叫私信或者pm(private message私信) [二]站内信案例 (1 ...
- 洛谷——P1176 路径计数2
P1176 路径计数2 题目描述 一个N \times NN×N的网格,你一开始在(1,1)(1,1),即左上角.每次只能移动到下方相邻的格子或者右方相邻的格子,问到达(N,N)(N,N),即右下角有 ...
- Java中对象流使用的一个注意事项
再写jsp的实验作业的时候,需要用到java中对象流,但是碰到了之前没有遇到过的情况,改bug改到崩溃!!记录下来供大家分享 如果要用对象流去读取一个文件,一定要先判断这个文件的内容是否为空,如果为空 ...
- static private 与 final 的用法总结
1.static表示静态.他是属于类的.可以在本身类里直接调用,或在其它类里用类名.方法名调用.不加static表示是实例的方法,必须用实例来调用.在本类里也一样,必须用实例调用 2.private是 ...
- TestNG设置测试用例执行优先级
@Test(priority = x)设置测试用例执行优先级.x默认为0,0的优先级最高,0>1>2>3... import org.testng.annotations.Test; ...
- Effective C++ 一些记录和思考
Effective C++ Iter 3 - 尽可能使用 const 一个反逻辑的 bitwise const class Text { ... char& operator[](std::s ...