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. 这是一个特别裸的拓扑排序的一个题目,拓扑排序我 ...
随机推荐
- JS高级——文件操作
https://www.cnblogs.com/mingmingruyuedlut/archive/2011/10/12/2208589.html https://blog.csdn.net/pl16 ...
- 用Java实现在【520,1314】之间生成随机整数的故事
做一个积极的人 编码.改bug.提升自己 我有一个乐园,面向编程,春暖花开! 在未来城市工作的的程序员小木,做了一个梦,梦到自己在塔鲁姆的街道上看到一个姑娘,这个姑娘从远处走向他,脸上带着微笑.让小木 ...
- PHP 之QQ第三方登录
一.下载QQ SDK 下载地址:http://wiki.open.qq.com/wiki/mobile/SDK 二.配置SDK 三.具体代码 login.html <!DOCTYPE html& ...
- MFC获取各窗口指针句柄
MFC在很多的对话框操作中,我们经常要用到在一个对话框中调用另一个对话框的函数或变量.可以用如下方法来解决. HWND hWnd=::FindWindow(NULL,_T("Sphere ...
- autocad 注册表
序号 版本号1 版本号2 描述 1 R15.0 1:804 2002中文版 2 R15.0 1:409 2002英文版 3 R16.0 201:804 2004中文版 4 R16.0 201:409 ...
- maven引入的包无法使用 解决方法
如果正常引入后在依赖中能够找到包,但是打不开 1.有可能是包下载不完整 把maven下载源由国外转成阿里镜像源找到 maven 的配置文件: settings.xml 文件: <mirror&g ...
- C++ 11常见功能介绍:auto,decltype,nullptr,for,lambda
什么是C++11 C++11是曾经被叫做C++0x,是对目前C++语言的扩展和修正,C++11不仅包含核心语言的新机能,而且扩展了C++的标准程序库(STL),并入了大部分的C++ Technical ...
- <MyBatis>入门五 查询的返回值处理
select : 返回对象: <select id = " " resultType= "对象的全类名" /> List: <sele ...
- 初识 MyBatis
框架技术 使用框架构建项目,当确定使用哪个技术框架后,就已经有了一个 “半成品”,然后在这个半成品上填上内容,完成任务需求. 框架技术的优点: (1)不用再考虑公共问题,框架已经帮我们做好了. (2) ...
- Linux下挂载新磁盘
Linux的硬盘识别: 一般使用”fdisk -l”命令可以列出系统中当前连接的硬盘 设备和分区信息.新硬盘没有分区信息,则只显示硬盘大小信息. 1.关闭服务器加上新硬盘 2.启动服务器,以r ...