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. 这是一个特别裸的拓扑排序的一个题目,拓扑排序我 ...
随机推荐
- c#数组的交集,差集,并集
, , , , }; , , , , }; // 差集 var z1 = x.Except(y); foreach (var i in z1) { Console.Write(i + " & ...
- C# json与对象之间的相互转换
1. 添加命名空间 using System.Runtime.Serialization.Json; 2. WriteObject方法 // 从一个对象信息生成Json串 public static ...
- storm简介[ZZ]
场景 伴随着信息科技日新月异的发展,信息呈现出爆发式的膨胀,人们获取信息的途径也更加多样.更加便捷,同时对于信息的时效性要求也越来越高.举个搜索 场景中的例子,当一个卖家发布了一条宝贝信息时,他希望的 ...
- css字体大小设置em与rem的区别
em 单位em是相对于父元素的,如果父元素没有设置字体大小,那就会追溯到body. 比如 如果我在box_text的父元素box加了一个字体大小 那么body的8px就会被box_text的父元 ...
- iOS+Swift: 使用MessageUI.framework发送短信
在iOS中, 可以使用MessageUI.framework框架发送短信, 步骤如下: 代码下载http://git.oschina.net/yao_yu/swift_cnblogs_samples/ ...
- 常见排序算法(JS版)
常见排序算法(JS版)包括: 内置排序,冒泡排序,选择排序,插入排序,希尔排序,快速排序(递归 & 堆栈),归并排序,堆排序,以及分析每种排序算法的执行时间. index.html <! ...
- matlab的常用快捷键
ctrl+shift+d:控制窗口嵌入还是非嵌入
- DIV焦点事件
div本来是没有focus和blur事件的. 如果用div来模拟一个input标签,同时需要它和input一样响应focus和blur事件, 就需要给他加上attribute:tabindex An ...
- 2016 年开发者应该掌握的十个 Postgres 技巧
[编者按]作为一款开源的对象-关系数据库,Postgres 一直得到许多开发者喜爱.近日,Postgres 正式发布了9.5版本,该版本进行了大量的修复和功能改进.而本文将分享10个 Postgres ...
- Hibernate 缓存 关于注解方式
要引入 import org.hibernate.annotations.Cache; 在类前面添加: @Cache(usage= CacheConcurrencyStrategy.NONSTRICT ...