poj 2367 Genealogical tree【拓扑排序输出可行解】
| Time Limit: 1000MS | Memory Limit: 65536K | |||
| Total Submissions: 3674 | Accepted: 2445 | 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 题意:给一个数字n接下来有n行数据,每行数据以输入0为结束标志,第i行表示第i行的所有点连在点i上(如第二行的4 5 1表示2->4 2->5 2->1)让你任意输出一组排好序之后的序列
题解:反向拓扑输出可行解
#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
using namespace std;
int n;
int vis[110];
vector<int>map[110];
int b[110];
void getmap()
{
int i,j;
memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++)
map[i].clear();
for(i=1;i<=n;i++)
{
int a;
while(scanf("%d",&a),a)
{
map[a].push_back(i);
vis[i]++;
}
}
}
void tuopu()
{
int i,j;
queue<int>q;
memset(b,0,sizeof(b));
while(!q.empty())
q.pop();
for(i=1;i<=n;i++)
if(vis[i]==0)
q.push(i);
int u,v;
int ans=0;
while(!q.empty())
{
u=q.front();
q.pop();
b[ans++]=u;
for(i=0;i<map[u].size();i++)
{
v=map[u][i];
vis[v]--;
if(vis[v]==0)
q.push(v);
}
}
for(i=ans-1;i>=1;i--)
printf("%d ",b[i]);
printf("%d\n",b[0]);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
getmap();
tuopu();
}
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: 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. 这是一个特别裸的拓扑排序的一个题目,拓扑排序我 ...
随机推荐
- 数据库(学习整理)----4--Oracle数据查询(基础点1)
其他: 计算机中的内存是线性的,一维. length('')计算字符的个数,而不是字节的个数 Oracle中的日期类型和数值类型的数据可以做运算符(>,=,<,<>)比较 如果 ...
- 解决GDB输出Qt内置类型的显示问题
自从GDB 7.0之后,就加入了Pretty-Printer的这个概念.简单理解就是他可以让你用Python写一串脚本,然后让gdb去读取这串脚本后,可以自由的输出由你想自己定义的格式.我们直接举个简 ...
- Java学习----finally块
public class Test { String x; public static void main(String[] args) { Test test = new Test(); try { ...
- R.Net 用法
using (REngine engine = REngine.CreateInstance("RDotNet")) { engine.Initialize(); var expa ...
- MySQL简单优化
1:对 MySQL 优化是一个综合性的技术,主要包括: a.标的设计合理(符合 3NF) b.添加适当索引(index)(普通索引.主键索引.唯一索引 unique.全文索引) ...
- JavaScript学习心得(七)
一 创建事件监听器 开发人员往往使用事件和元素组合来命名事件处理函数. 创建事件监听器方法: 嵌入式事件处理器即将JavaScript函数赋值给HTML元素属性(不推荐使用:污染HTML:无法应用渐进 ...
- Quartz1.8.5例子(二)
/* * Copyright 2005 - 2009 Terracotta, Inc. * * Licensed under the Apache License, Version 2.0 (the ...
- 扩展原生js的一些方法
扩展原生js的Array类 Array.prototype.add = function(item){ this.push(item); } Array.prototype.addRange = fu ...
- Contest20140705 testA 二分
testA 输入文件: testA.in 输出文件testA.out 时限2000ms 问题描述: 有一个城市拥有N个节点,被M条有权无向路径连接.现在你要在一个地方(可以在路径上当然也可以在节点上) ...
- [BZOJ 3564] [SHOI2014] 信号增幅仪 【最小圆覆盖】
题目链接:BZOJ - 3564 题目分析 求最小椭圆覆盖,题目给定了椭圆的长轴与 x 轴正方向的夹角,给定了椭圆长轴与短轴的比值. 那么先将所有点旋转一个角度,使椭圆长轴与 x 轴平行,再将所有点的 ...