Tree of Three


Time Limit: 2 Seconds      Memory Limit: 65536 KB

Now we have a tree and some queries to deal with. Every node in the tree has a value on it. For one node A, we want to know the largest three values in all the nodes of the subtree whose root is node A. Node 0 is root of the tree, except it, all other nodes have a parent node.

Input

There are several test cases. Each test case begins with a line contains one integer n(1 ≤ n ≤ 10000), which indicates the number of the node in the tree. The second line contains one integer v[0], the value on the root. Then for the following n - 1 lines(from the 3rd line to the (n + 1)th line), let i + 2 be the line number, then line i + 2contains two integers parent and v[i], here parent is node i's parent node, v[i] is the value on node i. Here 0 ≤ v[i] ≤ 1000000. Then the next line contains an integer m(1 ≤m ≤ 10000), which indicates the number of queries. Following m lines, each line contains one integer q, 0 ≤ q < n, it meas a query on node q.

Output

For each test case, output m lines, each line contains one or three integers. If the query asked for a node that has less than three nodes in the subtree, output a "-1"; otherwise, output the largest three values in the subtree, from larger to smaller.

Sample Input

5
1
0 10
0 5
2 7
2 8
5
0
1
2
3
4

Sample Output

10 8 7
-1
8 7 5
-1
-1

思路:深度优先搜索,一层一层由子节点向跟节点回溯。

1.

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#define MAX 11111
using namespace std;
int Three_Max[MAX][], val[MAX], cnt[MAX];
typedef struct{
int to, next;
}Node;
Node edge[MAX];
int head[MAX];
void AddEdge(int u, int v, int i){
edge[i].to = v;
edge[i].next = head[u];
head[u] = i;
}
bool cmp(int a, int b){
return a > b;
}
void dfs(int id){
cnt[id] = ;
Three_Max[id][] = val[id];
for(int i = head[id];i != -;i = edge[i].next){
int u = edge[i].to;
dfs(u);
for(int j = ;j <= ;j ++) Three_Max[id][j] = Three_Max[u][j-];
sort(Three_Max[id] + , Three_Max[id]+, cmp);
cnt[id] += cnt[u];
}
}
int main(){
int n, m, cc, u, v, k;
while(~scanf("%d", &n)){
memset(head, -, sizeof(head));
memset(Three_Max, , sizeof(Three_Max));
memset(cnt, , sizeof(cnt));
k = ;
for(int i = ;i < n;i ++){
if( == i){
scanf("%d", &cc);
val[] = cc;
}else{
scanf("%d%d", &u, &cc);
val[i] = cc;
AddEdge(u, i, k);
k ++;
}
}
dfs();
scanf("%d", &m);
for(int i = ;i < m;i ++){
scanf("%d", &u);
if(cnt[u] < ) printf("-1\n");
else{
for(int j = ;j < ;j ++) printf("%d ", Three_Max[u][j]);
printf("%d\n", Three_Max[u][]);
}
}
}
return ;
}

2.

 #include<iostream>
#include<climits>
#include<algorithm>
#include<cstring>
#include<cstdio>
#define MAX 11111
using namespace std;
int Three_Max[MAX][], val[MAX], cnt[MAX];
typedef struct{
int to, next;
}Node;
Node edge[MAX];
int head[MAX];
void AddEdge(int u, int v, int i){
edge[i].to = v;
edge[i].next = head[u];
head[u] = i;
}
bool cmp(int a, int b){
return a > b;
}
int *dfs(int id){
cnt[id] = ;
Three_Max[id][] = val[id];
for(int i = head[id];i != -;i = edge[i].next){
int u = edge[i].to;
int *temp = dfs(u);
for(int j = ;j <= ;j ++) Three_Max[id][j] = temp[j-];
sort(Three_Max[id] + , Three_Max[id]+, cmp);
cnt[id] += cnt[u];
}
return Three_Max[id];
}
int main(){
int n, m, cc, u, v, k;
while(~scanf("%d", &n)){
memset(head, -, sizeof(head));
memset(Three_Max, , sizeof(Three_Max));
memset(cnt, , sizeof(cnt));
k = ;
for(int i = ;i < n;i ++){
if( == i){
scanf("%d", &cc);
val[] = cc;
}else{
scanf("%d%d", &u, &cc);
val[i] = cc;
AddEdge(u, i, k);
k ++;
}
}
dfs();
scanf("%d", &m);
for(int i = ;i < m;i ++){
scanf("%d", &u);
if(cnt[u] < ) printf("-1\n");
else{
for(int j = ;j < ;j ++) printf("%d ", Three_Max[u][j]);
printf("%d\n", Three_Max[u][]);
}
}
}
return ;
}

