Nearest Common Ancestors(LCA板子)
题目链接:http://poj.org/problem?id=1330
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 36918 | Accepted: 18495 |
Description
In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is the root of the tree. Node x is an ancestor of node y if node x is in the path between the root and node y. For example, node 4 is an ancestor of node 16. Node 10 is also an ancestor of node 16. As a matter of fact, nodes 8, 4, 10, and 16 are the ancestors of node 16. Remember that a node is an ancestor of itself. Nodes 8, 4, 6, and 7 are the ancestors of node 7. A node x is called a common ancestor of two different nodes y and z if node x is an ancestor of node y and an ancestor of node z. Thus, nodes 8 and 4 are the common ancestors of nodes 16 and 7. A node x is called the nearest common ancestor of nodes y and z if x is a common ancestor of y and z and nearest to y and z among their common ancestors. Hence, the nearest common ancestor of nodes 16 and 7 is node 4. Node 4 is nearer to nodes 16 and 7 than node 8 is.
For other examples, the nearest common ancestor of nodes 2 and 3 is node 10, the nearest common ancestor of nodes 6 and 13 is node 8, and the nearest common ancestor of nodes 4 and 12 is node 4. In the last example, if y is an ancestor of z, then the nearest common ancestor of y and z is y.
Write a program that finds the nearest common ancestor of two distinct nodes in a tree.
Input
Output
Sample Input
2
16
1 14
8 5
10 16
5 9
4 6
8 4
4 10
1 13
6 15
10 11
6 7
10 2
16 3
8 1
16 12
16 7
5
2 3
3 4
3 1
1 5
3 5
Sample Output
4
3
Source
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
const int maxn=1e4+;
int N;//节点个数
vector<int>v[maxn];//树
vector<int> query[maxn];
int indeg[maxn];//节点的入度
int fa[maxn],deep[maxn],ancestor[maxn];//父亲 深度 祖先
bool vis[maxn];//是否被检查过
int root;
void Init()
{
for(int i=;i<=N;i++)
{
v[i].clear();
query[i].clear();
indeg[i]=; }
return ;
}
void add_edge(int x,int y)
{
v[x].push_back(y);
indeg[y]++;
return ;
}
void Input_query()
{
int u,v;
scanf("%d%d",&u,&v);
query[u].push_back(v);//注意 两个都要存
query[v].push_back(u);
return ;
}
void Init_set()
{
for(int i=;i<=N;i++)
{
fa[i]=i;
ancestor[i]=i;
deep[i]=;
}
return ;
}
int Find(int x)
{
return fa[x]==x?x:fa[x]=Find(fa[x]);
}
void Union(int u,int v)
{
int du=Find(u);
int dv=Find(v);
if(du>dv)
{
fa[dv]=du;
return ;
}
else
{
fa[du]=dv;
if(deep[du]==deep[dv]) deep[dv]++;
}
return ;
}
void Tarjan(int p)
{
for(int i=;i<v[p].size();i++)//遍历子树
{
Tarjan(v[p][i]);
Union(p,v[p][i]);
ancestor[Find(p)]=p;
}
vis[p]=true;
for(int i=;i<query[p].size();i++)
{
if(vis[query[p][i]])
{
printf("%d\n",ancestor[Find(query[p][i])]);
}
}
return ;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{ scanf("%d",&N);
Init();
for(int i=;i<N;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add_edge(u,v);
}
for(int i=;i<=N;i++)
{
if(indeg[i]==)
{
root=i;
break;
}
}
Input_query();
Init_set();
memset(vis,false,sizeof(vis));
Tarjan(root);
}
return ;
}
Nearest Common Ancestors(LCA板子)的更多相关文章
- POJ.1330 Nearest Common Ancestors (LCA 倍增)
POJ.1330 Nearest Common Ancestors (LCA 倍增) 题意分析 给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b.接下来给出xy,求出xy的lca节 ...
- POJ 1330 Nearest Common Ancestors LCA题解
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19728 Accept ...
- pku 1330 Nearest Common Ancestors LCA离线
pku 1330 Nearest Common Ancestors 题目链接: http://poj.org/problem?id=1330 题目大意: 给定一棵树的边关系,注意是有向边,因为这个WA ...
- poj 1330 Nearest Common Ancestors lca 在线rmq
Nearest Common Ancestors Description A rooted tree is a well-known data structure in computer scienc ...
- poj 1330 Nearest Common Ancestors LCA
题目链接:http://poj.org/problem?id=1330 A rooted tree is a well-known data structure in computer science ...
- Nearest Common Ancestors(LCA)
Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...
- [POJ1330]Nearest Common Ancestors(LCA, 离线tarjan)
题目链接:http://poj.org/problem?id=1330 题意就是求一组最近公共祖先,昨晚学了离线tarjan,今天来实现一下. 个人感觉tarjan算法是利用了dfs序和节点深度的关系 ...
- POJ 1330 Nearest Common Ancestors(LCA模板)
给定一棵树求任意两个节点的公共祖先 tarjan离线求LCA思想是,先把所有的查询保存起来,然后dfs一遍树的时候在判断.如果当前节点是要求的两个节点当中的一个,那么再判断另外一个是否已经访问过,如果 ...
- POJ 1330 Nearest Common Ancestors (LCA,倍增算法,在线算法)
/* *********************************************** Author :kuangbin Created Time :2013-9-5 9:45:17 F ...
随机推荐
- unittest测试框架详谈及实操(五)
测试报告——生成HTML格式的测试报告 前面的实例输出的所有测试结果都是以命令行日志的方式展示,不止于难看,但也不适合直接把那样的测试结果截图发给相关人员,尤其是领导.这时需要更加友好的测试结果,既能 ...
- Java内存模型(转载)
本文章节: 1.JMM简介 2.堆和栈 3.本机内存 4.防止内存泄漏 1.JMM简介 i.内存模型概述 Java平台自动集成了线程以及多处理器技术,这种集成程度比Java以前诞生的计算机语言要厉害很 ...
- Jenkins构建完成之后运行脚本可以杀掉TomCat但是起不来的解决方法
Jenkins构建完成之后运行脚本可以杀掉TomCat但是起不来的解决方法 写了一个重启tomcat的脚本,让jenkins编译.打包.发布时调用.在本地写好重启tomcat的脚本后,本地执行脚本没有 ...
- C#中使用Redis学习一 windows安装redis服务器端和客户端
学习背景 今天是2015年1月2日,新年刚开始的第二天,先祝大家元旦快乐啦(迟到的祝福吧^_^).前段时间一直写Jquery插件开发系列博文,这个系列文章暂停一段时间,最近一直在看redis,我将把r ...
- asp.net——地址栏传递中文参数乱码解决方案
地址栏传递中文参数乱码解决方案: 很多人在使用地址栏传递参数的时候都会遇到一个麻烦的问题(参数为中文时乱码了),那要怎么解决呢? 其实解决这个问题也不怎么难,无非就是给要传递的中文参数一个编码解码的过 ...
- Windows上编译libpng
下载libpng 1.5.10并解压到[工作目录]/png/libpng-1.5.10 用CMake选择png/libpng-1.5.10目录并Configure: CMAKE_C_FLAGS_DEB ...
- webapi发布常见错误及解决方案
webapi发布常见错误及解决方案 错误一: 错误:404 (Not Found) 解决方案: 在 <system.webServer>节点中添加如下模块: <modules ru ...
- 「HNOI 2013」游走
题目链接 戳我 \(Solution\) 首先申明几个变量: f[x]:到点x的概率, vis[x]:x点的度 dp[x][y]:(x,y)这条边的概率 number[x][y]:x这条边的编号 下面 ...
- 【洛谷九月月赛T1】签到题(bsgs)(快速乘)
说好的签到题呢qwq....怎么我签到题都不会啊qwq 之后看了bsgs才发现貌似不是那么那么难fake!!什么东西... 先贴上部分分做法(也就是枚举1的个数,然后每一步都进行取模(这和最后取模结果 ...
- Django-05模型层之单表操作1
7.1 ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开 ...