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. 这是一个特别裸的拓扑排序的一个题目,拓扑排序我 ...
随机推荐
- SQL server 上机练习题
首先创建一个数据库,里面有 登录表 学生表 课程表 选课表 成绩表 1. 查询Student表中的所有记录的Sname.Ssex和Class列.2. 查询教师所有的单位即不重复的Depart列 ...
- Git——github基本操作
基本概念 上一篇文章写到git共享仓库,但是有个局限性,就是这个仓库存在于本地,其他人无法从我们这个仓库拿到共享的内容 但是我们可以将这个共享仓库放入一个远程的服务器上,然后设置一些登录权限就能完美的 ...
- golang tar gzip 压缩,解压(含目录文件)
tar是用于文件归档,gzip用于压缩.仅仅用tar的话,达不到压缩的目的.我们常见的tar.gz就是用gzip压缩生成的tar归档文件. go实现tar压缩与解压与zip类似,区别在于tar需要使用 ...
- 【转载】Appium环境搭建(Windows版)
注:appium安装到C盘,node.js安装到C盘 一.安装node.js 1.到官网下载node.js:https://nodejs.org/en/download/ 2.获取到安装文件后,直接双 ...
- Python学习-字符串的基本知识
字符串的基本知识 根据所展示形式的不同,字符串也可以分为两类 原始字符串: 使用单引号包括:‘liuwen’ 使用双引号包括:"liuwen" 使用3个单引号包括 :'''liuw ...
- MyBatis 的基本要素—核心对象
MyBatis 三个基本要素 ➢ 核心接口和类 ➢ MyBatis 核心配置文件(mybatis-config.xml) ➢ SQL 映射文件(mapper.xml) MyBatis 核心接口和类 ...
- 腾讯云:基于 Ubuntu 搭建 VNC 远程桌面服务
基于 Ubuntu 搭建 VNC 远程桌面服务 前言 任务时间:5min ~ 10min 必要知识 本教程假设您已学习以下 Ubuntu 基本操作: 连接 SSH 执行命令 编辑文件 如果还没有掌握 ...
- 1.ARM嵌入式体系结构与接口技术(Cortex-A8版)
第1章 嵌入式系统基础知识 ---->1.1嵌入式系统的概述 -------->1.1.1嵌入式系统简介 -------->1.1.2嵌入式系统的特点 -------->1.1 ...
- Leetcode 80.删除重复数组的重复项
删除重复数组的重复项 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间 ...
- HDU 1130
题目大意 给定节点数 , 求通过这么多个节点能得到的二叉树的组成方式 用卡特兰数解决 f[n] = (4*n-2) * f[n-1] / (n+1); 递归不断解决 /** * @(#)Main.ja ...