A - Nearest Common Ancestors

Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu

Submit Status

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
【分析】这题就是求最近公共祖先,让我对Tarjan有了一个新的认识。先找到根节点,一直往下深搜,
找到子节点,若该节点就是要求的点且另一个点已经被访问过,则另一个点所在并查集的根节点即为最近公共祖先。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x3f3f3f3f
#define mod 1000000007
typedef long long ll;
using namespace std;
const int N = ;
int fa[N],node1,node2,ret;
bool root[N],visit[N];
vector<int>child[N];
int Find(int x)
{
if(x==fa[x]) return x;
return fa[x]=Find(fa[x]);
}
void Union(int x,int y)
{
x=Find(x); y=Find(y);
fa[y]=x;
}
void Tarjan(int root)
{
fa[root]=root;visit[root]=true;
for(int i=;i<child[root].size();i++)
{
Tarjan(child[root][i]);
Union(root,child[root][i]);
} if(root==node1&&visit[node2])
{
ret=fa[Find(node2)];
return ;
}
if(root==node2&&visit[node1])
{
ret=fa[Find(node1)];
return ;
}
}
int main()
{
int T,n,i;
scanf("%d",&T);
while(T--)
{
memset(visit,false,sizeof(visit));
scanf("%d",&n);
for(i=;i<=n;i++)
{
root[i]=true;
child[i].clear();
}
for(i=;i<=n-;i++)
{
int a,b;
scanf("%d%d",&a,&b);
child[a].push_back(b);
root[b]=false;//寻找root
}
scanf("%d%d",&node1,&node2);
for(i=;i<=n;i++)
if(root[i])
{
Tarjan(i);
break;
}
printf("%d\n",ret);
}
}

POJ1330 Nearest Common Ancestors(最近公共祖先)(tarjin)的更多相关文章

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

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

  2. POJ - 1330 Nearest Common Ancestors 最近公共祖先+链式前向星 模板题

    A rooted tree is a well-known data structure in computer science and engineering. An example is show ...

  3. POJ 1330 Nearest Common Ancestors (最近公共祖先LCA + 详解博客)

    LCA问题的tarjan解法模板 LCA问题 详细 1.二叉搜索树上找两个节点LCA public int query(Node t, Node u, Node v) { int left = u.v ...

  4. POJ1330 Nearest Common Ancestors

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

  5. poj 1330 Nearest Common Ancestors 求最近祖先节点

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

  6. [POJ1330]Nearest Common Ancestors(LCA, 离线tarjan)

    题目链接:http://poj.org/problem?id=1330 题意就是求一组最近公共祖先,昨晚学了离线tarjan,今天来实现一下. 个人感觉tarjan算法是利用了dfs序和节点深度的关系 ...

  7. POJ1330Nearest Common Ancestors——近期公共祖先(离线Tarjan)

    http://poj.org/problem? id=1330 给一个有根树,一个查询节点(u,v)的近期公共祖先 836K 16MS #include<iostream> #includ ...

  8. POJ1330 Nearest Common Ancestors (JAVA)

    经典LCA操作.. 贴AC代码 import java.lang.reflect.Array; import java.util.*; public class POJ1330 { // 并查集部分 ...

  9. POJ1470Closest Common Ancestors 最近公共祖先LCA 的 离线算法 Tarjan

    该算法的详细解释请戳: http://www.cnblogs.com/Findxiaoxun/p/3428516.html #include<cstdio> #include<alg ...

随机推荐

  1. P1641 [SCOI2010]生成字符串

    P1641 [SCOI2010]生成字符串 题目描述 lxhgww最近接到了一个生成字符串的任务,任务需要他把n个1和m个0组成字符串,但是任务还要求在组成的字符串中,在任意的前k个字符中,1的个数不 ...

  2. 【BZOJ 4007】[JLOI2015]战争调度 DP+搜索+状压

    又是一道思路清新的小清晰. 观察题目,如果我们确定了平民或者贵族的任意一方,我们便可以贪心的求出另一方,至此20分:我们发现层数十分小,那么我们就也是状压层数,用lca转移,线性dp,至此50分(好像 ...

  3. [BZOJ3829][Poi2014]FarmCraft 贪心

    这个题应该是很容易想到贪心的,只要可是怎么贪才是科学的呢?我们分析一下题干,对于每个边只能一进一出因此,对于树上的一棵子树,我们只要一进子树就必须遍历完,因此我们只能进行一遍 dfs() 然后我们发现 ...

  4. [fzu 2271]不改变任意两点最短路至多删的边数

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2271 题目中说每条边的边权都是[1,10]之间的整数,这个条件非常关键!以后一定要好好读题啊…… 做10次循环 ...

  5. 关于控制下拉框select只读的js控制

    文本框有readonly属性,直接设置:下拉框没有readonly属性,也不能通过其他属性进行只读的设置,下拉框只有disabled属性,但是这个属性设成true之后,值就获取不到了: 我在网上搜了一 ...

  6. windows远程桌面访问ubuntu12.04

    转载自 : http://blog.csdn.net/shuzui1985/article/details/7592569 1.dashboard----桌面共享 我们共享所使用的协议是rdp,所以我 ...

  7. css实现九宫格图片自适应布局

    我之前写九宫格自适应布局的时候,每个格子是使用媒体查询器(@media)或者js动态设置css,根据不同的手机屏幕宽度,适配不同手机,但是这样有个很大的缺点,那就是移动端的屏幕尺寸太多了,就得写很多代 ...

  8. HIT 1917 2—SAT

    题目大意:一国有n个党派,每个党派在议会中都有2个代表, 现要组建和平委员会,要从每个党派在议会的代表中选出1人,一共n人组成和平委员会. 已知有一些代表之间存在仇恨,也就是说他们不能同时被选为和平委 ...

  9. 单选按钮 JradioButton 和复选框 JcheckBox 的使用

    package first; import javax.swing.*; import java.awt.*; import java.awt.event.*; class BRTest extend ...

  10. [bzoj1823][JSOI2010]满汉全席——2-SAT

    题目大意 题目又丑又长我就不贴了,说一下大意,有n种菜,m个评委,每一个评委又有两种喜好,每种菜有满汉两种做法,只能选一种.判断是否存在一种方案使得所有评委至少喜欢一种菜品.输入包含多组数据. 题解 ...