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 ...
随机推荐
- About GAC
http://blogs.msdn.com/b/msbuild/archive/2007/04/12/new-reference-assemblies-location.aspx http://web ...
- ****Objective-C 中的方法的调用
oc语言中采用特定的语言调用类或者实例(对象)的方法称为发送消息或者方法调用. oc中方法的调用有两种: 第一种: [类名或对象名 方法名]; [ClassOrInstance method]; [C ...
- POJ2402+模拟
题意:找出第index 个回文数. 这题是有规律的,即list[]数组. 其次,对于某个 index 可以先精确到 位数 pos,然后在进行分析. 1a #include<stdio.h> ...
- linux如何关闭防火墙
1) 重启后生效 开启: chkconfig iptables on 关闭: chkconfig iptables off 2) 即时生效,重启后失效 开启: service iptables sta ...
- Java:List,ArrayList和LinkList的区别
1.大学数据结构中ArrayList是实现了基于动态数组的数据结构,LinkList基于链表的数据结构 2.对于随机访问get和set,ArrayList优于LinkList,因为LinkedList ...
- plsql 书写命名规范
俗话说事不预则废,无规矩不成方圆. 对sql脚本程序的设计,个人认为应该是从编码规范开始. 前段时间公司一些同事提交的脚本,风格迥异,让我审核起来倍感难受,丝毫没有审核代码的快感. 特整理了公司部分常 ...
- Server.MapPath()目录详解
最近在做相关的开发,碰到了Server.MapPath(),顺便来温习一下 Server.MapPath()获取网站的目录详解 ./当前目录 /网站主目录 ../上层目录 ~/网站虚拟目录 如果当前 ...
- 【POJ】1054 The Troublesome Frog
题目是非常经典的搜索+剪枝.题意简言之就是,青蛙需要沿着直线踩着踏点通过田地,并且踏点需要至少为3.问哪条路径青蛙踩坏的作物最多.很好的一个条件是青蛙每次移动都是等间距的.题目需要注意将其排序. #i ...
- 【HDOJ】1720 A+B coming
水题. #include <stdio.h> #include <string.h> #define MAXNUM 1005 int stoi(char); int main( ...
- 深入理解c语言_从编译器的角度考虑问题_纪念Dennis Ritchie先生
开源中国: Dennis Ritchie教授过世了,他发明了C语言,一个影响深远并彻底改变世界的计算机语言.一门经历40多年的到今天还长盛不训的语言,今天很多语言都受到C的影 响,C++,Java,C ...