POJ 1330 Nearest Common Ancestors(Tree)

题目:Nearest Common Ancestors

  根据输入建立树,然后求2个结点的最近共同祖先。

  注意几点:

  (1)记录每个结点的父亲,比较层级时要用;

  (2)记录层级;

  (3)记录每个结点的孩子,vector<int> v[M]写在主函数里面,放在外面超时

代码:

#include<iostream>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;

const int M = 10001;
//vector<int> v[M];
int level[M];
int father[M];

void visitEveryone(vector<int> v[], int one, int _level)
{
    level[one] = _level;
    for(vector<int>::iterator it = v[one].begin(); it != v[one].end(); ++it)
    {
        visitEveryone(v, *it, _level+1);
    }
}

int main()
{
    int T,N,a,b,i,j;
    scanf("%d", &T);
    while(T--)
    {
        vector<int> v[M];
        scanf("%d", &N);
        memset(father, 0, (N+1)*sizeof(int));
        for(i = 1; i < N; ++i)
        {
            scanf("%d %d", &a, &b);
            father[b] = a;
            v[a].push_back(b);
        }
        scanf("%d %d", &a, &b);
        for(j=1; father[j] > 0 && j <= N; ++j);

        visitEveryone(v, j, 0);        

        while(a != b)
        {

            if(level[a] < level[b])
                b = father[b];
            else
                a = father[a];
        }         

        printf("%d\n",a);
    }
    return 0;
}
时间: 2024-10-08 13:20:48

POJ 1330 Nearest Common Ancestors(Tree)的相关文章

POJ 1330 Nearest Common Ancestors(树)

Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17628   Accepted: 9335 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In the figure, each

[POJ 1330] Nearest Common Ancestors (朴素方法)

POJ 1330: Nearest Common Ancestors Time Limit: 1000ms Memory Limit: 32Mb 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 fro

POJ - 1330 Nearest Common Ancestors(基础LCA)

POJ - 1330 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 t

POJ题目1330 Nearest Common Ancestors(LCA)

Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20488   Accepted: 10785 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In the figure, each

POJ 1330 Nearest Common Ancestors(最近公共祖先 Tarjan离线)

题目链接:http://poj.org/problem?id=1330 题目: 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 ro

POJ 1330 Nearest Common Ancestors(LCA Tarjan算法)

题目链接:http://poj.org/problem?id=1330 题意:给定一个n个节点的有根树,以及树中的两个节点u,v,求u,v的最近公共祖先. 数据范围:n [2, 10000] 思路:从树根出发进行后序深度优先遍历,设置vis数组实时记录是否已被访问. 每遍历完一棵子树r,把它并入以r的父节点p为代表元的集合.这时判断p是不是所要求的u, v节点之一,如果r==u,且v已访问过,则lca(u, v)必为v所属集合的代表元.p==v的情况类似. 我的第一道LCA问题的Tarjan算法

poj 1330 Nearest Common Ancestors (最简单的LCA)

题意: 给出一棵树的结构. 给出两个点X和Y,求它俩的LCA. 思路: 只需求两个点的LCA,用了两种方法,一种离线tarjan,一种直接搞. 看代码. 代码: 方法一:直接搞. int const maxn = 10005; int T,n,a,b; int fa[maxn]; int X,Y; int main(){ cin>>T; while(T--){ scanf("%d",&n); mem(fa,-1); rep(i,1,n-1){ scanf("

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

题目同上篇,最近公共祖先. 因为没有清零tot,RE了好多次TAT 一定要初始化啊!! 1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #include<iostream> 5 using namespace std; 6 int root,head[10010]; 7 int next[10010],to[10010],tot; 8 int deep[10010],n; 9 int p[

POJ 1330 Nearest Common Ancestors LCA(在线RMQ,离线Tarjan)

链接:http://poj.org/problem?id=1330 题意:只看题目就知道题目是什么意思了,最近公共祖先,求在一棵树上两个节点的最近公共祖先. 思路:求最近公共祖先有两种算法,在线和离线,在线方法是用RMQ求LCA,一句话总结就是在从DFS时,从第一个点到第二个点的最短路径中深度最浅的点就是公共祖先,用RMQ处理,一般问题的最优解决方式的复杂度是O(NlogN)的预处理+N*O(1)的查询.离线方法是Tarjan算法,将所有询问的两个点都记录下来,在DFS过程中不断将每个点自身作为