【题目链接】

点击打开链接

【算法】

倍增法求最近公共祖先

【代码】

#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的更多相关文章

  1. 【51.64%】【POJ 1330】Nearest Common Ancestors

    Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26416 Accepted: 13641 Description A roote ...

  2. 【Poj 1330】Nearest Common Ancestors

    http://poj.org/problem?id=1330 题目意思就是T组树求两点LCA. 这个可以离线DFS(Tarjan)-----具体参考 O(Tn) 0ms 还有其他在线O(Tnlogn) ...

  3. POJ 1330:Nearest Common Ancestors

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20940   Accept ...

  4. 【POJ 1470】 Closest Common Ancestors

    [题目链接] 点击打开链接 [算法] 离线tarjan求最近公共祖先 [代码] #include <algorithm> #include <bitset> #include ...

  5. 【POJ1330】Nearest Common Ancestors(树链剖分求LCA)

    Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...

  6. 【LCA/tarjan】POJ1470-Closest Common Ancestors

    [题意] 给出一棵树和多组查询,求以每个节点为LCA的查询数有多少? [错误点] ①读入的时候,注意它的空格是随意的呀!一开始不知道怎么弄,后来看了DISCUSS区大神的话: 询问部分输入: scan ...

  7. 【LCA倍增】POJ1330-Nearest Common Ancestors

    [知识点:离线算法&在线算法] 一个离线算法,在开始时就需要知道问题的所有输入数据,而且在解决一个问题后就要立即输出结果. 一个在线算法是指它可以以序列化的方式一个个的处理输入,也就是说在开始 ...

  8. POJ 1330 Nearest Common Ancestors 【LCA模板题】

    任意门:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000 ...

  9. 【POJ】1330 Nearest Common Ancestors ——最近公共祖先(LCA)

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18136   Accept ...

随机推荐

  1. mysql explain的使用

    一.explain返回各列的含义: 1.table:显示这一行的数据是关于那张表的 2.type:重要的列,显示连接使用了何种类型,从最好到最差的连接类型为const.eq_reg.ref.range ...

  2. 自媒体人Chrome浏览器必备插件精选神器!

    自从互联网时代起,浏览器使用从最早的IE,到opera,到猎豹浏览器,到360双核浏览器,到火狐,到safari,到目前最喜欢用的chrome.一路下来,chrome的稳定性与扩展性征服了我,成了我必 ...

  3. POJ-1988Cube Stacking/HDU-2818Building Block;

    Cube Stacking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 23283   Accepted: 8166 Ca ...

  4. codeforces 359A

    #include<stdio.h> #define N 60 int map[N][N]; int main() {  int n,m,i,j,flag;  while(scanf(&qu ...

  5. 网络编程进阶:并发编程之协程、IO模型

    协程: 基于单线程实现并发,即只用一个主线程(此时可利用的CPU只有一个)情况下实现并发: 并发的本质:切换+保存状态 CPU正在运行一个任务,会在两种情况下切走去执行其他任务(切换有操作系统强制控制 ...

  6. [USACO12FEB]附近的牛Nearby Cows

    题目描述 Farmer John has noticed that his cows often move between nearby fields. Taking this into accoun ...

  7. php的socket通信【转载】

     对TCP/IP.UDP.Socket编程这些词你不会很陌生吧?随着网络技术的发展,这些词充斥着我们的耳朵.那么我想问: 1.         什么是TCP/IP.UDP?2.         Soc ...

  8. Gym 100792 King's Rout 拓扑排序

    K. King's Rout time limit per test 4.0 s memory limit per test 512 MB input standard input output st ...

  9. 快速幂取模模板 && 51nod 1013 3的幂的和

    #include <iostream> #include <cstdio> #include <cmath> #include <vector> #in ...

  10. [bzoj4131]并行博弈_博弈论

    并行博弈 bzoj-4131 题目大意:题目链接. 注释:略. 想法:我们发现无论如何操作都会使得$(1,1)$发生改变. 所以单个$ACG$的胜利条件就是$(1,1)$是否为黑色. 如果为黑色那么可 ...