POJ 1330 Nearest Common Ancestors(Tree)
根据输入建立树,然后求2个结点的最近共同祖先。
注意几点:
(1)记录每个结点的父亲,比较层级时要用;
(2)记录层级;
(3)记录每个结点的孩子,vector<int> v[M]写在主函数里面,放在外面超时。
代码:
#include<iostream>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std; const int M = ;
//vector<int> v[M];
int level[M];
int father[M]; void visitEveryone(vector<int> v[], int one, int _level)
{
level[one] = _level;
for(vector<int>::iterator it = v[one].begin(); it != v[one].end(); ++it)
{
visitEveryone(v, *it, _level+);
}
} int main()
{
int T,N,a,b,i,j;
scanf("%d", &T);
while(T--)
{
vector<int> v[M];
scanf("%d", &N);
memset(father, , (N+)*sizeof(int));
for(i = ; i < N; ++i)
{
scanf("%d %d", &a, &b);
father[b] = a;
v[a].push_back(b);
}
scanf("%d %d", &a, &b);
for(j=; father[j] > && j <= N; ++j); visitEveryone(v, j, ); while(a != b)
{ if(level[a] < level[b])
b = father[b];
else
a = father[a];
} printf("%d\n",a);
}
return ;
}
POJ 1330 Nearest Common Ancestors(Tree)的更多相关文章
- POJ 1330 Nearest Common Ancestors(lca)
POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...
- POJ - 1330 Nearest Common Ancestors(基础LCA)
POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %l ...
- poj 1330 Nearest Common Ancestors(LCA 基于二分搜索+st&rmq的LCA)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 30147 Accept ...
- POJ 1330 Nearest Common Ancestors (LCA,dfs+ST在线算法)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14902 Accept ...
- POJ 1330 Nearest Common Ancestors(Targin求LCA)
传送门 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26612 Ac ...
- POJ 1330 Nearest Common Ancestors(裸LCA)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39596 Accept ...
- poj 1330 Nearest Common Ancestors(LCA:最近公共祖先)
多校第七场考了一道lca,那么就挑一道水题学习一下吧= = 最简单暴力的方法:建好树后,输入询问的点u,v,先把u全部的祖先标记掉,然后沿着v->rt(根)的顺序检查,第一个被u标记的点即为u, ...
- POJ - 1330 Nearest Common Ancestors(dfs+ST在线算法|LCA倍增法)
1.输入树中的节点数N,输入树中的N-1条边.最后输入2个点,输出它们的最近公共祖先. 2.裸的最近公共祖先. 3. dfs+ST在线算法: /* LCA(POJ 1330) 在线算法 DFS+ST ...
- POJ 1330 Nearest Common Ancestors (dfs+ST在线算法)
详细讲解见:https://blog.csdn.net/liangzhaoyang1/article/details/52549822 zz:https://www.cnblogs.com/kuang ...
随机推荐
- 【Firefly API 新版文档】Package dbentrust
原地址:http://blog.csdn.net/uxqclm/article/details/11969653 该包下面主要是数据库的处理与memcached存储.里面封装了,从memcached数 ...
- 对CURL的一些研究
http://www.kuqin.com/article/23candcplusplus/586014.html 前两天看到有人求客户端socket 发HTTP包的代码,受flw版主启发找了一些per ...
- 图像色彩空间YUV和RGB的差别
http://blog.csdn.net/scg881008/article/details/7168637 假如是200万像素的sensor,是不是RGB一个pixel是2M,YUV是1M? 首先, ...
- 9. MonoBehaviour.StartCoroutine 开始协同程序
function StartCoroutine (routine : IEnumerator) : Coroutine 描述:开始协同程序. 一个协同程序在执行过程中,可以在任意位置使用yield语句 ...
- ANDROID_MARS学习笔记_S02_010_Animation_动画效果
一.流程 1.把要实现动画的一系列图片复制到res/drawable文件夹 2.在此文件新建一个xml文件用来组织图片 3.在mainactivity中用imageView.setBackground ...
- laravel code bright
Project RootLet’s start by taking a look at the root folder structure.• app/• bootstrap/• vendor/• p ...
- 常用的SQL分页算法及对比
SQL Server 2005引入的新方法. SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY keyField DESC) AS rowNum, * ...
- ActionBar官方教程(7)自定义操作项的view,如何得到它及处理它的事件
Adding an Action View An action view is a widget that appears in the action bar as a substitute for ...
- C#中的异常处理
C#中的异常处理 while (ex != null) { WriteExceptionLog(ex, fileName); ex = ex.InnerException; } /// <sum ...
- BZOJ_1084_[SCOI2005]_最大子矩阵_(动态规划)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1084 给出一个n*m的矩阵,其中m<=2,取k个子矩阵,求最大子矩阵和. 分析 1.m= ...