Genealogical tree(拓扑结构+邻接表+优先队列)
Genealogical tree
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 5 Accepted Submission(s) : 3
Special Judge
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.
#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(拓扑结构+邻接表+优先队列)的更多相关文章
- HDU 1535 Invitation Cards(逆向思维+邻接表+优先队列的Dijkstra算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1535 Problem Description In the age of television, n ...
- poj3013 邻接表+优先队列+Dij
把我坑到死的题 开始开题以为是全图连通是的最小值 ,以为是最小生成树,然后敲了发现不是,看了下别人的题意,然后懂了: 然后发现数据大,要用邻接表就去学了一下邻接表,然后又去学了下优先队列优化的dij: ...
- NBOJv2——Problem 1037: Wormhole(map邻接表+优先队列SPFA)
Problem 1037: Wormhole Time Limits: 5000 MS Memory Limits: 200000 KB 64-bit interger IO format: ...
- Prime邻接表+优先队列
#include <iostream> #include <cmath> #include <cstring> #include <cstdlib> # ...
- Reward(拓扑结构+邻接表+队列)
Reward Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submis ...
- HDU 2544 最短路(邻接表+优先队列+dijstra优化模版)
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 确定比赛名次(map+邻接表 邻接表 拓扑结构 队列+邻接表)
确定比赛名次 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- 基于STL优先队列和邻接表的dijkstra算法
首先说下STL优先队列的局限性,那就是只提供入队.出队.取得队首元素的值的功能,而dijkstra算法的堆优化需要能够随机访问队列中某个节点(来更新源点节点的最短距离). 看似可以用vector配合m ...
- 06-图1 列出连通集 (25分)(C语言邻接表实现)
题目地址:https://pta.patest.cn/pta/test/558/exam/4/question/9495 由于边数E<(n*(n-1))/2 所以我选用了邻接表实现,优先队列用循 ...
随机推荐
- 使用AES加密的帮助类
在开发中经常使用加密/解密对一些内容进行处理,比如密码在存入数据库之前先经过加密处理等等,这里就把一个加密帮助类代码贴出来,供以后查找使用. 这个帮助类主要功能是对字符串和字节数组进行加密解密处理. ...
- qt 自动完成LineEdit
原地址:http://www.cppblog.com/biao/archive/2009/10/31/99873.html ---------------------------------- ...
- zookeeper 各节点数据保证是弱一致性
一致性保证: ZooKeeeper 是一个高性能的,可扩展的服务.不管是读和写操作是被设计成快速,虽然读比写快. 这样做的原因是在读的情况下,Zookeeper 可以提供旧的数据, 反过来又是由于Zo ...
- arm+linux 裸机环境搭建之安装工具篇(eclipse)
之前已经讲述如何安装gcc和gdb,在此不赘述! 一.所需要的软件有两个: jre-7u25-linux-i586.rpm(虚拟机) eclipse-cpp-kepler-R-linux-gtk .t ...
- jquery选择器从认识到使用初级篇
1. .class 选择器 ---一种通过元素类别属性查找元素 调用格式: $(".class") ----其中参数表示元素的css类别名称(类选择器)<input cl ...
- 05JS高级 方法没有块级作用域
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- iOS断点及打印日志
首先,最简单的断点就是在Xcode项目文件中任意一行行号那点一下,就是加了一个断点 再次点击会变成浅蓝色,表示disable掉了 disable掉的断点不会起作用,但会在左上角蓝色的标签那留下记录,这 ...
- [stack]Evaluate Reverse Polish Notation
Total Accepted: 55722 Total Submissions: 249668 Difficulty: Medium Evaluate the value of an arithmet ...
- Linux编程C/C++
C/C++基本数据类型 C/C++语言有一组基本数据类型,对应于计算机的基本存储单元和使用这些单元去保存数据的一些常用方式. 基本数据类型如下: 上面表格中的类型是基本的C/C++数据类型,但是在C+ ...
- JavaScript基本概念(操作符)
一元操作符 一元操作符在处理所有的非数值时,相当于将该值经过Number()转换成数值,如 +"12" 将把 "12" 字符串转换为数字. 位操作符 负数在计算 ...