zoukankan      html  css  js  c++  java
  • poj 1330 Nearest Common Ancestors (LCA)

    Nearest Common Ancestors
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 16530   Accepted: 8839

    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在线算法模板题:

     1 //3304K    47MS    C++    1778B    2014-04-15 15:45:4
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<math.h>
     5 #define N 10005
     6 struct node{
     7     int u,v;
     8     int next; 
     9 } edge[2*N];
    10 int n,num,u,v;
    11 int set[2*N],dep[2*N];
    12 int rank[N],head[N],vis[N];
    13 int dp[2*N][30],in[N]; 
    14 void addedge(int u,int v)
    15 {
    16     edge[num].u=u;
    17     edge[num].v=v;
    18     edge[num].next=head[u];
    19     head[u]=num++;
    20 }
    21 int Min(int a,int b)
    22 {
    23     return dep[a]<dep[b]?a:b;
    24 }
    25 void dfs(int u,int deep)
    26 {
    27     vis[u]=1;
    28     rank[u]=++num;
    29     dep[num]=deep;
    30     set[num]=u;
    31     for(int i=head[u];i!=-1;i=edge[i].next){
    32         int v=edge[i].v;
    33         if(vis[v]) continue;
    34         dfs(v,deep+1);
    35         set[++num]=u;
    36         dep[num]=deep; 
    37     } 
    38 }
    39 void init()
    40 {
    41     int nn=2*n-1;
    42     int m=(int)(log(1.0*nn)/log(2.0));
    43     for(int i=1;i<=nn;i++)
    44         dp[i][0]=i;
    45     for(int j=1;j<=m;j++)
    46         for(int i=1;i+(1<<j)-1<=nn;i++)
    47             dp[i][j]=Min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
    48 }
    49 int RMQ(int l,int r)
    50 {
    51     int m=(int)(log((r-l+1)*1.0)/log(2.0));
    52     return Min(dp[l][m],dp[r-(1<<m)+1][m]); 
    53 }
    54 int LCA(int u0,int v0)
    55 {
    56     int x=rank[u0];
    57     int y=rank[v0];
    58     if(x>y) return set[RMQ(y,x)];
    59     else return set[RMQ(x,y)];
    60 }
    61 int main(void)
    62 {
    63     int t;
    64     scanf("%d",&t);
    65     while(t--)
    66     {
    67         scanf("%d",&n);
    68         memset(in,0,sizeof(in));
    69         memset(head,-1,sizeof(head));
    70         num=0;
    71         for(int i=1;i<n;i++){
    72             scanf("%d%d",&u,&v);
    73             addedge(u,v);
    74             addedge(v,u);
    75             in[v]++;
    76         }
    77         int root=0;
    78         while(in[++root]);
    79         scanf("%d%d",&u,&v);
    80         memset(vis,0,sizeof(vis));
    81         num=0;
    82         dfs(root,1);
    83         init();
    84         printf("%d
    ",LCA(u,v));
    85     }
    86     return 0;
    87 } 

    Tarjan 离线算法:

     1 //1036K    79MS    C++    1243B    2014-04-15 16:38:17
     2 #include<iostream>
     3 #include<queue>
     4 #include<vector>
     5 #define N 10005
     6 using namespace std;
     7 vector<int>V[N];
     8 int set[N];
     9 int vis[N];
    10 int dp[N][30];
    11 int n,u0,v0,ans;
    12 void init()
    13 {
    14     for(int i=0;i<=n;i++) V[i].clear();
    15     memset(vis,0,sizeof(vis));  
    16 }
    17 int find(int x)
    18 {
    19     if(set[x]!=x) set[x]=find(set[x]);
    20     return set[x];
    21 }
    22 void merge(int a,int b)
    23 {
    24     int x=find(a);
    25     int y=find(b);
    26     set[y]=x;
    27 }
    28 void LCA(int u)
    29 {
    30     set[u]=u;
    31     vis[u]=1;
    32     if(u==u0 && vis[v0]){
    33         ans=find(v0);
    34     }
    35     else if(u==v0 && vis[u0]){
    36         ans=find(u0);
    37     }
    38     for(int i=0;i<V[u].size();i++){
    39         if(!vis[V[u][i]]){
    40             LCA(V[u][i]);
    41             merge(u,V[u][i]);
    42             set[V[u][i]]=u;
    43         }
    44     }
    45 }
    46 int main(void)
    47 {
    48     int t;
    49     int a,b;
    50     scanf("%d",&t);
    51     while(t--)
    52     {
    53         init();
    54         int in[N]={0};
    55         scanf("%d",&n);
    56         for(int i=1;i<n;i++){
    57             scanf("%d%d",&a,&b);
    58             V[a].push_back(b);
    59             V[b].push_back(a);
    60             in[b]++;
    61         }
    62         scanf("%d%d",&u0,&v0);
    63         int id=0;
    64         while(in[++id]);
    65         LCA(id);
    66         printf("%d
    ",ans);
    67     }
    68     return 0;
    69 }
  • 相关阅读:
    Graph neural networks: A review of methods and applications文献阅读
    IMBD数据集处理
    GNN知识整理(二)
    GNN认识整理(一)
    Linux中python中的#!/usr/bin/python
    Linux下运行g++
    itextpdf7自写算法的表格展示 制表符
    itext7 List序号 有序列表 解决中文不显示
    java使用itextpdf7实现导出pdf表格;java使用itextpdf7实现pdf加水印
    csv导出导入工具类 commons-csv导出
  • 原文地址:https://www.cnblogs.com/GO-NO-1/p/3666506.html
Copyright © 2011-2022 走看看