【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 ...
随机推荐
- 零基础入门学习Python(20)--函数:内嵌函数和闭包
知识点 global关键字 使用global关键字,可以修改全局变量: >>> count = 5 >>> def Myfun(): count = 10 prin ...
- 树莓派 -- oled 续(2) python
上文中的代码通过wiringPi的API调用devfs API来显示图片. 这里分析的Python代码也通过类似的方法来显示图片. 主要用到了两个Library. import spidev impo ...
- lucene-5.3.1配置(win7x64)
lucene下载地址:http://www.us.apache.org/dist/lucene/java/5.3.1/lucene-5.3.1.zip 下载之后解压 控制台应用程序下配置: 找到luc ...
- 10.Spring Bean的生命周期
Spring IOC容器可以管理Bean的生命周期,允许在Bean声明周期的特定点执行定制的任务. Spring IOC容器对Bean的生命周期进行管理的过程. 1.通过构造器或工厂方法创建Bean实 ...
- 数据结构代码实现之队列的链表实现(C/C++)
上班闲着无聊,一直想着要开始写博客,但又不知道写什么.最近又回顾了下数据结构的知识,那就从数据结构开始吧. 前言 关于C语言结构体的知识以及队列的特性请读者自行了解,此处不做过多解释,嘻嘻. 同时此篇 ...
- MyBaties异常之 ORA-00918: 未明确定义列
原因: 如果a表与b表连接,且a与b中存在两个相同的字段,则必须指明字段是哪个表的 箭头所致位置没有指定ROOM_ID为那个表的,应修改为t1.ROOM_ID
- SQL Server 2016 CTP3.2 开荒 Reporting Service 篇
仅仅是开荒资源页,反正过不了多久就会有新的CTP. 下面是MSDN I Tell you 提供的 不过是中文,个人不是很建议,因为现在大多的资源页都是英文的ed2k://|file|cn_sql_se ...
- Python接口测试之对MySQL的操作(六)
本文章主要来说python对mysql数据库的基本操作,当然,前提是已经搭建了python环境和搭建了Mysql 数据库的环境,python操作mysql数据库提供了MySQLdb库,下载的地址为: ...
- Leetcode 214.最短回文串
最短回文串 给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串. 示例 1: 输入: "aacecaaa" 输出: &qu ...
- Spring & Java
Spring & Java https://spring.io/ Spring Boot https://www.shiyanlou.com/courses/1152 Spring Boot入 ...