【POJ 1330】 Nearest Common Ancestors
【题目链接】
【算法】
倍增法求最近公共祖先
【代码】
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 10010
#define MAXLOG 20 struct Edge
{
int to,nxt;
} e[MAXN]; int i,T,tot,n,root,x,y;
int fa[MAXN],dep[MAXN],head[MAXN],anc[MAXN][MAXLOG]; template <typename T> inline void read(T &x)
{
int f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
template <typename T> inline void write(T x)
{
if (x < )
{
putchar('-');
x = -x;
}
if (x > ) write(x/);
putchar(x%+'');
}
template <typename T> inline void writeln(T x)
{
write(x);
puts("");
}
inline void add(int x,int y)
{
tot++;
e[tot] = (Edge){y,head[x]};
head[x] = tot;
}
inline void lca_init(int u)
{
int i,v;
anc[u][] = fa[u];
for (i = ; i < MAXLOG; i++)
{
if (dep[u] <= (i << )) break;
anc[u][i] = anc[anc[u][i-]][i-];
}
for (i = head[u]; i; i = e[i].nxt)
{
v = e[i].to;
dep[v] = dep[u] + ;
lca_init(v);
}
}
inline int lca(int u,int v)
{
int i,k;
if (dep[u] > dep[v]) swap(u,v);
k = dep[v] - dep[u];
for (i = ; i < MAXLOG; i++)
{
if (k & ( << i))
v = anc[v][i];
}
if (u == v) return u;
for (i = MAXLOG - ; i >= ; i--)
{
if (anc[u][i] != anc[v][i])
{
u = anc[u][i];
v = anc[v][i];
}
}
return fa[u];
} int main() { read(T);
while (T--)
{
memset(fa,,sizeof(fa));
memset(anc,,sizeof(anc));
read(n);
tot = ;
for (i = ; i <= n; i++) head[i] = ;
for (i = ; i < n; i++)
{
read(x); read(y);
add(x,y);
fa[y] = x;
}
for (i = ; i <= n; i++)
{
if (!fa[i])
root = i;
}
dep[root] = ;
lca_init(root);
read(x); read(y);
writeln(lca(x,y));
}
return ; }
【POJ 1330】 Nearest Common Ancestors的更多相关文章
- 【51.64%】【POJ 1330】Nearest Common Ancestors
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26416 Accepted: 13641 Description A roote ...
- 【Poj 1330】Nearest Common Ancestors
http://poj.org/problem?id=1330 题目意思就是T组树求两点LCA. 这个可以离线DFS(Tarjan)-----具体参考 O(Tn) 0ms 还有其他在线O(Tnlogn) ...
- POJ 1330:Nearest Common Ancestors
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20940 Accept ...
- 【POJ 1470】 Closest Common Ancestors
[题目链接] 点击打开链接 [算法] 离线tarjan求最近公共祖先 [代码] #include <algorithm> #include <bitset> #include ...
- 【POJ1330】Nearest Common Ancestors(树链剖分求LCA)
Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...
- 【LCA/tarjan】POJ1470-Closest Common Ancestors
[题意] 给出一棵树和多组查询,求以每个节点为LCA的查询数有多少? [错误点] ①读入的时候,注意它的空格是随意的呀!一开始不知道怎么弄,后来看了DISCUSS区大神的话: 询问部分输入: scan ...
- 【LCA倍增】POJ1330-Nearest Common Ancestors
[知识点:离线算法&在线算法] 一个离线算法,在开始时就需要知道问题的所有输入数据,而且在解决一个问题后就要立即输出结果. 一个在线算法是指它可以以序列化的方式一个个的处理输入,也就是说在开始 ...
- POJ 1330 Nearest Common Ancestors 【LCA模板题】
任意门:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000 ...
- 【POJ】1330 Nearest Common Ancestors ——最近公共祖先(LCA)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18136 Accept ...
随机推荐
- python3.x Day6 多线程
线程???进程????区别???何时使用??? 进程:是程序以一个整体的形式暴露给操作系统管理,里边包含了对各种资源的调用,内存的使用,对各种资源的管理的集合,这就叫进程 线程:是操作系统最小的调度单 ...
- Win2008 Server MySql安装包详细安装教程
首先去官网下载 下载MySql 下载地址:http://downloads.mysql.com/archives/community/ 我这里选择MSI的32位安装包安装,服务器系统32位的. 安装M ...
- [学习资料] Tiny210(S5PV210) u-boot移植
Tiny210(S5PV210) u-boot移植http://www.microoh.com/bbs/forum.php?mod=viewthread&tid=254&fromuid ...
- 一次偶遇 IOException
今天遇到了个挺有意思的事情,为了实现上传头像的功能,这是需要实现的功能,项目在本地跑起来且在本地 服务器(tomcat)也实现了头像上传,并把头像路径存入数据库,但是却在上传到线上服务器的时候死活存不 ...
- C51 动态数码管 个人笔记
8段led管构成一个数字. 开发板上共有8个数字. 每个数字有一个使能端(段选引脚) 每个数字的位选端(选择8段led管哪些亮,即构成什么图案)并联在一起 轮流点亮不同数字,速度很快,视觉暂留,从而形 ...
- xtu Shortest Path
Acceteped : 23 Submit : 61 Time Limit : 5000 MS Memory Limit : 65536 KB Description 题目描述 N(3≤N≤1 ...
- HDU 1016 素数环问题
题目大意: 给定1-n这n个数,组成以1开头的素数环,保证相邻两个数相加均为素数 题目用dfs搜索再回溯,这样碰到不成立的立刻退出递归,就减少了很多步骤,不然暴力来就是n!次复杂度,肯定是超时的 每次 ...
- 食物(bzoj 3280)
Description 明明这次又要出去旅游了,和上次不同的是,他这次要去宇宙探险! 我们暂且不讨论他有多么NC,他又幻想了他应该带一些什么东西.理所当然的,你当然要帮他计算携带N件物品的方案数. 他 ...
- mysql 之 Workbench的使用
mysql 之 Workbench的使用 (1)简介 MySQL Workbench是一款专为MySQL设计的ER/数据库建模工具.它是著名的数据库设计工具DBDesigner4的继任者.你可以用My ...
- Spring中通过java的@Valid注解和@ControllerAdvice实现全局异常处理。
通过java原生的@Valid注解和spring的@ControllerAdvice和@ExceptionHandler实现全局异常处理的方法: controller中加入@Valid注解: @Req ...