POJ 1330 Nearest Common Ancestors (模板水题离线LCA,3级)
来源:互联网 发布:艾美仕数据 编辑:程序博客网 时间:2024/05/20 10:12
N - Nearest Common Ancestors
Crawling in process...Crawling failedTime Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64uAppoint description:
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思路:水的没法说了,不解释
#include<iostream>#include<cstdio>#include<cstring>#define FOR(i,a,b) for(int i=a;i<=b;++i)#define clr(f,z) memset(f,z,sizeof(f))using namespace std;const int mm=10002;class Edge{ public:int v,next;}e[mm+mm];int qh[mm],head[mm],edge,rt[mm],id[mm];bool vis[mm];int anc[mm],n,m,root,ans;void data(){ clr(qh,-1);clr(head,-1);edge=0;}void add(int u,int v,int*h){ e[edge].v=v;e[edge].next=h[u];h[u]=edge++;}int find(int x){ if(x^rt[x]) { rt[x]=find(rt[x]); } return rt[x];}void dfs(int u){ int v; rt[u]=u;vis[u]=1; for(int i=head[u];~i;i=e[i].next) { v=e[i].v; if(!vis[v]) { dfs(v);rt[v]=u; } } for(int i=qh[u];~i;i=e[i].next) { v=e[i].v; if(vis[v]) { //printf("%d %d %d\n",u,v,find(v)); ans=find(v); } }}void find_bcc(){ clr(vis,0); dfs(root);}int main(){ int u,v,cas; while(~scanf("%d",&cas)) while(cas--) { data(); clr(id,0); scanf("%d",&n); FOR(i,2,n) { scanf("%d%d",&u,&v); id[v]++; add(u,v,head);add(v,u,head); } FOR(i,1,n) if(id[i]==0)root=i; scanf("%d%d",&u,&v); add(u,v,qh);add(v,u,qh); find_bcc(); printf("%d\n",ans); }}
- POJ 1330 Nearest Common Ancestors (模板水题离线LCA,3级)
- poj-1330 Nearest Common Ancestors(LCA模板题)
- poj 1330 Nearest Common Ancestors(离线LCA)
- POJ 1330 Nearest Common Ancestors(LCA 离线算法)
- POJ 1330Nearest Common Ancestors LCA模板
- poj 1330 Nearest Common Ancestors(LCA模板)
- POJ 1330(Nearest Common Ancestors-Lca模板题)
- POJ-1330 Nearest Common Ancestors(lca模板题)
- 【LCA模板题】poj 1330 Nearest Common Ancestors
- poj 1330 Nearest Common Ancestors(LCA模板)
- poj 1330 Nearest Common Ancestors(LCA)
- 【POJ 1330】 Nearest Common Ancestors(LCA)
- poj 1330 Nearest Common Ancestors(LCA)
- POJ 1330Nearest Common Ancestors(LCA)
- POJ 1330 Nearest Common Ancestors(LCA)
- POJ 1330 Nearest Common Ancestors(LCA)
- POJ1330 Nearest Common Ancestors (tarjan离线求LCA模板)
- poj 1330 Nearest Common Ancestors (LCA) tarjan离线算法
- Android中使用WebView, WebChromeClient和WebViewClient加载网页
- python21 函数可以返回一些东西
- 怎样提高WebService性能大数据量网络传输处理(转)
- RAC的日常管理命令
- python中 class 或对象属性转化成dict 、dict转换成对象
- POJ 1330 Nearest Common Ancestors (模板水题离线LCA,3级)
- poj 1503 高精度加法
- python encode和decode函数说明
- 菜鸟做百度知道自问自答心得
- 【listview&checkbox】listview中有checkbox 滑动后状态改变
- SPOJ 962 - Intergalactic Map 1<-2->3 构图最大流
- 解读:百度将处理不相关静态搜索结果页
- Visual Studio 2008 和 Windows 环境变量及如何在文件中引用环境变量
- 出栈次序问题---卡特兰属的另一种理解