ZOJ --- 3516 Tree of Three
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的更多相关文章
- ZOJ 3201 Tree of Tree
树形DP.... Tree of Tree Time Limit: 1 Second Memory Limit: 32768 KB You're given a tree with weig ...
- 【HDU】3516 Tree Construction
http://acm.hdu.edu.cn/showproblem.php?pid=3516 题意:平面n个点且满足xi<xj, yi>yj, i<j.xi,yi均为整数.求一棵树边 ...
- HDOJ 3516 Tree Construction
四边形优化DP Tree Construction Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- HDOJ 3516 Tree Construction 四边形优化dp
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3516 题意: 大概就是给你个下凸包的左侧,然后让你用平行于坐标轴的线段构造一棵树,并且这棵树的总曼哈顿 ...
- HDU 3516 Tree Construction (四边形不等式)
题意:给定一些点(xi,yi)(xj,yj)满足:i<j,xi<xj,yi>yj.用下面的连起来,使得所有边的长度最小? 思路:考虑用区间表示,f[i][j]表示将i到j的点连起来的 ...
- HDU.3516.Tree Construction(DP 四边形不等式)
题目链接 贴个教程: 四边形不等式学习笔记 \(Description\) 给出平面上的\(n\)个点,满足\(X_i\)严格单增,\(Y_i\)严格单减.以\(x\)轴和\(y\)轴正方向作边,使这 ...
- HDU 3516 Tree Construction
区间$dp$,四边形优化. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio&g ...
- ZOJ - 3201 Tree of Tree (树形背包)
题意:有一棵树,树上每个结点都有一个权值,求恰好包含k个结点的子树的最大权值. 设dp[i][j]为以结点i为根的树中包含j个结点的子树的最大权值,则可以把这个结点下的每棵子树中所包含的所有子树的大小 ...
- 【转载】ACM总结——dp专辑
感谢博主—— http://blog.csdn.net/cc_again?viewmode=list ---------- Accagain 2014年5月15日 动态规划一 ...
随机推荐
- iOS lanchImage 和icon的设置
1 icon的设置 打开项目中的Assets.xcassets 这里边有一个icon 首先需要有icon 的尺寸 尺寸如下: 29*29 2x 29*29 3x 40*40 2x 40* ...
- iOS 图片按比例压缩,指定大小压缩
使用系统方法UIImageJPEGRepresentation(UIimage *image,CGFloat quality)进行图片质量压缩,暂且叫参数quality为压缩比吧,取值范围为0-1. ...
- tar 命令基本使用(加密)
本文讲述tar命令的基本使用,special: 使用tar命令对文件加密. 假定在当前目录下有一个文件夹/stuff. 1.将/stuff目录下的所有文件打包成为.tar 文件. $ tar -cvf ...
- JS数值输入控制
在html文本框录入数值时,可用如下方法进行控制判断. 整数:<input type="text" name="aaa" onkeypress=" ...
- == 和 equals()的区别
package com.liaojianya.chapter1; /** * This program demonstrates the difference between == and equal ...
- Poj 1511 Invitation Cards(spfa)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...
- cocos2d-x v3.0的window平台搭建和编译成andriod程序
首先添加这个地址到系统环境变量,path 然后打开CMD,输入如下语句 现在就可以创建一个新项目了 这样一个空的cocos2d-x v3.0的项目就创建好了 接下来编译andriod程序 先在系统环境 ...
- 【CODECHEF】【phollard rho + miller_rabin】The First Cube
All submissions for this problem are available. Read problems statements in Mandarin Chinese and Rus ...
- 安卓手机无法连接VPN的解决办法
这篇不能算是技术博客吧,但是在网上很难找到解决方案,至少我找了好久也没弄好.. 三种方案,因机而异,我就长话短说了: 一. "/system/xbin" 和 "/syst ...
- C++ 11 笔记 (五) : std::thread
这真是一个巨大的话题.我猜记录完善绝B需要一本书的容量. 所以..我只是略有了解,等以后用的深入了再慢慢补充吧. C++写多线程真是一个痛苦的事情,当初用过C语言的CreateThread,见过boo ...