POJ1330Nearest Common Ancestors
题意
第一行输入T,有T组数据。
对于每组数据,给出一棵树,先输入n,然后n-1行,每行两个数a,b,表示a是b的父亲;第n行输入两个数A,B表示询问A和B的最近公共祖先。
题解
LCA模板题。
LCA倍增算法&POJ1330标程
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
const int N=+;
vector <int> son[N];
int T,n,depth[N],fa[N][],in[N],a,b;
void dfs(int prev,int rt){
depth[rt]=depth[prev]+;
fa[rt][]=prev;
for (int i=;(<<i)<=depth[rt];i++)
fa[rt][i]=fa[fa[rt][i-]][i-];
for (int i=;i<son[rt].size();i++)
dfs(rt,son[rt][i]);
}
int LCA(int a,int b){
if (depth[a]>depth[b])
swap(a,b);
for (int i=depth[b]-depth[a],j=;i>;i>>=,j++)
if (i&)
b=fa[b][j];
if (a==b)
return a;
int k;
for (k=;(<<k)<=depth[a];k++);
for (;k>=;k--)
if ((<<k)<=depth[a]&&fa[a][k]!=fa[b][k])
a=fa[a][k],b=fa[b][k];
return fa[a][];
}
int main(){
scanf("%d",&T);
while (T--){
scanf("%d",&n);
for (int i=;i<=n;i++)
son[i].clear();
memset(in,,sizeof in);
for (int i=;i<n;i++){
scanf("%d%d",&a,&b);
son[a].push_back(b);
in[b]++;
}
depth[]=-;
int rt=;
for (int i=;i<=n&&rt==;i++)
if (in[i]==)
rt=i;
dfs(,rt);
scanf("%d%d",&a,&b);
printf("%d\n",LCA(a,b));
}
return ;
}
POJ1330Nearest Common Ancestors的更多相关文章
- poj1330Nearest Common Ancestors 1470 Closest Common Ancestors(LCA算法)
LCA思想:http://www.cnblogs.com/hujunzheng/p/3945885.html 在求解最近公共祖先为问题上,用到的是Tarjan的思想,从根结点开始形成一棵深搜树,非常好 ...
- poj----1330Nearest Common Ancestors(简单LCA)
题目连接 http://poj.org/problem?id=1330 就是构建一棵树,然后问你两个节点之间最近的公共父节点是谁? 代码: /*Source Code Problem: 1330 U ...
- POJ1330Nearest Common Ancestors——近期公共祖先(离线Tarjan)
http://poj.org/problem? id=1330 给一个有根树,一个查询节点(u,v)的近期公共祖先 836K 16MS #include<iostream> #includ ...
- POJ-1330--Nearest Common Ancestors(离线LCA)
LCA离线算法 它需要一次输入所有的询问,然后有根节点开始进行深度优先遍历(DFS),在深度优先遍历的过程中,进行并查集的操作,同时查询询问,返回结果. 题意: 求A ,B两点的最近公共祖先 分析: ...
- poj1330Nearest Common Ancestors(LCA小结)
题目请戳这里 题目大意:意如其名. 题目分析:本题只有一个查询,所以可以各种乱搞过去. 不过对于菜鸟而言,还是老老实实练习一下LCA算法. LCA有很多经典的算法.按工作方式分在线和离线2种. tar ...
- POJ1330Nearest Common Ancestors最近公共祖先LCA问题
用的离线算法Tarjan 该算法的详细解释请戳 http://www.cnblogs.com/Findxiaoxun/p/3428516.html 做这个题的时候,直接把1470的代码copy过来,改 ...
- 【LCA倍增】POJ1330-Nearest Common Ancestors
[知识点:离线算法&在线算法] 一个离线算法,在开始时就需要知道问题的所有输入数据,而且在解决一个问题后就要立即输出结果. 一个在线算法是指它可以以序列化的方式一个个的处理输入,也就是说在开始 ...
- 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
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27316 Accept ...
随机推荐
- Ubuntu选择软件源
1. 系统桌面右上角,系统菜单中选择Software Up to Date 2. 点击Settings-按钮 3. Ubuntu Software标签页,Download from选择中国的软件服务器 ...
- Django ----- 模板2
tags for <ul> {% for user in user_list %} <li>{{ user.name }}</li> {% endfor %} #结 ...
- [转]PhpStorm快捷键大全
1 前言 PhPStorm 是 JetBrains 公司开发的一款商业的 PHP 集成开发工具,PhpStorm可随时帮助用户对其编码进行调整,运行单元测试或者提供可视化debug功能.Phpstro ...
- cocos2d内存管理,类的生命周期
下面资料来自<Cocos2d-x之Lua核心编程>
- linux学习之uniq
uniq最经常用的是统计次数,通常先排序,然后uniq -c cat a.txt |sort -nr |uniq -c
- STM32应用实例十四:利用光敏二极管实现光度测量
最近我们在开发臭氧发生器时,需要监测生成的臭氧的浓度,于是想到使用光度计来测量.因为不同浓度的臭氧对管的吸收作用是不相同的,于是检测光照强度的变化就可以得到相应的浓度数据. 1.硬件设计 此次光照度检 ...
- Struts2框架中使用Servlet的API示例
1. 在Action类中也可以获取到Servlet一些常用的API * 需求:提供JSP的表单页面的数据,在Action中使用Servlet的API接收到,然后保存到三个域对象中,最后再显示到JSP的 ...
- extjs中store的reload事件异步问题解决
转载自:http://blog.sina.com.cn/s/blog_8f8b7fc10100zd75.html store0.reload({params:{start:0, limit:10}}) ...
- C++ GetComputerName()
关于函数“GetComputerName()”,参考:https://msdn.microsoft.com/en-us/library/windows/desktop/ms724295(v=vs.85 ...
- C# 不使用递归遍历目录树中的文件和文件夹
public class StackBasedIteration { static void Main(string[] args) { // Specify the starting folder ...