ZOJ --- 3516 Tree of Three的更多相关文章

  1. ZOJ 3201 Tree of Tree

    树形DP.... Tree of Tree Time Limit: 1 Second      Memory Limit: 32768 KB You're given a tree with weig ...

  2. 【HDU】3516 Tree Construction

    http://acm.hdu.edu.cn/showproblem.php?pid=3516 题意:平面n个点且满足xi<xj, yi>yj, i<j.xi,yi均为整数.求一棵树边 ...

  3. HDOJ 3516 Tree Construction

    四边形优化DP Tree Construction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  4. HDOJ 3516 Tree Construction 四边形优化dp

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3516 题意: 大概就是给你个下凸包的左侧,然后让你用平行于坐标轴的线段构造一棵树,并且这棵树的总曼哈顿 ...

  5. HDU 3516 Tree Construction (四边形不等式)

    题意:给定一些点(xi,yi)(xj,yj)满足:i<j,xi<xj,yi>yj.用下面的连起来,使得所有边的长度最小? 思路:考虑用区间表示,f[i][j]表示将i到j的点连起来的 ...

  6. HDU.3516.Tree Construction(DP 四边形不等式)

    题目链接 贴个教程: 四边形不等式学习笔记 \(Description\) 给出平面上的\(n\)个点,满足\(X_i\)严格单增,\(Y_i\)严格单减.以\(x\)轴和\(y\)轴正方向作边,使这 ...

  7. HDU 3516 Tree Construction

    区间$dp$,四边形优化. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio&g ...

  8. ZOJ - 3201 Tree of Tree (树形背包)

    题意:有一棵树,树上每个结点都有一个权值,求恰好包含k个结点的子树的最大权值. 设dp[i][j]为以结点i为根的树中包含j个结点的子树的最大权值,则可以把这个结点下的每棵子树中所包含的所有子树的大小 ...

  9. 【转载】ACM总结——dp专辑

    感谢博主——      http://blog.csdn.net/cc_again?viewmode=list       ----------  Accagain  2014年5月15日 动态规划一 ...

随机推荐

  1. iOS 设置图片imageView圆角——对图片进行裁剪

    以前设置图片圆角总是把imageView设置成圆形,然后设置maskToBounds为YES,其实这样处理很消耗性能,图片多了之后比较卡,最好将图片进行裁剪后显示:这里有个分类可以用: UIImage ...

  2. c# 串口发送接收数据

    /********************** 串口数据接收事件 *****************************/ private void SerialPort_DataReceived ...

  3. js window.open 参数设置

    function OpenWin(type, obj){ window.open ("http://www.baidu.com" + type, "_blank" ...

  4. Ubuntu_14.04安装docker

    Ubuntu_14.04安装docker $ sudo apt-get update $ sudo apt-get install apt-transport-https ca-certificate ...

  5. 01_JavaMail_02_Base64加密

    [简述] Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一.Base64编码可用于在HTTP环境下传递较长的标识信息.例如,在Java Persistence系统Hibernate中 ...

  6. 专题二、ArrayList序列化技术细节详解

    一.绪论 所谓的JAVA序列化与反序列化,序列化就是将JAVA 对象以一种的形式保持,比如存放到硬盘,或是用于传输.反序列化是序列化的一个逆过程. JAVA规定被序列化的对象必须实现java.io.S ...

  7. 基于NodeJs的网页爬虫的构建(二)

    好久没写博客了,这段时间已经忙成狗,半年时间就这么没了,必须得做一下总结否则白忙.接下去可能会有一系列的总结,都是关于定向爬虫(干了好几个月后才知道这个名词)的构建方法,实现平台是Node.JS. 背 ...

  8. localstorage 使用

    localstorage作为HTML5的一个特殊属性,在发布时就备受关注:最近总结了其一些小的用法,希望可以抛砖引玉. 因HTML5本地存储只能存字符串,所以所有数据存储的话,都要转化成字符串:而js ...

  9. express cookie-session解惑

    express的中间件基于connect模块而来,所以相关文档可以直接参考 http://www.senchalabs.org/connect/ 使用cookie-session中间件过程中,比较困惑 ...

  10. python调用Moxa PCOMM Lite通过串口Ymodem协议发送文件

    本文采用python 2.7编写. 经过长期搜寻,终于找到了Moxa PCOMM Lite.调用PCOMM.DLL可以非常方便的通过串口的Xmodem.Ymodem.Zmodem等协议传输文件,而无需 ...