【拓扑排序】Genealogical tree
[POJ2367]Genealogical tree
| Time Limit: 1000MS | Memory Limit: 65536K | |||
| Total Submissions: 5696 | Accepted: 3729 | Special Judge | ||
Description
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
Output
Sample Input
5
0
4 5 1 0
1 0
5 3 0
3 0
Sample Output
2 4 5 3 1
Source
直接拓扑……
代码
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
//#include<cmath> using namespace std;
const int INF = 9999999;
#define LL long long inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
int N,M;
bool dis[501][501];
bool vis[501];int ru[501]; int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
N=read();
for(int i=1;i<=N;i++){
int a=read();
while(a!=0) ru[a]++,dis[i][a]=true,a=read();
}
int tmp;
for(int i=1;i<=N;i++){
if(!ru[i]) {printf("%d ",i);tmp=i;break;}
}
vis[tmp]=true;
for(int i=1;i<N;i++){
for(int j=1;j<=N;j++)
if(dis[tmp][j]) dis[tmp][j]=false,ru[j]--;
for(int j=1;j<=N;j++) if(!ru[j]&&!vis[j]){
vis[j]=true; tmp=j;
if(i!=N-1)printf("%d ",j);
else printf("%d\n",j);
break;
}
}
return 0;
}
【拓扑排序】Genealogical tree的更多相关文章
- 拓扑排序 POJ2367Genealogical tree[topo-sort]
---恢复内容开始--- Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4875 A ...
- timus 1022 Genealogical Tree(拓扑排序)
Genealogical Tree Time limit: 1.0 secondMemory limit: 64 MB Background The system of Martians’ blood ...
- 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 ...
- POJ 2367:Genealogical tree(拓扑排序)
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2738 Accepted: 1838 Spe ...
- poj 2367 Genealogical tree (拓扑排序)
火星人的血缘关系很奇怪,一个人可以有很多父亲,当然一个人也可以有很多孩子.有些时候分不清辈分会产生一些尴尬.所以写个程序来让n个人排序,长辈排在晚辈前面. 输入:N 代表n个人 1~n 接下来n行 第 ...
- 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【拓扑排序/记录路径】
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7101 Accepted: 4585 Spe ...
随机推荐
- C++ Vector 中自定义对象的排序
需求: 客户端收到游戏中的所有联盟列表,现在需要按联盟的属性比如lv来进行排序. 数据存储: 每个联盟数据是一个对象,所有的联盟列表存在一个vector容器里面. 老的解决方法: 冒泡排序方法算法 新 ...
- 简易微信小程序签到功能
一.效果图 点击签到后 二.数据库 用一张数据表存用户签到的信息,每次用户签到都会往表中添加一条记录了用户id和签到日期的数据,如下图 三.后端 后端写两个接口,一个用于查询用户今日是否签到和签到记录 ...
- 15、简述MySQL的执行计划?
具体的Mysql的执行计划,请参考下面的链接: MySQL_执行计划详细说明
- feign hystrix 线程池伸缩控制
当前使用的版本 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spr ...
- LCD实验学习笔记(十):TFT LCD
硬件组成: REGBANK是LCD控制寄存器组,含17个寄存器及一块256*16的调色板,用来设置参数. LCDCDMA中有两个FIFO,当FIFO空或数据减少到阈值,自动发起DMA传输,从内存获取图 ...
- Python3 嵌套函数
嵌套函数: 函数体内用def定义函数 注意:函数体中调用其他函数不算嵌套函数,只能是函数的调用 简单的嵌套函数: 输出结果:
- copy_from_user分析
前言 copy_from_user函数的目的是从用户空间拷贝数据到内核空间,失败返回没有被拷贝的字节数,成功返回0.它内部的实现当然不仅仅拷贝数据,还需要考虑到传入的用户空间地址是否有效,比如地址是不 ...
- python基础===tkinter学习链接
http://effbot.org/tkinterbook/tkinter-classes.htm
- 浅析MongoDB用户管理
浅析MongoDB用户管理 http://www.jb51.net/article/53830.htm mongodb3.03开启认证 http://21jhf.iteye.com/blog/2216 ...
- C中级 数据序列化简单使用和讨论 (二)
引言 - 一种更好的方式 其实不管什么语言, 开发框架都会遇到序列化问题. 序列化可以理解为A 和 B 交互的一种协议. 很久以前利用 printf 和 scanf 的协议实现过一套序列化问题. C ...