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 所以我选用了邻接表实现,优先队列用循 ...
随机推荐
- 强化一下,QDialog有专门的exec函数和finished信号,QWidget都没有
http://blog.csdn.net/dbzhang800/article/details/6300519
- C#调用C++DLL传递结构体数组的终极解决方案
在项目开发时,要调用C++封装的DLL,普通的类型C#上一般都对应,只要用DllImport传入从DLL中引入函数就可以了.但是当传递的是结构体.结构体数组或者结构体指针的时候,就会发现C#上没有类型 ...
- Linux进程间通信——信号集函数
一.什么是信号 用过Windows的我们都知道,当我们无法正常结束一个程序时,可以用任务管理器强制结束这个进程,但这其实是怎么实现的呢?同样的功能在Linux上是通过生成信号和捕获信号来实现的,运行中 ...
- C Deepin指针
L-value -->标识了一个可以存储结果值的内存存储地址--指针变量 or 普通内存变量; R-value -->数值;
- Z.Studio高级成衣定制(双井店)价格,地址(图)-北京-大众点评网
Z.Studio高级成衣定制(双井店)价格,地址(图)-北京-大众点评网 Z.Studio高级成衣定制(双井店)
- linux C之access函数 (20
http://blog.sina.com.cn/s/blog_6a1837e90100uh5d.html linux C之access函数 (20access():判断是否具有存取文件的权限 相关函数 ...
- hdu 4393 Throw nails(STL之优先队列)
Problem Description The annual school bicycle contest started. ZL is a student in this school. He is ...
- nyoj 43 24 Point game(dfs暴力)
描述 There Point game. In .The expression mustn't have any other operator except plus,minus,multiply,d ...
- UVA 1614 - Hell on the Markets
题意: 输入n个数,第i个数ai满足1≤ai≤i.对每个数添加符号,使和值为0. 分析: 排序后从最大的元素(假设为k)开始,凑出sum/2即可.用去掉了k的集合,一定可以凑出sum/2 - a[k] ...
- OC——NSString的常用方法
NSString *str1 = @"BeiJing"; NSString *str2 = @"beijing"; //全部转为大写 NSLog(@" ...