Nearest Common Ancestors
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 30082   Accepted: 15386

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

Source

 
题目大意:编写一个程序,找到树中两个不同节点的最近的共同祖先。
lca板子
代码:
#include<cstdio>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define N 10100
using namespace std;
vector<int>vec[N];
int x,y,n,t,root;
];
int read()
{
    ,f=; char ch=getchar();
    ; ch=getchar();}
    +ch-'; ch=getchar();}
    return x*f;
}
int dfs(int x)
{
    deep[x]=deep[fa[x][]]+;
    ;fa[x][i];i++)
      fa[x][i+]=fa[fa[x][i]][i];
    ;i<vec[x].size();i++)
     if(!deep[vec[x][i]])
      fa[vec[x][i]][]=x,dfs(vec[x][i]);
}
int lca(int x,int y)
{
    if(deep[x]>deep[y]) swap(x,y);
    ;i>=;i--)
     if(deep[fa[y][i]]>=deep[x])
      y=fa[y][i];
    if(x==y) return x;
    ;i>=;i--)
     if(fa[x][i]!=fa[y][i])
      x=fa[x][i],y=fa[y][i];
    ];
}
void begin()
{
    ;i<=N;i++)
      vec[i].clear();
    memset(fa,,sizeof(fa));
    memset(dad,,sizeof(dad));
    memset(deep,,sizeof(deep));
}
int main()
{
    t=read();
    while(t--)
    {
        n=read();begin();
        ;i<n;i++)
        {
            x=read(),y=read();
            dad[y]=x;
            vec[x].push_back(y);
            vec[y].push_back(x);
        }
        ;i<=n;i++)
         if(!dad[i]) root=i;
        deep[root]=;
        dfs(root);
        x=read(),y=read();
        printf("%d\n",lca(x,y));
    }
    ;
}
 

poj——1330 Nearest Common Ancestors的更多相关文章

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

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

  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 倍增)

    POJ.1330 Nearest Common Ancestors (LCA 倍增) 题意分析 给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b.接下来给出xy,求出xy的lca节 ...

  4. LCA POJ 1330 Nearest Common Ancestors

    POJ 1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24209 ...

  5. POJ 1330 Nearest Common Ancestors(lca)

    POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...

  6. POJ 1330 Nearest Common Ancestors 倍增算法的LCA

    POJ 1330 Nearest Common Ancestors 题意:最近公共祖先的裸题 思路:LCA和ST我们已经很熟悉了,但是这里的f[i][j]却有相似却又不同的含义.f[i][j]表示i节 ...

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

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

  8. POJ 1330 Nearest Common Ancestors (LCA,dfs+ST在线算法)

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

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

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

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

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

随机推荐

  1. Ubuntu 16.04 安装OpenSSH7.4

      前几天突然收到接到网安总队下发通知说我们在aws里面的服务器存在重大漏洞及安全隐患.必须在规定时间内修改.我们收到邮件打开Excel发现这些问题 是由于OpenSSH版本太低导致的.于是便安排紧急 ...

  2. Jsp四个作用域page、request、session和application的区别

    1.简单说 page指当前页面.在一个jsp页面里有效  2.request 指从http请求到服务器处理结束,返回响应的整个过程.在这个过程中使用forward方式跳转多个jsp.在这些页面里你都可 ...

  3. JSON使用讲解

    前端操作json  一.JSON字符串与JSON对象的区别 1. 一个对象以“{”  开始,     “}”结束.   每个“名称”后跟一个“:”(冒号):“‘名称/值’ 对”之间运用 “,”(逗号) ...

  4. bnu 51640 Training Plan DP

    https://www.bnuoj.com/bnuoj/problem_show.php?pid=51640 dp[i][j]表示前j个数,分成了i组,最小需要多少精力. 那么,求解订票dp[i][j ...

  5. 上传txt文件编码格式判断(文本乱码解决方法)

    说明 通过ajax或者浏览上传文本文件,上传时候c#处理时候因为文本格式的创建不同,在获取内容时候会出现中文乱码. 解决方法 通过上传的文件流,判断文件的编码格式,在使用对应的编码格式获取文本内容 # ...

  6. Activity的退出和進入效果

    看了android的源代码和资源文件,终于明白如何去修改设置Dialog和Activity的进入和退出效果了.设置Dialog首先通过 getWindow()方法获取它的窗口,然后通过getAttri ...

  7. CF821B Okabe and Banana Trees

    思路: 暴力枚举. 实现: #include <iostream> #include <cstdio> using namespace std; typedef long lo ...

  8. can't set blob value on that column

    MySQL_Prepared_Statement::setBlob: can't set blob value on that column, MySQL error code:0, SQLState ...

  9. SQL server 上机练习题

    首先创建一个数据库,里面有 登录表 学生表   课程表   选课表 成绩表 1. 查询Student表中的所有记录的Sname.Ssex和Class列.2. 查询教师所有的单位即不重复的Depart列 ...

  10. java虚拟机(六)--垃圾收集器和内存分配策略

    目前没有完美的收集器,不同的厂商.版本的虚拟机提供的垃圾收集器会有很大的差别,用户根据自己应用特点和要求组合出各个年代所使用 的收集器.基于jdk1.7Update14之后的虚拟机. HotSpot的 ...