当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
Oracle中对临时表空间执行shrink操作
北京大学,新迎3位副校长!其中一人为中科院院士!
Win11怎么重装显卡驱动程序?Win11显卡驱动怎么卸载重装?
MATLAB程序设计与应用 3.1 特殊矩阵
*iframe*
A topic of map
外键约束;外键约束
Leetcode - using sequence traversal features first completed 114. The binary tree to the list
Camunda overall architecture and related concepts
Using .NET to simply implement a high-performance clone of Redis (2)
随机推荐
winform 在Datatable插入一笔数据
Leetcode——利用先序遍历特性完成114. 二叉树展开为链表
『快速入门electron』之实现窗口拖拽
apache dolphin scheduler 文件dolphinscheduler-daemon.sh详解
DQL-查询操作
北京大学,新迎3位副校长!其中一人为中科院院士!
Oracle中对临时表空间执行shrink操作
【Qt】解决 “由于找不到Qt5Cored.dll,无法继续执行代码”(亲测有效)
Maple 2022 software installation package download and installation tutorial
The use of DDR3 (Naive) in Xilinx VIVADO (2) Read and write design
Zikko launches new Thunderbolt 4 docking station with both HDMI2.1 and 2.5GbE
Mysql——》类型转换符binary
ECCV 2022 | 清华&腾讯AI Lab提出REALY: 重新思考3D人脸重建的评估方法
ORA-00054 资源正忙
Leetcode刷题——543. 二叉树的直径、617. 合并二叉树(递归解决)
cubemx stm32 afm3000 module gas flow sensor driver code
Leetcode刷题——路径总和
Win11文件类型怎么改?Win11修改文件后缀的方法
你值得拥有的登录注册页面(附赠源码)
Graphic and text hands-on tutorial--ESP32 MQTT docking EMQX local server (VSCODE+ESP-IDF)