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 ...
随机推荐
- linux下如何安装rzsz
1, 下载包: wget http://freeware.sgi.com/source/rzsz/rzsz-3.48.tar.gz 2, 解压缩:tar -zxvf rzsz-3.48.tar.gz ...
- asp.net DropDownList无刷新ajax二级联动实现详细过程
只适合新手制作DropDownList无刷新ajax二级联动效果: 数据库实现,添加两表如图:表1,pingpai,表2,type,具体数据库实现看自己的理解: //页面主要代码: <asp:S ...
- 关于shell环境变量的思考
shell中执行sh文件时需要开子进程,因此export的环境变量在子进程中,若想要sh中定义的脚本生效,使用source命令,并且可以被子进程继承.而子进程export的内容父进程获取不到
- 函数lock_rec_find_similar_on_page
/*********************************************************************//** Looks for a suitable type ...
- 常用的Web服务器
常用的Web服务器有IIS.Apache.Tomcat.Jboss.Resin.Weblogic.WebSpher IISIIS服务是Windows产品自带的一种免费的Web服务器,安装配置简单,主要 ...
- c语言字符串库函数#include<string.h>
字符串函数<string.h> 在头文件<string.h>中定义了两组字符串函数.第一组函数的名字以str开头:第二组函数的名字以mem开头.只有函数memmove对重叠对象 ...
- android 欢迎界面的淡入效果
package com.example.spinnertest; import android.app.Activity; import android.content.Intent; import ...
- 转 Xcode磁盘空间大清理
http://www.iwangke.me/2013/09/09/clean-xcode-to-free-up-disk-space/#jtss-tsina 我的设备是Macbook Air 13’ ...
- windows查看服务端口
开始--运行--cmd 进入命令提示符 输入netstat -ano 即可看到所有连接的PID 之后在任务管理器中找到这个PID所对应的程序如果任务管理器中没有PID这一项,可以在任务管理器中选&qu ...
- IIS里面网站停止了,不能启动
IIS里面网站文件夹显示红色的叉叉,停止了,不能启动,所有站点都停止了: 原来是Word wide web publish service 服务停止了,启动就好了