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 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
 题解:虽然没太懂题意,但是画了个图发现是标准的拓扑结构;
就是好像是外星人生孩子吧;总共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 ;
}

Genealogical tree(拓扑结构+邻接表+优先队列)的更多相关文章

  1. HDU 1535 Invitation Cards(逆向思维+邻接表+优先队列的Dijkstra算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1535 Problem Description In the age of television, n ...

  2. poj3013 邻接表+优先队列+Dij

    把我坑到死的题 开始开题以为是全图连通是的最小值 ,以为是最小生成树,然后敲了发现不是,看了下别人的题意,然后懂了: 然后发现数据大,要用邻接表就去学了一下邻接表,然后又去学了下优先队列优化的dij: ...

  3. NBOJv2——Problem 1037: Wormhole(map邻接表+优先队列SPFA)

    Problem 1037: Wormhole Time Limits:  5000 MS   Memory Limits:  200000 KB 64-bit interger IO format: ...

  4. Prime邻接表+优先队列

    #include <iostream> #include <cmath> #include <cstring> #include <cstdlib> # ...

  5. Reward(拓扑结构+邻接表+队列)

    Reward Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submis ...

  6. HDU 2544 最短路(邻接表+优先队列+dijstra优化模版)

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  7. 确定比赛名次(map+邻接表 邻接表 拓扑结构 队列+邻接表)

    确定比赛名次 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  8. 基于STL优先队列和邻接表的dijkstra算法

    首先说下STL优先队列的局限性,那就是只提供入队.出队.取得队首元素的值的功能,而dijkstra算法的堆优化需要能够随机访问队列中某个节点(来更新源点节点的最短距离). 看似可以用vector配合m ...

  9. 06-图1 列出连通集 (25分)(C语言邻接表实现)

    题目地址:https://pta.patest.cn/pta/test/558/exam/4/question/9495 由于边数E<(n*(n-1))/2 所以我选用了邻接表实现,优先队列用循 ...

随机推荐

  1. java面试题大全-基础方面

    Java基础方面: 1.作用域public,private,protected,以及不写时的区别答:区别如下:作用域           当前类       同一package  子孙类        ...

  2. MEMS市场介绍

    惠普第一.德州仪器第二 市场观察发展报告说,MEMS市场在2007年增长百分之九,达到70亿美元,其中前30名制造商的收入总和有56亿美元,平均增长7个百分点. 惠普(HP)打印机使用MEMS喷墨头, ...

  3. 4.1. 如何在Windows环境下开发Python

    4.1. 如何在Windows环境下开发Python 4.1. 如何在Windows环境下开发Python 4.1.1. Python的最原始的开发方式是什么样的 4.1.1.1. 找个文本编辑器,新 ...

  4. 2014第7周三初识CouchBase

    今天主要还是完善需求,然后提交评审流程,尽可能不纠结一些细节问题后发现自己速度更快了,或许这才是最好的顺序,其它可能的问题就留在后续发现并解决吧.今天第一次听到并重视下couchbase.上午看到同事 ...

  5. fafu 1568 Matrix(二分匹配+二分)

    Description:   You are given a matrix which <= n <= m <= ). You are supposed to choose n el ...

  6. 解决:Incorrect line ending: found carriage return (\r) without corresponding newline (\n)

    解决方案: ——clean一下项目,这个方法可以解决 . 此方案经过验证OK

  7. OC基础5:继承

    "OC基础"这个分类的文章是我在自学Stephen G.Kochan的<Objective-C程序设计第6版>过程中的笔记. 1.根类即是最顶层的类,父类也可称为超类: ...

  8. c/c++性能优化--- cache优化的一点杂谈

    之前写了一篇关于c/c++优化的一点建议,被各种拍砖和吐槽,有赞成的有反对的,还有中立的,网友对那篇博客的的评论和吐槽,我一个都没有删掉,包括一些具有攻击性的言论.笔者有幸阅读过IBM某个项目的框架代 ...

  9. MTK Android4.0.3 ICS 添加缅甸语Myanmar

    最近几个项目需要添加缅甸语,借助网络资源,同时结合自身实践,成功添加缅甸语,现分享经验如下. 一. 前期工作: 准备Myanmar字库,下载地址:http://www.myordbok.com/mya ...

  10. 【最大流】【HDU3338】【Kakuro Extension】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3338 题目大意:填数字,使白色区域的值得和等于有值得黑色区域的相对应的值,用网络流来做 题目思路:增加 ...