zoukankan      html  css  js  c++  java
  • poj 1330

    Nearest Common Ancestors
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 18598   Accepted: 9860

    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

    此题思路很多,并查集什么的,BFS等,我们主要是利用这些给出的边来利用合理的存储结构建树,以便利我们查找最近的公共父节点,于是我们想到将每个节点分层,并且记录记录每个节点其最近的父节点,然后通过DFS为每个节点维护一个层次信息,这样之后,当给出X,Y,我们只需要分析其层次高低,决定沿着谁的父节点走,直到x==y就找到了最近的公共父节点了。
     
     1 #include<iostream>
     2 #include<vector>
     3 #include<cstdio>
     4 #include<cstring>
     5 using namespace std;
     6 const int maxnum = 10000;
     7 
     8 vector<int> Son[maxnum];  //存储每个节点的孩子
     9 int level[maxnum];  // 存储每个节点的层次
    10 int parent[maxnum]; //存储每个节点的父亲节点
    11 
    12 
    13 void DFS(int number,int deep)
    14 {
    15     level[number]=deep;
    16     for(vector<int>::iterator it = Son[number].begin();it!=Son[number].end();it++)
    17         DFS(*it,deep+1);
    18 }
    19 
    20 int main()
    21 {
    22     int t;
    23     while(scanf("%d",&t)!=EOF)
    24     {
    25         while(t--)
    26         {
    27             int num;
    28             scanf("%d",&num);
    29             int dad,son;
    30             for(int i=0;i<maxnum;i++)
    31                 Son[i].clear();
    32             memset(parent,-1,sizeof(parent));
    33             for(int i=0;i<num-1;i++)
    34             {
    35                 scanf("%d%d",&dad,&son);
    36                 Son[dad-1].push_back(son-1);
    37                 parent[son-1] = dad-1;   //存储每个节点的上一个节点
    38             }
    39             int t=dad-1;
    40             while(parent[t]!=-1)
    41                 t=parent[t];
    42             DFS(t,0);   //遍历分层
    43 
    44             int x,y;
    45             scanf("%d%d",&x,&y);
    46             x=x-1;
    47             y=y-1;
    48             while(x!=y)
    49             {
    50                 if(level[x]<level[y])
    51                     y=parent[y];
    52                 else
    53                     x = parent[x];
    54             }
    55             printf("%d
    ",x+1);
    56 
    57 
    58         }
    59     }
    60     return 0;
    61 }
  • 相关阅读:
    使用IDEA启动Tomcat时出现端口被占用的问题解决办法
    在Navicat中设置id主键为UUID自增
    关于IDEA构建的maven项目:WEB-INF下的jsp移动到webapp下出现404无法访问的问题
    Spring框架学习日志(2/4)
    Spring框架学习日志(1/4)
    jenkins+python+pytest+selenium 自动化执行脚本并发送报告
    Selenium 添加Cookie实现绕过登录流程
    CSS
    集合
    Javaweb初学
  • 原文地址:https://www.cnblogs.com/jhldreams/p/3956665.html
Copyright © 2011-2022 走看看