POJ1330 Nearest Common Ancestors (tarjan离线求LCA模板)

11 篇文章 0 订阅
2 篇文章 0 订阅

原题点这里 Time Limit: 1000MS Memory Limit: 10000K

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

题目:

输入一个T,下面拥有T个数据,每一个数据开头是n,下面n-1行输入x y,表明x是y的父节点
再输入一行a b,询问a和b的最近公共祖先

一道典型的LCA题目(典型到就是模板
然而,还是浪费了我两个半小时
最后找到的问题呢
就是很多东西都没有清零
因为题目有多组数据
所以每次都一定要记得将各个数组变为初始状态
最好写一个函数,之后可以直接调用而且不会忘记
真的真的很重要啊,为什么呢,不多说看图
这里写图片描述
除了最上方是自己过的,其他的AC都是为了验证别人的题解
其实最开始的思路其实已经离最终答案差不多了
就是有数组没有清零

代码

#include<cstdio>
#include<cstring>
using namespace std;
int fa[10005],n,tot,h[10005],x,y;
struct node{
    int v,next;
}e[20005];
bool vis[10005];
bool r[10005];
void add(int from,int to)
{
    e[++tot].v=to;
    e[tot].next=h[from];
    h[from]=tot;
}
int find(int x)
{
    if(fa[x]!=x)fa[x]=find(fa[x]);
    return fa[x];
}
void tarjan(int r)
{
    if(x==0||y==0) return;//防止超时 
    vis[r]=1;
    for(int i=h[r];i;i=e[i].next)
    {
        if(!vis[e[i].v])
        {
            tarjan(e[i].v);//如果没有遍历的话就要遍历该点 
            fa[e[i].v]=r;
        }
    }
    if(r==x)//当目前的点是询问的一个点 
    {
        if(vis[y])//如果另外一个点已经访问的话 
        {
            printf("%d\n",find(y));//直接输出它现在的祖先 
            y=0;return;
        }
    }
    if(r==y)//同上 
    {
        if(vis[x])
        {
            printf("%d\n",find(x));
            x=0; return;
        }
    }
}
void init()
{
    tot=0;
    memset(vis,0,sizeof(vis));
    memset(h,0,sizeof(h));
    memset(r,0,sizeof(r));
} // 很重要很重要 
void work()
{
    scanf("%d",&n);
    init();//每次一定要记得清零 
    int root;
    for(int i=1;i<n;i++)
    {
        scanf("%d%d",&x,&y);
        add(x,y);//由题得爸爸儿子关系已经固定了 
        r[y]=1;
    }
    for(int i=1;i<=n;i++) 
    {
        if(!r[i])root=i;//找根节点 
        fa[i]=i;//父节点也要记得每次都要改回去 
    }
    scanf("%d%d",&x,&y);
    tarjan(root);
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++) work();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值