POJ 1330 Nearest Common Ancestors:lca entry question

Source: Internet
Author: User
Tags bool int size integer printf time limit

Link:
http://poj.org/problem?id=1330

Topic:

Nearest Common Ancestors
Time limit:1000ms Memory limit:10000k
Total submissions:12678 accepted:6764

Description
A rooted tree are a well-known data structure in computer and engineering. An example is shown below:

The

In the figure, which is labeled with a integer from {1, 2,..., 16}. Node 8 is the root of the tree. Node x is a ancestor of node y if node x is in the path between the root and node Y. For example, node 4 was an ancestor of node 16. Node also an ancestor of node 16. As a matter of fact, nodes 8, 4, and are the ancestors of node 16. Remember that's a node is a 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 a ancestor of Node Z. Thus, nodes 8 and 4 are the common ancestors of nodes and 7. A node x is called the nearest common ancestor of nodes Y and Z if x are a common ancestor of Y and Z and nearest to Y Z among their common ancestors. Hence, the nearest common ancestor of nodes and 7 is node 4. Node 4 is nearer to nodes and 7 than node 8 is.

For other examples, the nearest common ancestor of nodes 2 and 3 are node, the nearest common ancestor of nodes 6 and 13 is node 8, and the nearest common ancestor of nodes 4 and are node 4. In the last example, if y are an ancestor of Z, then the nearest common ancestor of Y and Z are y.

Write A program This 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 the 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 then represent an edge--the-A is the parent node of T He second an integer. Note "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 are to be computed.

Output
Print exactly one line for each test case. The line should contain the "is" the nearest common ancestor.

Sample Input

2
16
1 14
8 5
10 16
5 9
5 W
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
Taejon 2002

Analysis and Summary:

An introductory question on the recent public ancestor (LCA) problem, Tarjan off-line LCA algorithm.

1.Tarjan off-line request for LCA

#include <cstdio> #include <cstring> const int VN = 10005;  
int q1,q2;  
    
BOOL Flag;  
        Class tarjan_lca{public:void init (int _n) {n=_n; size=0;  
        Memset (Head,-1, sizeof (head));  
        memset (Vis, 0, sizeof (VIS));  
        memset (indegree, 0, sizeof (indegree));  
        memset (ancestor, 0, sizeof (ancestor));  
    Make_set (n);  
        void insert (int u, int v) {e[size].v = v;  
        E[size].next = Head[u];  
        Head[u] = size++;  
    ++INDEGREE[V];   
        } void Tarjan_lca () {for (int i=1; i<=n; ++i) if (!indegree[i)) {DFS (i);  
    } private:void make_set (int _n) {for (int i=0; i<=n; ++i) rank[i]=0,f[i]=i;  
        int find (int x) {int i, j=x;  
        while (J!=f[j]) j=f[j];  
        while (X!=J) {i=f[x]; f[x]=j x=i;}  
    Return J;  } void Union (int x,int y) {int a=find (x), B=find (y);

        if (a==b) return;  
        if (rank[a]>rank[b]) f[b]=a;  
            else{if (rank[a]==rank[b]) ++rank[b];  
        F[a]=b;  
        } void Dfs (int u) {if (flag) return;  
        Ancestor[find (u)] = u;  
            for (int e=head[u]; e!=-1 e=e[e].next) {dfs (E[E].V);  
            Union (U, E[E].V);  
        Ancestor[find (E[E].V)] = u;  
        } Vis[u] = true;  
            if (U==q1&&vis[find (Q2)]) {flag=true;  
        printf ("%d\n", Ancestor[find (q2));  
            else if (U==q2&&vis[find (Q1)]) {flag=true;  
        printf ("%d\n", Ancestor[find (Q1));  
    } private:struct edge{int V, next;  
    
    }E[VN];  
    int n;  
    int size;  
    int ANCESTOR[VN];  
    int FATHER[VN];     int HEAD[VN]; adjacency table header int INDEGREE[VN];     The degree of entry of each node bool VIS[VN];      
    node staining int F[VN]; int RANK[VN];  
    
};  
    
Tarjan_lca G;  
    int main () {int t,n,m,u,v;  
    scanf ("%d", &t);  
        while (t--) {scanf ("%d", &n);  
        G.init (n);  
            for (int i=0; i<n-1; ++i) {scanf ("%d%d", &u,&v);  
        G.insert (U,V);  
        } scanf ("%d%d", &AMP;Q1,&AMP;Q2);  
        Flag=false;  
    G.tarjan_lca ();  
return 0; }

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.