zoukankan      html  css  js  c++  java
  • 数据结构 最小子树问题 (模拟)

    Description

    已知在以二叉链表存储的二叉树t中,p和q为二叉树中任意输入的两个不同的结点,试编写一个算法,求包含结点p和q的最小子树。

    Input

    输入样例有多组,每组第一行两个数n,q,分别表示树的结点个数和询问个数。结点编号从1到n。接下来的n–1行按层序输入n-1条边,每一行输入两个数u、v,表示u是v的父亲结点。输入保证是一棵二叉树。再接下来输入q行查询,每行两个不同的整数a,b,表示查询包含ab点的最小子树。 2<= n <= 50000, 1 <= q <= 20000。

    Output

    对于每个查询,输出最小子树的树根结点编号。

    Sample Input

    7 5
    1 2
    1 3
    2 4
    2 5
    3 6
    3 7
    1 2
    2 5
    4 5
    4 7
    1 7

    Sample Output

    1
    2
    2
    1
    1

    HINT

    Append Code

    析:很容易知道,这个最小子树的根结点就是这两个点最近的公共祖先,首先我们先把这个树记录下来,是由子结点指向父结点,然后在找公共祖先时,先用另一个数组,

    来记录一个结点的所有祖先,也就是从下向上遍历,然后再用另一个结点也从向下向上遍历,如果发现这个结点已经被访问过,那么就是这个结点,由于树的深度不大了,

    应该不会超时。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    //#include <tr1/unordered_map>
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    //using namespace std :: tr1;
     
    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const LL LNF = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 5e4 + 5;
    const LL mod = 10000000000007;
    const int N = 1e6 + 5;
    const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
    const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
    const int hr[]= {-2, -2, -1, -1, 1, 1, 2, 2};
    const int hc[]= {-1, 1, -2, 2, -2, 2, -1, 1};
    const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    inline LL gcd(LL a, LL b){  return b == 0 ? a : gcd(b, a%b); }
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline int Min(int a, int b){ return a < b ? a : b; }
    inline int Max(int a, int b){ return a > b ? a : b; }
    inline LL Min(LL a, LL b){ return a < b ? a : b; }
    inline LL Max(LL a, LL b){ return a > b ? a : b; }
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
     
    }
    int p[maxn];
    bool vis[maxn];
     
    int solve(int u, int v){
        int x = u;
        vis[u] = true;
        while(p[x] != x){
            vis[x] = true;
            x = p[x];
        }
        vis[x] = true;
        int y = v;
        while(!vis[y]) y = p[y];
        x = u;
        vis[u] = false;
        while(p[x] != x){
            vis[x] = false;
            x = p[x];
        }
        vis[x] = false;
        return y;
    }
     
    int main(){
        while(scanf("%d %d", &n, &m) == 2){
            for(int i = 1; i <= n; ++i)  p[i] = i, vis[i] = false;
            int u, v;
            for(int i = 1; i < n; ++i){
                scanf("%d %d", &u, &v);
                p[v] = u;
            }
            for(int i = 0; i < m; ++i){
                scanf("%d %d", &u, &v);
                printf("%d
    ", solve(u, v));
            }
        }
        return 0;
    }
    
  • 相关阅读:
    跟着太白老师学python 10day 函数的动态参数 *args, **kwargs, 形参的位置顺序
    IO 流之字符流的缓冲区
    Java IO异常处理方式
    Java IO 流
    Java 其他对象的 API
    Java 集合框架之 JDK 1.5 新特性
    Java 集合框架工具类
    Java 集合框架之 Map
    Java 集合框架查阅技巧
    Java 集合框架之泛型
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/5990749.html
Copyright © 2011-2022 走看看