题干:

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条有向边,然后一个询问u,v 求两者的lca。

解题报告:

那部分注释掉的代码写哪个都可以。。。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int n;
int head[MAX];
struct Edge {int to,ne;
} e[MAX];
int tot;
void add(int u,int v) {e[++tot].to = v;e[tot].ne = head[u];head[u] = tot;
}
int fa[MAX][22],dep[MAX],in[MAX];
void dfs(int cur,int rt) {fa[cur][0] = rt;dep[cur] = dep[rt]+1;for(int i = 1; i<22; i++) {fa[cur][i] = fa[fa[cur][i-1]][i-1];}for(int i = head[cur]; i!=-1; i = e[i].ne) {int v = e[i].to;if(v == rt) continue;dfs(v,cur);}
}
int lca(int u,int v) {if(dep[u] < dep[v]) swap(u,v);//让u往上倍增 int dc = dep[u]-dep[v];for(int i = 21; i>=0; i--) {int now = fa[u][i];if(dep[now] >= dep[v]) u = now;}
//  for(int i = 0; i<22; i++) {
//      if((1<<i)&dc) u = fa[u][i];
//  }if(u == v) return u;for(int i = 21; i>=0; i--) {if(fa[u][i] != fa[v][i]) {u = fa[u][i];v = fa[v][i];}}return fa[u][0];
}
int main()
{   int t,u,v;cin>>t;while(t--) {     scanf("%d",&n);//inittot=0;for(int i = 1; i<=n; i++) head[i] = -1,in[i]=0;for(int i = 1; i<=n-1; i++) {scanf("%d%d",&u,&v);//u is v's parentsadd(u,v);in[v]++;}int tar;for(int i = 1; i<=n; i++) {if(in[i] == 0) {tar = i;break;}}dfs(tar,-1);scanf("%d%d",&u,&v);printf("%d\n",lca(u,v));} return 0 ;
}
//20:32-20:50

【POJ - 1330】Nearest Common Ancestors(lca,模板题)相关推荐

  1. poj 1330 Nearest Common Ancestors LCA/DFS

    题目链接: http://poj.org/problem?id=1330 题意: 求出两点间的最近公共祖先. 题解: 第一种: 并查集维护:http://www.cnblogs.com/procedu ...

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

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

  3. POJ 1330 Nearest Common Ancestors 【LCA模板题】

    任意门:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000 ...

  4. POJ 1330:Nearest Common Ancestors【lca】

    题目大意:唔 就是给你一棵树 和两个点,问你这两个点的LCA是什么 思路:LCA的模板题,要注意的是在并查集合并的时候并不是随意的,而是把叶子节点合到父节点上 #include<cstdio&g ...

  5. POJ - 1330 Nearest Common Ancestors(树上倍增/树链剖分求LCA)

    题目链接:点击查看 题目大意:给出一棵有根树,我们需要求出两个点的lca 题目分析:因为题目说了是有根树,所以我们在建图的时候直接建有向图就好了,并且记录一下每个点的入度,建完图后找一下入度为0的点, ...

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

    题目同上篇,最近公共祖先. 因为没有清零tot,RE了好多次TAT 一定要初始化啊!! 1 #include<cstdio> 2 #include<cstring> 3 #in ...

  7. POJ - 1330 Nearest Common Ancestors tanjan_LCA

    传送门 题意就是题目 所谓在线,就是一个一个贼笨且时间太长. #include<stdio.h> #include<string.h> #include<iostream ...

  8. Nearest Common Ancestors(LCA板子)

    题目链接:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 1000 ...

  9. LCA——JD 3055 Nearest Common Ancestors

    3055: Nearest Common Ancestors Time Limit: 1 Sec   Memory Limit: 128 MB Description 给定N个节点的一棵树,有K次查询 ...

  10. JDOJ 3055: Nearest Common Ancestors

    JDOJ 3055: Nearest Common Ancestors JDOJ传送门 Description 给定N个节点的一棵树,有K次查询,每次查询a和b的最近公共祖先. 样例中的16和7的公共 ...

最新文章

  1. python 科学计算基础教程电子版-Python 科学计算基础 (整理)
  2. Zjnu Stadium HDU - 304 加权并查集
  3. 面试必考的网络协议相关题目应该如何回答
  4. LAMP介绍-MySQL安装
  5. Ajax技术的基本原理
  6. 制作 小 linux 教程,用BusyBox制作Linux最小系统
  7. 2020年7大技术趋势
  8. 如何使用 Weave 网络?- 每天5分钟玩转 Docker 容器技术(63)
  9. 关于fragstats内存问题
  10. 修好了一个罗技鼠标,鼠标左键单击变双击问题解决了。
  11. Oracle索引的原理及使用
  12. 多项式乘法 快速傅里叶变换
  13. 【散文诗】STM32时钟框图
  14. 一点知识丨Base64 的图片如何完美复制到系统粘贴板
  15. 【修真院WEB小课堂】 angular js中的依赖注入是什么?
  16. 字节跳动的产品经理是怎么工作的?
  17. 【基础版】大学计算机-计算思维导论
  18. 除了中国知网和谷歌文学还有哪些好的有权威的资源站?
  19. Docker基础(下)
  20. 超分算法RDN:Residual Dense Network for Image Super-Resolution 超分辨率图像重建

热门文章

  1. 你是个有魅力的人吗?人格魅力这样修养而成
  2. leetcode数组汇总_[LeetCode] 300. 最长上升子序列
  3. spring webflow getting start
  4. 如何写一个脚本语言_零基础小白如何学会写文案?文案写作技巧之一:如何写一个吸引读者的文案开头...
  5. x210烧写流程(inand)
  6. wince2秒快速启动TOC分析
  7. JOJ的2042面试题目的数学推导过程
  8. 如何连接安卓手机_安卓手机如何使用AirPods
  9. python的map怎么用_python中的map怎么使用
  10. 点云数据生成三维模型_可直接编辑的高质量3D生成模型:三维深度生成方法SDM-NET...