A. Nearest Common Ancestors

Time Limit: 1000ms
Case Time Limit: 1000ms
Memory Limit: 10000KB
 
64-bit integer IO format: %lld      Java class name: Main
 
 
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 解题:本来打算用dfs+RMQ写的,结果写得一塌糊涂,完全不对。后来发现只有一个询问,即为了这一次询问可以破坏原有结构。直接暴力好了。
 #include <iostream>
#include <cstdio>
using namespace std;
bool vis[];
int uf[];
int main() {
int ks,n,i,x,y;
scanf("%d",&ks);
while(ks--) {
scanf("%d",&n);
for(i = ; i <= n; i++) {
vis[i] = false;
uf[i] = i;
}
for(i = ; i < n; i++) {
scanf("%d %d",&x,&y);
uf[y] = x;
}
scanf("%d %d",&x,&y);
vis[x] = true;
x = uf[x];
while(x != uf[x]) {
vis[x] = true;
x = uf[x];
}
while(y != uf[y]) {
if(vis[y]) break;
y = uf[y];
}
printf("%d\n",y);
}
return ;
}

倍增法求LCA

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
int n,deep[maxn],fa[maxn][];
vector<int>g[maxn];
void dfs(int u,int f){
for(int i = ; i < g[u].size(); i++){
if(g[u][i] == f) continue;
deep[g[u][i]] = deep[u]+;
dfs(g[u][i],u);
}
}
void init(){
for(int j = ; j < ; j++){
for(int i = ; i <= n; i++)
fa[i][j] = fa[fa[i][j-]][j-];
}
}
int LCA(int u,int v){
if(deep[u] < deep[v]) swap(u,v);
int i,d = deep[u]-deep[v];
for(i = ; i < ; i++) if((<<i)&d) u = fa[u][i];
if(u == v) return u;
for(i = ; i >= ; i--){
if(fa[u][i] != fa[v][i]){
u = fa[u][i];
v = fa[v][i];
}
}
return fa[u][];
}
int main() {
int t,i,u,v,root;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(i = ; i <= n; i++){
g[i].clear();
deep[i] = ;
}
memset(fa,,sizeof(fa));
for(i = ; i < n; i++){
scanf("%d %d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
fa[v][] = u;
if(!fa[u][]) root = u;
}
deep[root] = ;
dfs(root,-);
init();
scanf("%d %d",&u,&v);
printf("%d\n",LCA(u,v));
}
return ;
}

tarjan求LCA

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
vector<int>g[maxn];
int n,x,y,uf[maxn],ans;
bool vis[maxn];
int Find(int rt) {
if(rt != uf[rt])
uf[rt] = Find(uf[rt]);
return uf[rt];
}
bool dfs(int u) {
uf[u] = u;
for(int i = ; i < g[u].size(); i++) {
if(!vis[g[u][i]]) {
if(dfs(g[u][i])) return true;
uf[g[u][i]] = u;
}
}
if(u == x && vis[y]) {
ans = Find(y);
return true;
} else if(u == y && vis[x]) {
ans = Find(x);
return true;
}
vis[u] = true;
return false;
}
int main() {
int t,i,u,v,root;
scanf("%d",&t);
while(t--) {
scanf("%d",&n);
for(i = ; i <= n; i++) {
g[i].clear();
vis[i] = false;
}
for(i = ; i < n; i++) {
scanf("%d %d",&u,&v);
g[u].push_back(v);
vis[v] = true;
}
for(i = ; i <= n; i++)
if(!vis[i]) {
root = i;
break;
}
scanf("%d%d",&x,&y);
memset(vis,false,sizeof(vis));
dfs(root);
cout<<ans<<endl;
}
return ;
}

A. Nearest Common Ancestors的更多相关文章

  1. POJ 1330 Nearest Common Ancestors(Targin求LCA)

    传送门 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26612   Ac ...

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

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

  3. POJ 1330 Nearest Common Ancestors

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

  4. POJ1330 Nearest Common Ancestors

      Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24587   Acce ...

  5. POJ 1330 Nearest Common Ancestors(Tree)

    题目:Nearest Common Ancestors 根据输入建立树,然后求2个结点的最近共同祖先. 注意几点: (1)记录每个结点的父亲,比较层级时要用: (2)记录层级: (3)记录每个结点的孩 ...

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

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

  7. POJ 1330 Nearest Common Ancestors LCA题解

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

  8. POJ - 1330 Nearest Common Ancestors(基础LCA)

    POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %l ...

  9. POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)

    POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...

  10. pku 1330 Nearest Common Ancestors LCA离线

    pku 1330 Nearest Common Ancestors 题目链接: http://poj.org/problem?id=1330 题目大意: 给定一棵树的边关系,注意是有向边,因为这个WA ...

随机推荐

  1. JS中的回收机制

    js的设计者为了让没有必要的变量保存在内存中,(我们写的任何变量都是需要内存空间的),什么叫没有必要的变量?也就是说你不在需要这个变量的时候它就会被销毁?那么你肯定会问js怎么知道那些变量是我们不需要 ...

  2. 高性能Javascript总结

    一.加载和运行 Javascript代码执行会阻塞其他浏览器处理过程.充分利用webpack或gulp工具对文件打包压缩,减少js文件的数量,从而减少http请求的次数,以提高网页应用的实际性能. 二 ...

  3. Wrapper class package.jaxws.methodName is not found. Have you run APT to generate them?解决方案

    使用JAX-WS 2.X基于Web容器发布WebService报错,错误信息类似于: Wrapper class package.jaxws.methodName is not found. Have ...

  4. 【iOS】UITableview cell 顶部空白的n种设置方法

    我知道没人会主动设置这个东西,但是大家一定都遇到过这个问题,下面总结下可能是哪些情况: 1, self.automaticallyAdjustsScrollViewInsets = NO;  这个应该 ...

  5. (十一)mybatis之映射器(select)

    映射器 映射器的主要元素有八种: 元素名称 描述 select 查询语句,可自定义参数 insert 插入语句,执行后返回插入的条数 update 更新语句,执行后返回更新的条数 delete 删除语 ...

  6. Servlet The Request

    The Request HTTP Protocol Parameters 所有的HTTP Protocol Parameters都会放在一个Map中, 可以通过getParameterMap得到. 对 ...

  7. 几个不错的APP网站。

    http://www.yunshipei.com/yunshipei.html http://www.appcan.cn/

  8. R文件报错的原因

    一般R文件报错,无非是资源文件错误,图片命名错误,但是编译都会报错,可以很快解决.但是前几天,引入一个第三方aar包后,项目编译正确,但是就是R文件报错,找不到R文件,整个项目一片报红. 1)首先编译 ...

  9. ucosii(2.89)semaphore 应用要点

    semaphore 的作用:1,允许一个任务与其他任务(中断)同步.2,取得共享资源使用权.3,标志事件的发生.

  10. layui 数据table隐藏表头

    $('.layui-table .layui-table-cell > span').css({'font-weight': 'bold'});//表头字体样式 /*$('th').css({' ...