当前位置:网站首页>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;
}
边栏推荐
- Graphic and text hands-on tutorial--ESP32 MQTT docking EMQX local server (VSCODE+ESP-IDF)
- MATLAB程序设计与应用 3.2 矩阵变换
- Leetcode Brush Questions - Path Sum
- 临床研究方法学,到现场,到数据真实发生的地方 | 对话数智 x 张维拓
- 多行函数;group_by分组;having分组后筛选;单表查询总结
- 章节小测一
- 小程序实战(三) - head组件的封装与使用
- MySQL 45 讲 | 11 怎么给字符串字段加索引?
- 【黄啊码】MySQL入门—1、SQL 的执行流程
- 小程序实战(一)- 骨架屏的应用与实现
猜你喜欢

ESP8266-Arduino编程实例-TSL2561亮度传感器驱动

多行函数;group_by分组;having分组后筛选;单表查询总结

【Idea series】idea configuration

Leetcode brush questions - binary search tree related topics (98. Verify binary search tree, 235. The nearest common ancestor of binary search tree, 1038. From binary search tree to bigger sum tree, 5

字节技术官亲码算法面试进阶神技太香了

map的一道题目<单词识别>

云原生Devops 的实现方法

入门MySql表的增删查改

深度强化学习与APS的一些感想

Xilinx VIVADO 中 DDR3(Naive)的使用(2)读写设计
随机推荐
winform 在Datatable插入一笔数据
iMeta | German National Cancer Center Gu Zuguang published a complex heatmap visualization method
DB2查看执行过长的SQL
ECCV 2022 | 清华&腾讯AI Lab提出REALY: 重新思考3D人脸重建的评估方法
zabbix deployment
关于架构的思考
123
ESP8266-Arduino编程实例-APDS-9930环境光和趋近感器驱动
DDL和DML的补充
ESP8266-Arduino编程实例-TSL2561亮度传感器驱动
*iframe*
【黄啊码】MySQL入门—2、使用数据定义语言(DDL)操作数据库
不会还有人不知道防抖吧?
北京大学,新迎3位副校长!其中一人为中科院院士!
网管型交换机比傻瓜交换机多了哪些功能
数据库对象-视图;存储过程
【飞控开发高级教程7】疯壳·开源编队无人机-编队飞行
Leetcode刷题——二叉搜索树相关题目(98. 验证二叉搜索树、235. 二叉搜索树的最近公共祖先、1038. 从二叉搜索树到更大和树、538. 把二叉搜索树转换为累加树)
Using .NET to simply implement a high-performance clone of Redis (2)
深度学习100例 —— 卷积神经网络(CNN)天气识别