当前位置:网站首页>POJ2367Genealogical tree题解
POJ2367Genealogical tree题解
2022-08-04 11:21:00 【bj_hacker】
题目
链接
http://poj.org/problem?id=2367
字面描述
Genealogical tree
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12889 Accepted: 7794 Special Judge
Description
The system of Martians’ blood relations is confusing enough. Actually, Martians bud when they want and where they want. They gather together in different groups, so that a Martian can have one parent 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
The first line of the standard input contains an only number N, 1 <= N <= 100 — a number of members of the Martian Planetary Council. According to the centuries-old tradition members of the Council 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
The standard output should contain in its only line a sequence of speakers’ numbers, separated by spaces. If several sequences satisfy the conditions of the problem, you are to write to the standard 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
Source
Ural State University Internal Contest October’2000 Junior Session
代码实现
拓扑排序模板题
#include<cstdio>
#include<stack>
using namespace std;
const int maxn=100+10;
int n;
int indegree[maxn],topo[maxn];
stack<int>s;
bool map[maxn][maxn];
inline void Topo_sort(){
int cnt=0;
for(int i=1;i<=n;i++){
if(!indegree[i])s.push(i);
}
while(!s.empty()){
int x=s.top();
s.pop();
topo[++cnt]=x;
for(int i=1;i<=n;i++){
if(map[x][i]){
if(--indegree[i]==0)s.push(i);
}
}
}
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
int k;
while(1){
scanf("%d",&k);
if(!k)break;
indegree[k]++;
map[i][k]=true;
}
}
//printf("1\n");
Topo_sort();
//printf("1\n");
for(int i=1;i<=n;i++)printf("%d ",topo[i]);
printf("\n");
return 0;
}
边栏推荐
猜你喜欢

数据库对象

命令模式(Command)

Events in August | 51CTO's 17th Anniversary Celebration, post a blog post to get gifts such as tea sets/notebooks/T-shirts!

vscode插件设置——Golang开发环境配置

Camunda overall architecture and related concepts

Meishe Q&A Room | Meiying VS Meishe Cloud Editing

【LeetCode】653. 两数之和 IV - 输入 BST

Win11文件类型怎么改?Win11修改文件后缀的方法

【Idea series】idea configuration

深度强化学习与APS的一些感想
随机推荐
RL78 development environment
MySql数据库入门的基本操作
Leetcode Brush Questions - Path Sum
多行函数;group_by分组;having分组后筛选;单表查询总结
datax oracle to oracle离线json文件
热成像测温的原理是什么呢?你知道吗?
使用.NET简单实现一个Redis的高性能克隆版(二)
C language * Xiaobai's adventure
解析treeSet集合进行自定义类的排序
手搓一个“七夕限定”,用3D Engine 5分钟实现烟花绽放效果
数据库对象
【虹科案例】基于3D相机组装家具
123
【LeetCode】98.验证二叉搜索树
SkiaSharp 之 WPF 自绘 粒子花园(案例版)
命令模式(Command)
知网网站地址更换
Maple 2022 software installation package download and installation tutorial
【机器学习】:如何对你的数据进行分类?
Leetcode——利用先序遍历特性完成114. 二叉树展开为链表