ZOJ3516 (图的遍历)
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 + 2 contains 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 建图方法及遍历:链式齐向前星。
http://blog.csdn.net/acdreamers/article/details/16902023(链式向前星)
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdlib>
using namespace std;
int num,k,pa;
#define maxn 32010
int val[maxn];
int head[maxn];
int cnt[maxn];
struct Edge
{
int u;
int v;
int next;
};
Edge edge[maxn];
void addedge(int u,int v)//链式前向星模板
{
edge[num].u=u;
edge[num].v=v;
edge[num].next=head[u];
head[u]=num++;
}
void dfs(int p)
{
for(int i=head[p];i!=-;i=edge[i].next)
{
int v=edge[i].v;
cnt[k++]=val[v];
dfs(v);
}
}
int main()
{
int n,root;
while(~scanf("%d",&n))
{
memset(head,-,sizeof(head));
num=;
scanf("%d",&val[]);
for(int i=;i<n;i++)
{
scanf("%d%d",&pa,&val[i]);
addedge(pa,i);
}
int m,pos;
scanf("%d",&m);
for(int i=;i<m;i++)
{
k=;
memset(cnt,,sizeof(cnt));
scanf("%d",&pos);
cnt[k++]=val[pos];
dfs(pos);
if(k<)
printf("-1\n");
else
{
sort(cnt,cnt+k);
reverse(cnt,cnt+k);
printf("%d %d %d\n",cnt[],cnt[],cnt[]);
}
}
} return ;
}
ZOJ3516 (图的遍历)的更多相关文章
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- C++编程练习(9)----“图的存储结构以及图的遍历“(邻接矩阵、深度优先遍历、广度优先遍历)
图的存储结构 1)邻接矩阵 用两个数组来表示图,一个一维数组存储图中顶点信息,一个二维数组(邻接矩阵)存储图中边或弧的信息. 2)邻接表 3)十字链表 4)邻接多重表 5)边集数组 本文只用代码实现用 ...
- Kruskal和prime算法的类实现,图的遍历BFS算法。
一.图的遍历 #include<iostream> #include<queue> #include<vector> using namespace std; in ...
- 图的遍历——DFS(矩形空间)
首先,这里的图不是指的我们一般所说的图结构,而是大小为M*N的矩形区域(也可以看成是一个矩阵).而关于矩形区域的遍历问题经常出现,如“寻找矩阵中的路径”.“找到矩形区域的某个特殊点”等等之类的题目,在 ...
- 图的遍历——DFS和BFS模板(一般的图)
关于图的遍历,通常有深度优先搜索(DFS)和广度优先搜索(BFS),本文结合一般的图结构(邻接矩阵和邻接表),给出两种遍历算法的模板 1.深度优先搜索(DFS) #include<iostrea ...
- 图的遍历算法:DFS、BFS
在图的基本算法中,最初需要接触的就是图的遍历算法,根据访问节点的顺序,可分为深度优先搜索(DFS)和广度优先搜索(BFS). DFS(深度优先搜索)算法 Depth-First-Search 深度优先 ...
- 15 图-图的遍历-基于邻接矩阵实现的BFS与DFS算法
算法分析和具体步骤解说直接写在代码注释上了 TvT 没时间了等下还要去洗衣服 就先不赘述了 有不明白的欢迎留言交流!(估计是没人看的了) 直接上代码: #include<stdio.h> ...
- python 回溯法 子集树模板 系列 —— 8、图的遍历
问题 一个图: A --> B A --> C B --> C B --> D B --> E C --> A C --> D D --> C E -- ...
- [图的遍历&多标准] 1087. All Roads Lead to Rome (30)
1087. All Roads Lead to Rome (30) Indeed there are many different tourist routes from our city to Ro ...
随机推荐
- 【HDU1754】I Hate It(线段树)
update:单点替换 query:区间最值 #include <iostream> #include <cstring> #include <cstdlib> # ...
- linux使用共享内存通信的进程同步退出问题
两个甚至多个进程使用共享内存(shm)通信,总遇到同步问题.这里的“同步问题”不是说进程读写同步问题,这个用信号量就好了.这里的同步问题说的是同步退出问题,到底谁先退出,怎么知道对方退出了.举个例子: ...
- 关系型数据库遵循ACID规则
事务在英文中是transaction,和现实世界中的交易很类似,它有如下四个特性: 1.A (Atomicity) 原子性原子性很容易理解,也就是说事务里的所有操作要么全部做完,要么都不做,事务成功的 ...
- 用CSS/CSS3 实现 水平居中和垂直居中的完整攻略
水平居中:行内元素解决方案 只需要把行内元素包裹在一个属性display为block的父层元素中,并且把父层元素添加如下属性即可: .parent { text-align:center; } 水 ...
- VirtualBox 扩展包卸载或安装失败(VERR_ALREADY_EXISTS)
最近在卸载VirtualBox出现了无法卸载的错误.提示为Failed to install the extension. The installer failed with exit code 1: ...
- 开源 免费 java CMS - FreeCMS2.0 会员我的评论
项目地址:http://www.freeteam.cn/ 我的评论 从左側管理菜单点击我的评论进入. 在这里能够查看当前登录会员的全部评论记录. 删除评论 选择评论然后点击删除button能够完毕删除 ...
- LDA-线性判别分析(二)
本来是要调研 Latent Dirichlet Allocation 的那个 LDA 的, 没想到查到很多关于 Linear Discriminant Analysis 这个 LDA 的资料.初步看了 ...
- LR脚本自定义显示Controller虚拟用户状态
在场景监控的过程中,想知道场景运行时Vusers的运行状态以及每一个Vuser虚拟用户在本次场景运行的过程共迭代了多少次,那么就需要在VuGen脚本中自定义显示虚拟用户状态信息. 代码如下: stat ...
- 执行update操作的话,就会报“Connection is read-only. Queries leading to data modification are not allowed”的异常。
我用的是 spring + springmvc + mybatis +mysql. <tx:advice id="txAdvice" transaction-manager= ...
- jquery列表动画
//新闻导航 (function (){ //获取分类名称 var _text = $('.news .news-wrapper .news-left .news-left-title .positi ...