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 ...
随机推荐
- python 前面几个单词含义
切片 str[start:end:step] start:从xxx开始 (startswith) end:切到xxx为止 (endswith) 不包括 字符串操作 .capitalize()# ...
- PHP中get请求中参数的key不能是para
¶会被转化成¶,然后就无法进行下去了. 仅作记录.
- Codeforces 1117G Recursive Queries [线段树]
Codeforces 洛谷:咕咕咕 思路 设\(L_i,R_i\)为\(i\)左右第一个大于它的位置. 对于每一个询问\(l,r\),考虑区间每一个位置的贡献就是\(\min(r,R_i-1)-\ma ...
- PL/SQL设置
PL/SQL 自定义快捷键(比如输入s,直接就显示select * from) 1.1 修改Code assistant快捷键tools->preferences->User Interf ...
- WebSocket异步通讯,实时返回数据实例
定义类中的异步方法 using System;using System.Collections.Generic;using System.IO;using System.Linq;using Syst ...
- Confluence 6 数据库连接方式
你可以使用 JDBC URL 或者一个 JNDI 数据源来连接 Confluence 到你的数据库. 在默认的设置向导中,只提供了使用 JDBC 数据库连接选项,这个也是推荐的数据库连接选项. 如果你 ...
- css固定表头,表单内容可以滑动
<html><head> <meta charset="utf-8"> <title>Table</title&g ...
- SpringBoot集成Shiro
Shiro是一个安全框架,控制登陆,角色权限管理(身份认证.授权.回话管理.加密) Shiro不会去维护用户,维护权限:这些需要通过realm让开发人员自己注入 1.在pom.xml中引入shiro的 ...
- Android开发实战一 百度SDK
一 申请百度API key 百度地图API:http://developer.baidu.com/map/ 百度地图开发者平台地址:http://developer.baidu.com/map/ .点 ...
- exgcd求解同余方程的最小正整数解 poj1061 poj2115
这两题都是求解同余方程,并要求出最小正整数解的 对于给定的Ax=B(mod C) 要求x的最小正整数解 首先这个式子可转化为 Ax+Cy=B,那么先用exgcd求出Ax+Cy=gcd(A,C)的解x ...