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. java新手笔记5 类

    1.进制转换 /* 企业发放的奖金根据利润提成. 利润(I)低于或等于10万元时,奖金可提10%: 利润高于10万元,低于20万元时, 低于10万元的部分按10%提成,高于10万元的部分,可提成7.5 ...

  2. OpenJudge 2749 分解因数

    1.链接地址: http://bailian.openjudge.cn/practice/2749/ 2.题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 给出一个正整数a,要求分 ...

  3. IOS 学习笔记 2015-03-24 OC-API-网络访问-案例一

    // // WPSuggest.h // OC-API-网络访问 // // Created by wangtouwang on 15/3/24. // Copyright (c) 2015年 wan ...

  4. centOS 多网卡 启动网络 eth0 does not to be present

    centOS 6.4 中 em1 就是eth0... ---------------------------------------- http://www.php-oa.com/2012/03/07 ...

  5. Fedora 17 修改GRUB启动菜单顺序

    Fedora 16采用GRUB2,因此启动菜单编辑方式与以前版本有所不同 设置默认启动Windows 1. 首先找到Windows的menuentry # cat   /boot/grub2/grub ...

  6. sql 自身连接

    "select table1.field1, table2.field1 from table table1, table table2 where table1.id=table2.par ...

  7. less学习-浏览器端编译(一)

    demo地址 http://www.qq210.com/shoutu/android 1.下载less包,官网 2.引入less文件 <link rel="stylesheet/les ...

  8. Vue.js 2.0 和 React、Augular

    Vue.js 2.0 和 React.Augular 引言 这个页面无疑是最难编写的,但也是非常重要的.或许你遇到了一些问题并且先前用其他的框架解决了.来这里的目的是看看Vue是否有更好的解决方案.那 ...

  9. uva 1400 - "Ray, Pass me the dishes!"

    又是一道线段树区间更新的题: #include<cstdio> #include<algorithm> #include<cstring> #define ll l ...

  10. uva 12206 - Stammering Aliens

    基于hash的LCP算法: #include<cstdio> #include<cstring> #include<algorithm> #define maxn ...