poj 2367
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 3658 | Accepted: 2433 | Special Judge |
Description
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
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
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
Source
//理解题意非常重要!
#include <stdio.h>
#include <string.h>
int n;
int map[105][105];
int indegree[105];
int queue[105];
void topo()
{
int m,t=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(indegree[j]==0)
{
m=j;
break;
}
}
indegree[m]=-1;
queue[t++]=m;
for(int j=1;j<=n;j++)
{
if(map[m][j]==1)
{
indegree[j]--;
}
}
}
for(int i=0;i<n-1;i++)
printf("%d ",queue[i]);
printf("%d\n",queue[n-1]);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(map,0,sizeof(map));
memset(indegree,0,sizeof(indegree));
int a,b;
for(int i=1;i<=n;i++)
{
while(scanf("%d",&a)&&a)//注意输入的格式,每一行遇到0就结束!
{
if(map[i][a]==0)
{
map[i][a]=1;
indegree[a]++;
}
}
}
topo();
}
return 0;
}
poj 2367的更多相关文章
- POJ 2367 topological_sort
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2920 Accepted: 1962 Spe ...
- POJ 2367 (裸拓扑排序)
http://poj.org/problem?id=2367 题意:给你n个数,从第一个数到第n个数,每一行的数字代表排在这个行数的后面的数字,直到0. 这是一个特别裸的拓扑排序的一个题目,拓扑排序我 ...
- Poj(2367),拓扑排序
题目链接:http://poj.org/problem?id=2367 题意: 知道一个数n, 然后n行,编号1到n, 每行输入几个数,该行的编号排在这几个数前面,输出一种符合要求的编号名次排序. 拓 ...
- 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
今天网易的笔试,妹的,算法题没能A掉,虽然按照思路写了出来,但是尼玛好歹给个测试用例的格式呀,吐槽一下网易的笔试出的太烂了. 就一道算法题,比较石子重量,个人以为解法应该是拓扑排序. 就去POJ找了道 ...
- 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: 8003 Accepted: 5184 ...
- POJ 2367:Genealogical tree(拓扑排序模板)
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7285 Accepted: 4704 ...
随机推荐
- Oracle触发器简单入门记录
写在前面: 最近,老项目新增了日报优化的需求,丽姐让我用触发器去实现当数据插入或者更新的时候,实现对日报表数据更新操作.嗯嗯嗯呢,之前学习数据库的时候,有碰到过触发器,但都是一跳而过,也没怎么去真正的 ...
- SQL表操作习题3 11~13题
- java.util.List.subList ,开区间和闭区间
比如集合中的内容为1,2,3,4,5list.sublist(2,4)就返回一个子集合:它的内容包括从下标为2到下标为4,而且这是左闭右开的就是说是从大于等于2到小于4那子集内容就是3,4(集合的下标 ...
- C/C++框架和库 (真的很强大) 转
http://blog.csdn.net/xiaoxiaoyeyaya/article/details/42541419 值得学习的C语言开源项目 - 1. Webbench Webbench ...
- Word中如何让两个图片垂直居中对齐
如图,两个图片都剧终了,大小也一致,但是发现没有对齐,那是因为需要设置(矩阵框住的)缩进,让两个在一起,上下对齐.
- Springboot集成BeanValidation扩展二:加载jar中的资源文件
一.需求 今天在搭建Springboot框架的时候,又遇到一个需求:在多模块系统中,有些模块想自己管理BeanValidation的资源文件(默认是启动项目claspath下的 ValidationM ...
- 级联关系(内容大部分来自JavaEE轻量型解决方案其余的是我的想法)
1. 级联关系 在Hibernate程序中持久化的对象之间会通过关联关系互相引用.对象进行保存.更新和删除等操作时,有时需要被关联的对象也执行相应的操作,如:假设需要关联关系的主动方对象执行操作时,被 ...
- 新人补钙系列教程之:拒绝CPU高占用
1.关于MovieClip和Sprite的鼠标事件,当不需要鼠标事件的时候将mouseEnabled和mouseChildren设为false. 不断的检测鼠标交互事件会消耗CPU,尤其是大量交互对象 ...
- ECSHOP去版权(删除ECSHOP所有标识)
前台部分: 1:去掉头部TITLE部分的ECSHOP演示站 Powered by ecshop前者”ECSHOP演示站”在后台商店设置 – 商店标题修改后者” Powered by ecshop”打开 ...
- Android——Activity的生命周期
一,Demo測试Activity的生命周期 写两个Activity: package com.example.activity_04; import android.os.Bundle; import ...