POJ1330(LCA离线和在线)
来源:互联网 发布:淘宝怎么能分期付款 编辑:程序博客网 时间:2024/06/06 05:24
Nearest Common Ancestors
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 28271 Accepted: 14467
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.
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
2
16
1 14
8 5
10 16
5 9
4 6
8 4
4 10
1 13
6 15
10 11
6 7
10 2
16 3
8 1
16 12
16 7
5
2 3
3 4
3 1
1 5
3 5
Sample Output
4
3
题意:给出n个点,n-1条边的树,求lca(u,v);
题解:
离线:
#include<stdio.h>#include<string.h>#include<iostream>#include<cmath>#define eps 1e-8#define ll long longusing namespace std;const int N=10010;int f[N];int find(int x){ return f[x]==x?x:f[x]=find(f[x]);}void unite(int u,int v){ int x=find(u); int y=find(v); if(x!=y) f[x]=y;}bool vis[N];int acr[N];int head[N];int ans[N];bool flag[N];int h[N];int tot,tt;struct node{ int to,next;}edge[N<<1];struct Query{ int q,next; int index;}query[N];void add(int u,int v){ edge[tot].to=v; edge[tot].next=head[u]; head[u]=tot++;}void addq(int u,int v,int index){ query[tt].q=v; query[tt].next=h[u]; query[tt].index=index; h[u]=tt++; query[tt].q=u; query[tt].next=h[v]; query[tt].index=index; h[v]=tt++;}void init(){ tot=0; tt=0; memset(head,-1,sizeof(head)); memset(h,-1,sizeof(h)); memset(vis,0,sizeof(vis)); memset(acr,0,sizeof(acr)); memset(flag,0,sizeof(flag));}void LCA(int u){ acr[u]=u; vis[u]=true; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(vis[v]) continue; LCA(v); unite(u,v); acr[find(u)]=u; } for(int i=h[u];i!=-1;i=query[i].next) { int v=query[i].q; if(vis[v]) ans[query[i].index]=acr[find(v)]; }}int n,u,v;int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=1;i<=n;i++)f[i]=i; init(); for(int i=1;i<n;i++) { scanf("%d%d",&u,&v); flag[v]=true; add(u,v); add(v,u); } int root; int Q=1; for(int i=0;i<Q;i++) { scanf("%d%d",&u,&v); addq(u,v,i); } for(int i=1;i<=n;i++) { if(!flag[i]) { root=i; break; } } LCA(root); for(int i=0;i<Q;i++) printf("%d\n",ans[i]); } return 0;}
在线:
#include<iostream>#include<stdio.h>#include<stdlib.h>#include<cmath>#include<string.h>using namespace std;const int N=1e5;int vis[N];int ver[N];int sta[N];int first[N];int head[N];int mi[N][20];int deep[N];int tot;struct node{ int to,next;}edge[N<<1];void add(int u,int v){ edge[tot].to=v; edge[tot].next=head[u]; head[u]=tot++;}void dfs(int u,int dep){ vis[u]=true; ver[++tot]=u; first[u]=tot; deep[tot]=dep; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(!vis[v]) { dfs(v,dep+1); ver[++tot]=u; deep[tot]=dep; } }}void RMQ(int num){ for(int i=1;i<=num;i++) mi[i][0]=i; for(int j=1;j<20;j++) { for(int i=1;i<=num;i++) { if(i+(1<<j)-1<=num) { int a=mi[i][j-1]; int b=mi[i+(1<<(j-1))][j-1]; mi[i][j]=deep[a]<deep[b]?a:b; } } }}int lca(int u,int v){ int x=first[u]; int y=first[v]; if(x>y) swap(x,y); int k=(int)(log((double)(y-x+1))/log(2.0)); int res=deep[mi[x][k]]<deep[mi[y-(1<<k)+1][k]]?mi[x][k]:mi[y-(1<<k)+1][k]; return ver[res];}void init(){ tot=0; memset(head,-1,sizeof(head)); memset(vis,0,sizeof(vis)); memset(sta,0,sizeof(sta));}int n,u,v,l,r;int main(){ int t; scanf("%d",&t); while(t--) { init(); scanf("%d",&n); for(int i=1;i<n;i++) { scanf("%d%d",&u,&v); add(u,v); sta[v]=1; } for(int i=1;i<=n;i++) { if(!sta[i]) { dfs(i,1); break; } } RMQ(tot); scanf("%d%d",&l,&r); printf("%d\n",lca(l,r)); }}
- POJ1330(LCA离线和在线)
- lca离线算法模板(poj1330验证)
- poj1330 LCA离线算法
- POJ1330(LCA-离线tarjan)
- LCA三种算法学习(离线算法tarjan+在线算法转rmq+在线倍增)例题poj1330、1470;hdu4547、2874
- poj1330+hdu2586 LCA离线算法
- poj1330 Nearest Common Ancestors(LCA离线算法)
- POJ1330:Nearest Common Ancestors(LCA + Tarjan离线处理)
- POJ1330 Nearest Common Ancestors (tarjan离线求LCA模板)
- poj1330 Nearest Common Ancestors(LCA离线)
- POJ 1986 Distance Queries(LCA在线和离线)
- poj1330 LCA在线算法 复杂度O(nlogn)
- LCA在线算法ST&&DFS->POJ1330&&POJ1470
- LCA模板题(在线加离线)
- (开坑)在线/离线求lca
- poj1330 LCA离线tarjan算法 复杂度O(n+q)
- LCA最近公共祖先的离线算法(Tarjan)和在线算法(ST)
- 详谈LCA 在线(RMQ-ST) 和 离线(Tarjan)hdu2586为例
- SLF改造计划(三分枚举)(AOJ 853)
- JavaWeb RSA加密使用小解
- jquery选择器中两个class是什么意思?
- Class Throwable
- MyBatis 3 电子书
- POJ1330(LCA离线和在线)
- Java字符串类
- [Lpp] HTTP协议状态码详解(HTTP Status Code)
- 左程云著算法与数据结构题目最优解笔记-链表
- Centos修改镜像为国内的阿里云源或者163源等国内源
- $(...).modal is not a function
- leetcode 220. Contains Duplicate III
- ZOJ2481 Unique Ascending Array
- VUA-10375 Choose and divide