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. logstash 处理nginx 访问日志

    [root@dr-mysql01 frontend]# cat logstash_frontend.conf input { file { type => "zj_frontend_a ...

  2. Linux下安装nfs服务器

    1. 安装nfs服务 $sudo apt-get install nfs-kernel-server portmap 2. 在配置文件/etc/exports中添加以下内容/home/jxhui/nf ...

  3. cf455A Boredom

    A. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  4. 协方差cov

    摘录wiki如下(红色字体是特别标注的部分): http://zh.wikipedia.org/wiki/%E5%8D%8F%E6%96%B9%E5%B7%AE 协方差 协方差(Covariance) ...

  5. Python中and和or

    转自:http://unei66.blog.163.com/blog/static/544640292010320745886/ python中的and和or 4.6. and 和 or 的特殊性质在 ...

  6. 网站服务管理系统wdcp简介及功能特性

    WDCP是WDlinux Control Panel的简称,是一套用PHP开发的Linux服务器管理系统以及虚拟主机管理系统,,旨在易于使用Linux系统做为我们的网站服务器,以及平时对Linux服务 ...

  7. CF 444A(DZY Loves Physics-低密度脂蛋白诱导子图)

    A. DZY Loves Physics time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. LeetCode Day3

     Lowest Common Ancestor of a Binary Search Tree import java.util.ArrayList; import java.util.List; / ...

  9. rem和em和px vh vw和% 移动端长度单位

    1.rem和em.px 首先来说说em和px的关系 em是指字体高度 浏览器默认1em=16px,所以0.75em=12px;我们经常会在页面上看到根元素写的font-size:65%; 这样em就成 ...

  10. 二、fragment使用

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8881711 我们都知道,Android上的界面展示都是通过Activity实现的, ...