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 ...
随机推荐
- Java通过JNI调用C/C++
From:http://www.cnblogs.com/mandroid/archive/2011/06/15/2081093.html JNI是JAVA标准平台中的一个重要功能,它弥补了JAVA的与 ...
- 基于Android的物理类游戏,源代码(JAVA)分享
游戏视频DEMO:http://v.youku.com/v_show/id_XNTM5MzM1Mzg0.html?from=s1.8-1-1.2 说明:一个自己做的Android上的物理类游戏,物理引 ...
- poj 2288 Islands and Bridges_状态压缩dp_哈密尔顿回路问题
题目链接 题目描述:哈密尔顿路问题.n个点,每一个点有权值,设哈密尔顿路为 C1C2...Cn,Ci的权值为Vi,一条哈密尔顿路的值分为三部分计算: 1.每一个点的权值之和 2.对于图中的每一条CiC ...
- qt model/view 架构基础介绍之QTreeWidget
# -*- coding: utf-8 -*- # python:2.x #说明:QTreeWidget用于展示树型结构,也就是层次结构同前面说的 QListWidget 类似,这个类需要同另外一个辅 ...
- FZU 2102 Solve equation(水,进制转化)&& FZU 2111(贪心,交换使数字最小)
C Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Pra ...
- [Python学习笔记][Python内置函数]
Python 常用内建函数 比较基础的列表 abs(x) 求绝对值 pow(x,y) 返回x的y次方,等同于x**y round(x[,小数位数]) 对x进行四舍五入,若不指定位数,则返回整数 chr ...
- SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问的解决方案
今天写了一个excel表的导入功能,结果在excel表中的内容导入到页面时报错:SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT ...
- FlexSlider是一个非常出色的jQuery滑动切换插件
FlexSlider是一个非常出色的jQuery滑动切换插件,它支持所有主流浏览器,并有淡入淡出效果.适合所有初级和高级网页设计师使用.不过很多人都只是使用默认的参数,今天来说说具体的参数来给大家看看 ...
- c++设计模式之观察者模式
概念:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新. 通常讲就是被观察者向左右观察对象通知其状态的改变,以使得观察者进行相应信息的更新. 代码 ...
- imadjust函数分析一
声明:本文涉及到的行数皆指本文提供的附件imadjust.m的代码中行数 本文只讨论imadjust函数是一种用法,即 J = IMADJUST(I,[LOW_IN; HIGH_IN],[LOW_OU ...