1. 程式人生 > >poj-1330 Nearest Common Ancestors

poj-1330 Nearest Common Ancestors

AS PE ram stdin tinc contain repr can node

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

LCA模板題

//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
typedef long long ll;
#define inf 2147483647
const ll INF = 0x3f3f3f3f3f3f3f3fll;
#define ri register int
template <class T> inline T min(T a, T b, T c)
{
    return min(min(a, b), c);
}
template <class T> inline T max(T a, T b, T c)
{
    return max(max(a, b), c);
}
template <class T> inline T min(T a, T b, T c, T d)
{
    return min(min(a, b), min(c, d));
}
template <class T> inline T max(T a, T b, T c, T d)
{
    return max(max(a, b), max(c, d));
}
#define pi acos(-1)
#define me(x, y) memset(x, y, sizeof(x));
#define For(i, a, b) for (int i = a; i <= b; i++)
#define FFor(i, a, b) for (int i = a; i >= b; i--)
#define bug printf("***********\n");
#define mp make_pair
#define pb push_back
const int N = 1e5+5;
const int M=200005;
// name*******************************
struct edge
{
    int to,nxt,idx;
} e[N<<1],e_[N];
int tot=1;
int tot_=0;
int fa[N];
int fst[N];
int fst_[N];
int rk[N];
int n,m,T;
int acr[N];
int ins[N];
bool flag[N];
int ans[N];
// function******************************
void init()
{
    tot=1;
    tot_=0;
    me(ins,0);
    For(i,1,n)fa[i]=i;
    me(rk,0);
    me(acr,0);
    me(fst,0);
    me(fst_,0);
    me(flag,0);
}
void add(int u,int v)
{
    e[++tot].to=v;
    e[tot].nxt=fst[u];
    fst[u]=tot;
}
void add_(int u,int v,int t)
{
    e_[++tot_].to=v;
    e_[tot_].nxt=fst_[u];
    e_[tot_].idx=t;
    fst_[u]=tot_;
}
int find(int x)
{
    return x==fa[x]?x:fa[x]=find(fa[x]);
}
void uone(int a,int b)
{
    int t1=find(a),t2=find(b);
    if(t1!=t2)
    {
        if(rk[t1]>rk[t2])fa[t2]=t1;
        else fa[t1]=t2;
        if(rk[t1]==rk[t2])rk[t2]++;
    }
}
void LCA(int u)
{
    acr[u]=u;
    ins[u]=1;
    for(int p=fst[u]; p; p=e[p].nxt)
    {
        int v=e[p].to;
        if(ins[v])continue;
        LCA(v);
        uone(u,v);
        acr[find(u)]=u;
    }
    for(int p=fst_[u]; p; p=e_[p].nxt)
    {
        int v=e_[p].to;
        if(ins[v])ans[e_[p].idx]=acr[find(v)];
    }
}

//***************************************
int main()
{
//    ios::sync_with_stdio(0);
//    cin.tie(0);
    // freopen("test.txt", "r", stdin);
    //  freopen("outout.txt","w",stdout);
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        int u,v;
        init();
        For(i,1,n-1)
        {
            scanf("%d%d",&u,&v);
            add(u,v);
            add(v,u);
            flag[v]=1;
        }
        For(i,1,1){
        scanf("%d%d",&u,&v);
        add_(u,v,i);
        add_(v,u,i);
        }
        int pos;
        For(i,1,n)
        if(!flag[i])
        {
            pos=i;
            break;
        }

        LCA(pos);
        For(i,1,1)
        printf("%d\n",ans[i]);

    }
    return 0;
}

poj-1330 Nearest Common Ancestors