【lca】lca的tarjan写法 poj1330
来源:互联网 发布:电脑流量统计软件 编辑:程序博客网 时间:2024/05/10 15:44
今天看了一下午的lca的tarjan写法,发现果真还是不能全部理解,不过还是有大部分能够理解,就将自己的思路写在这里备忘吧。。
tarjan算法是基于并查集与dfs的一种离线算法
tarjan算法的步骤是(当dfs到节点u时):
1 在并查集中建立仅有u的集合,设置该集合的祖先为u,就是普通的并查集,fa[i] = i;
1 对u的每个孩子v:
1.1 tarjan之
1.2 合并v到父节点u的集合,确保集合的祖先是u
2 设置u为已遍历
3 处理关于u的查询,若查询(u,v)中的v已遍历过,则LCA(u,v)=v所在的集合的祖先
图上已经讲解的很详细了,这里是并查集的一个应用。。
不过光看思想并不能解决问题,于是我找了一道lca的果题,poj1330
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=11136
Nearest Common Ancestors
Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u
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. Node x is an ancestor of node y if node x is in the path between the root and node y. For example, node 4 is an ancestor of node 16. Node 10 is also an ancestor of node 16. As a matter of fact, nodes 8, 4, 10, and 16 are the ancestors of node 16. Remember that a node is an ancestor of itself. Nodes 8, 4, 6, and 7 are the ancestors of node 7. A node x is called a common ancestor of two different nodes y and z if node x is an ancestor of node y and an ancestor of node z. Thus, nodes 8 and 4 are the common ancestors of nodes 16 and 7. A node x is called the nearest common ancestor of nodes y and z if x is a common ancestor of y and z and nearest to y and z among their common ancestors. Hence, the nearest common ancestor of nodes 16 and 7 is node 4. Node 4 is nearer to nodes 16 and 7 than node 8 is.
For other examples, the nearest common ancestor of nodes 2 and 3 is node 10, the nearest common ancestor of nodes 6 and 13 is node 8, and the nearest common ancestor of nodes 4 and 12 is node 4. In the last example, if y is an ancestor of z, then the nearest common ancestor of y and z is y.
Write a program that finds the nearest common ancestor of two distinct nodes in a tree.
In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is the root of the tree. Node x is an ancestor of node y if node x is in the path between the root and node y. For example, node 4 is an ancestor of node 16. Node 10 is also an ancestor of node 16. As a matter of fact, nodes 8, 4, 10, and 16 are the ancestors of node 16. Remember that a node is an ancestor of itself. Nodes 8, 4, 6, and 7 are the ancestors of node 7. A node x is called a common ancestor of two different nodes y and z if node x is an ancestor of node y and an ancestor of node z. Thus, nodes 8 and 4 are the common ancestors of nodes 16 and 7. A node x is called the nearest common ancestor of nodes y and z if x is a common ancestor of y and z and nearest to y and z among their common ancestors. Hence, the nearest common ancestor of nodes 16 and 7 is node 4. Node 4 is nearer to nodes 16 and 7 than node 8 is.
For other examples, the nearest common ancestor of nodes 2 and 3 is node 10, the nearest common ancestor of nodes 6 and 13 is node 8, and the nearest common ancestor of nodes 4 and 12 is node 4. In the last example, if y is an ancestor of z, then the nearest common ancestor of y and z is y.
Write a program that finds the nearest common ancestor of two distinct nodes in a tree.
Input
The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case starts with a line containing an integer N , the number of nodes in a tree, 2<=N<=10,000. The nodes are labeled with integers 1, 2,..., N. Each of the next N -1 lines contains a pair of integers that represent an edge --the first integer is the parent node of the second integer. Note that a tree with N nodes has exactly N - 1 edges. The last line of each test case contains two distinct integers whose nearest common ancestor is to be computed.
Output
Print exactly one line for each test case. The line should contain the integer that is the nearest common ancestor.
Sample Input
2161 148 510 165 94 68 44 101 136 1510 116 710 216 38 116 1216 752 33 43 11 53 5
Sample Output
43
其实这道题用RMQ,倍增也可以做,不过今天看的tarjan,就用tarjan做吧,具体看代码注释
【代码】
先贴一个标程
//O(n+Q)#include <iostream>#include <cstdio>#include <cstring>#include <vector>using namespace std;#define MAXN 10001int n,fa[MAXN];int rank[MAXN];int indegree[MAXN];int vis[MAXN];vector<int> hash[MAXN],Qes[MAXN];int ances[MAXN];//祖先void init(int n){ for(int i=0;i<=n;i++) { fa[i]=i; rank[i]=0; indegree[i]=0; vis[i]=0; ances[i]=0; hash[i].clear(); Qes[i].clear(); }}int find(int x){ if(x != fa[x]) fa[x]=find(fa[x]); return fa[x];}void unio(int x,int y){ int fx=find(x),fy=find(y); if(fx==fy) return ; if(rank[fy]<rank[fx]) fa[fy]=fx; else { fa[fx]=fy; if(rank[fx]==rank[fy]) rank[fy]++; }}void Tarjan(int u){ ances[u]=u; int i,size = hash[u].size(); for(i=0;i<size;i++) { Tarjan(hash[u][i]);//递归处理儿子 unio(u,hash[u][i]);//将儿子父亲合并,合并时会将儿子的父亲改为u ances[find(u)]=u;//此时find(u)仍为u,即 } vis[u]=1; //查询 size = Qes[u].size(); for(i=0;i<size;i++) { if(vis[Qes[u][i]]==1)//即查询的另一个结点开始已经访问过,当前的u在此回合访问。 { printf("%d\n",ances[find(Qes[u][i])]);//由于递归,此时还是在u return; } }}int main(){ int t; int i,j; scanf("%d",&t); while(t--) { scanf("%d",&n); init(n); int s,d; for(i=1;i<=n-1;i++) { scanf("%d%d",&s,&d); hash[s].push_back(d); indegree[d]++; } scanf("%d%d",&s,&d); Qes[s].push_back(d); Qes[d].push_back(s); for(j=1;j<=n;j++) { if(indegree[j]==0) { Tarjan(j); break; } } } return 0;}
再贴一个自己写的,但不知道哪错了,再找找错误吧
#include<cstdio>#include<iostream>#include<vector>#include<cstring>#define mem(a,x) memset(a, x , sizeof(a))using namespace std;struct edge{int v,next;}e[10000 + 5];int ind[10000 + 5];//记录每个点的入度int fa[10000 + 5];//并查集用int head[10000 + 5], k = 1;bool vis[10000 + 5];//判断是否遍历过int rnk[10000 + 5];//就是这个不知道什么用,觉得像是并查集压缩路径的优化int anc[10000 + 5];//每个点的祖先vector<int >que[10000 + 5];//与x有关的询问放在q[x]中int T;int root;int n; void init()//初始化{mem(vis,0);mem(head,0);mem(fa,0);mem(ind,0);mem(rnk,0);mem(e,0);k = 1;for(int i = 1; i <= n; i++)fa[i] = i,anc[i] = 0,que[i].clear();} void adde(int u, int v)//加边,其实觉得用vector数组更好用{e[k].v = v;e[k].next = head[u];head[u] = k++;}int find(int x)//并查集,不解释{return fa[x] == x ? x : fa[x] = find(fa[x]);} void Union(int x, int y)//合并{int fx = find(x),fy = find(y);if(fx == fy)return ;if(rnk[fx] > rnk[fy]) fa[fy] = fx;else fa[fx] = fy, rnk[fy] += rnk[fx] == rnk[fy];}void tarjan(int u){anc[u] = u;//u是u集合的祖先for(int i = head[u]; i ; i = e[i].next)//遍历边{int v = e[i].v;if(!vis[v]){tarjan(v);Union(u,v);anc[find(u)] = u;//保证u的子树的祖先是u}}vis[u] = 1;for(int i = 0; i < que[u].size(); i++)//处理查询if(vis[que[u][i]]){printf("%d\n",anc[find(que[u][i])]);return;}}int main(){scanf("%d", &T);while(T--){init();scanf("%d", &n);for(int i = 1; i < n; i++){int u, v;scanf("%d%d", &u, &v);adde(u,v);ind[v]++;}for(int i = 1; i <= n; i++)if(!ind[i]){root = i;break;}int a,b;scanf("%d%d", &a, &b);que[a].push_back(b);que[b].push_back(a);tarjan(root);}return 0;}
暂时就这些了,到时再来慢慢完善吧。
0 0
- 【lca】lca的tarjan写法 poj1330
- POJ1330(LCA-离线tarjan)
- LCA问题的Tarjan算法(POJ1330)
- poj1330 Nearest Common Ancestors LCA Tarjan 待补完
- poj1330 Nearest Common Ancestors(lca,tarjan&倍增)
- LCA的Tarjan算法
- 【lca】lca转rmq poj1330
- poj1330 LCA离线tarjan算法 复杂度O(n+q)
- POJ1330 Nearest Common Ancestors【最近公共祖先】【Tarjan-LCA算法】
- POJ1330:Nearest Common Ancestors(LCA + Tarjan离线处理)
- POJ1330 Nearest Common Ancestors (tarjan离线求LCA模板)
- LCA问题 poj1330 / poj1470
- poj1330 LCA离线算法
- poj1330 lca转rmq
- poj1330 倍增LCA
- poj1330 LCA模板题
- poj1330 裸LCA
- [poj1330]lca最劣版
- hdoj 1556 Color the ball(线段树||树状数组)
- jquery插件编写(以折叠面板为例)
- OLED显示汉字
- Oracle语句优化规则汇总(2)
- 跳表
- 【lca】lca的tarjan写法 poj1330
- iOS开发多线程篇—自定义NSOperation
- Hbase系统架构及数据结构
- C语言循环语句
- 创建UILable的时候,可以根据文字算出文字所占区域大小的方法
- git简单使用
- iOS 在UILabel显示不同的颜色和字体
- Oracle语句优化规则汇总(3)
- Android Touch事件传递机制解析