[POJ 1330] Nearest Common Ancestors (倍增法)

题目同上篇,最近公共祖先。

因为没有清零tot,RE了好多次TAT

一定要初始化啊!!

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<queue>
 4 #include<iostream>
 5 using namespace std;
 6 int root,head[10010];
 7 int next[10010],to[10010],tot;
 8 int deep[10010],n;
 9 int p[10010][16];
10 int deg[10010];
11 inline void bfs(int root) {
12     queue<int> Q;
13     p[root][0]=root; deep[root]=0;
14     Q.push(root);
15     while(!Q.empty()) {
16         int u=Q.front(); Q.pop();
17         for(int i=1;i<15;i++)
18             p[u][i]=p[p[u][i-1]][i-1];
19         for(int i=head[u];i!=-1;i=next[i]) {
20             int v=to[i];
21             deep[v]=deep[u]+1; p[v][0]=u;
22             Q.push(v);
23         }
24     }
25 }
26 inline int LCA(int x,int y) {
27     if(deep[x]<deep[y]) {int t=x;x=y;y=t;}
28     for(int i=0;i<15;i++)
29         if((deep[x]-deep[y]) & (1<<i)) x=p[x][i];
30     if(x==y)  return x;
31     for(int i=14;i>=0;i--)
32         if(p[x][i]!=p[y][i])
33             x=p[x][i],y=p[y][i];
34     return p[x][0];
35 }
36 int main() {
37     int T;
38     scanf("%d",&T);
39     while(T--) {
40         tot=0;
41         memset(head,-1,sizeof(head)); memset(deg,0,sizeof(deg));
42         scanf("%d",&n);
43         for (int i=1;i<=n-1;++i) {
44             int u,v;
45             scanf("%d%d",&u,&v);
46             to[tot]=v;
47             next[tot]=head[u];
48             head[u]=tot++;
49             deg[v]=1;
50         }
51         for (int i=1;i<=n;++i)
52             if(! deg[i]) {root=i;break;}
53         bfs(root);
54         int u,v;
55         scanf("%d%d",&u,&v);
56         printf("%d\n",LCA(u,v));
57     }
58     return 0;
59 }

倍增法简介:

deep[i] 表示 i节点的深度, fa[i,j]表示 i 的 2^j (即2的j次方) 倍祖先,那么fa[i , 0]即为节点i 的父亲,然后就有一个递推式子fa[i,j]=fa [fa[i,j-1],j-1]

可以这样理解:设tmp = fa [i, j - 1] ,tmp2 = fa [tmp, j - 1 ] ,即tmp 是i 的第2 ^ (j - 1) 倍祖先,tmp2 是tmp 的第2 ^ (j - 1) 倍祖先,

所以tmp2 是i 的第2 ^ (j - 1) + 2 ^ (j - 1) =  2^ j 倍祖先

注意:这里的“倍”可不能理解为倍数的意思,而是距离节点i有多远的意思,节点i的第2 ^ j 倍祖先表示的节点u满足deep[ u ] - deep[ i ] = 2 ^ j。

        这样子一个O(NlogN)的预处理求出每个节点的 2^k 的祖先  
        然后对于每一个询问的点对a, b的最近公共祖先就是: 

先判断是否 d[x]< d[y] ,如果是的话就交换一下(保证 x 的深度大于 y 的深度), 然后把 x 调到与 y 同深度, 同深度以后再把a, b 同时往上调,调到有一个最小的 j 满足fa [x,j] != fa [y,j] (x,y是在不断更新的), 最后再把(x,y)往上调(x=p[x,0], y=p[y,0])  ,一个一个向上调直到x = y, 这时 x或y 就是他们的最近公共祖先。

时间: 2024-10-08 10:33:28

[POJ 1330] Nearest Common Ancestors (倍增法)的相关文章

POJ 1330 Nearest Common Ancestors 倍增算法的LCA

POJ 1330 Nearest Common Ancestors 题意:最近公共祖先的裸题 思路:LCA和ST我们已经很熟悉了,但是这里的f[i][j]却有相似却又不同的含义.f[i][j]表示i节点的第2j个父亲是多少   这个代码不是我的,转自 邝斌博客 1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013-9-5 9:45:17 4 File Name :F

POJ 1330 Nearest Common Ancestors LCA题解

本题是一个多叉树,然后求两点的最近公共单亲节点. 就是典型的LCA问题.这是一个很多解法的,而且被研究的很透彻的问题. 原始的解法:从根节点往下搜索,若果搜索到两个节点分别在一个节点的两边,那么这个点就是最近公共单亲节点了. Trajan离线算法:首次找到两个节点的时候,如果记录了他们的最低单亲节点,那么答案就是这个最低的单亲节点了. 问题是如何有效记录这个最低单亲节点,并有效根据遍历的情况更新,这就是利用Union Find(并查集)记录已经找到的节点,并及时更新最新访问的节点的当前最低单亲节

POJ 1330 Nearest Common Ancestors(树)

Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17628   Accepted: 9335 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In the figure, each

POJ 1330 Nearest Common Ancestors LCA(在线RMQ,离线Tarjan)

链接:http://poj.org/problem?id=1330 题意:只看题目就知道题目是什么意思了,最近公共祖先,求在一棵树上两个节点的最近公共祖先. 思路:求最近公共祖先有两种算法,在线和离线,在线方法是用RMQ求LCA,一句话总结就是在从DFS时,从第一个点到第二个点的最短路径中深度最浅的点就是公共祖先,用RMQ处理,一般问题的最优解决方式的复杂度是O(NlogN)的预处理+N*O(1)的查询.离线方法是Tarjan算法,将所有询问的两个点都记录下来,在DFS过程中不断将每个点自身作为

[POJ 1330] Nearest Common Ancestors (朴素方法)

POJ 1330: Nearest Common Ancestors Time Limit: 1000ms Memory Limit: 32Mb Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:  In the figure, each node is labeled with an integer fro

POJ - 1330 Nearest Common Ancestors(基础LCA)

POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %lld & %llu Submit Status Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:  In t

poj 1330 Nearest Common Ancestors

题目连接 http://poj.org/problem?id=1330 Nearest Common Ancestors Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:  In the figure, each node is labeled with an integer from {1, 2,...,

[最近公共祖先] POJ 1330 Nearest Common Ancestors

Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27316   Accepted: 14052 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:  In the figure, eac

poj 1330 Nearest Common Ancestors lca 在线rmq

Nearest Common Ancestors Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:  In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is the root of the tree.

poj 1330 Nearest Common Ancestors 【LCA】

Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20073   Accepted: 10631 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In the figure, each