首页 技术 正文
技术 2022年11月17日
0 收藏 346 点赞 4,848 浏览 2664 个字

Genealogical tree

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 5   Accepted Submission(s) : 3
Special Judge

Problem DescriptionThe 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.  InputThe 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. OutputThe 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 Input5 0 4 5 1 0 1 0 5 3 0 3 0  Sample Output2 4 5 3 1 题解:虽然没太懂题意,但是画了个图发现是标准的拓扑结构;就是好像是外星人生孩子吧;总共N个人;第i行的数据是第i个人的孩子;每组数据0结束;代码:

 #include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int MAXN=;
struct Node{
int to,next;
};
Node edg[MAXN*MAXN];
int head[MAXN],que[MAXN],ans[MAXN],top,N;
priority_queue<int,vector<int>,greater<int> >dl;
void topu(){
for(int i=;i<=N;i++){
if(!que[i])dl.push(i);
}
while(!dl.empty()){
ans[top++]=dl.top();
int k=dl.top();
dl.pop();
for(int j=head[k];j!=-;j=edg[j].next){
que[edg[j].to]--;
if(!que[edg[j].to])dl.push(edg[j].to);
}
}
for(int i=;i<top;i++){
if(i)printf(" ");
printf("%d",ans[i]);
}
puts("");
}
void initial(){
memset(head,-,sizeof(head));
memset(que,,sizeof(que));
top=;
while(!dl.empty())dl.pop();
}
int main(){
int a;
while(~scanf("%d",&N)){
initial();
int k=;
for(int i=;i<=N;i++){
while(scanf("%d",&a),a){
edg[k].to=a;
edg[k].next=head[i];
head[i]=k;
k++;
que[a]++;
}
}
topu();
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,031
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,860