• A and B and Lecture Rooms(LCA)


    题目描述

    A and B are preparing themselves for programming contests.

    The University where A and B study is a set of rooms connected by corridors. Overall, the University has n rooms connected by n - 1 corridors so that you can get from any room to any other one by moving along the corridors. The rooms are numbered from 1 to n.

    Every day А and B write contests in some rooms of their university, and after each contest they gather together in the same room and discuss problems. A and B want the distance from the rooms where problems are discussed to the rooms where contests are written to be equal. The distance between two rooms is the number of edges on the shortest path between them.

    As they write contests in new rooms every day, they asked you to help them find the number of possible rooms to discuss problems for each of the following m days.

    Input

    The first line contains integer n (1 ≤ n ≤ 105) — the number of rooms in the University.

    The next n - 1 lines describe the corridors. The i-th of these lines (1 ≤ i ≤ n - 1) contains two integers ai and bi (1 ≤ ai, bi ≤ n), showing that the i-th corridor connects rooms ai and bi.

    The next line contains integer m (1 ≤ m ≤ 105) — the number of queries.

    Next m lines describe the queries. The j-th of these lines (1 ≤ j ≤ m) contains two integers xj and yj (1 ≤ xj, yj ≤ n) that means that on the j-th day A will write the contest in the room xj, B will write in the room yj.

    Output

    In the i-th (1 ≤ i ≤ m) line print the number of rooms that are equidistant from the rooms where A and B write contest on the i-th day.

    Examples

    Input
    4
    1 2
    1 3
    2 4
    1
    2 3
    Output
    1
    Input
    4
    1 2
    2 3
    2 4
    2
    1 2
    1 3
    Output
    0
    2
    解析:
    给出树上两点u,v,找到与u,v距离相等的点的个数
    先找到u,v的公共祖先r,然后知道u,v间的距离,中间位置为mid;
    当然若dis(u,v)为奇数则无解;
    将mid的位置进行讨论:
    mid在r上:
    mid不在r上:
    //毒瘤题 
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    #include<iostream>
    #include<vector>
    #include<queue>
    using namespace std;
    vector<int>G[140000];
    int n,m;
    int size[140000],grand[140000][21],deep[140000];
    //数据范围、数组范围要注意 
    void build(int x,int pre)
    {
        for(int i=1;i<=20;i++)
        {
            grand[x][i]=grand[grand[x][i-1]][i-1];
        }
        size[x]=1;
        for(int i=0;i<G[x].size();i++)
        {
            int v=G[x][i];
            if(v==pre)continue;
            deep[v]=deep[x]+1;
            grand[v][0]=x;
            build(v,x);
            size[x]+=size[v];
        }
    }
    int lca(int u,int v)
    {
        if(deep[u]<deep[v])swap(u,v);
        int dis=deep[u]-deep[v];
    /////////////////////////////////// 
        for(int k=0;k<19;k++){
            if((dis>>k)&1){    
                u=grand[u][k];
            }
        }
        //把u调到和v同一深度 
    /////////////////////////////////// 
        if(u==v)return u;//当且只当u、v处于一条链时 
        for(int k=19;k>=0;k--){
            if(grand[u][k]!=grand[v][k]){
                u=grand[u][k];
                v=grand[v][k];
            }
        }
        return grand[u][0];
    }
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<n;i++)
        {
            int u,v;scanf("%d%d",&u,&v);
            G[u].push_back(v);
            G[v].push_back(u);//vector可以...节约代码量 
        }
        build(1,0);//建树 
        scanf("%d",&m);//m个询问 
        for(int i=1;i<=m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            int r=lca(u,v);
            int dis=deep[u]+deep[v]-2*deep[r];
            if(dis&1){printf("0
    ");}
            if(deep[u]>deep[v])swap(u,v);
            else
            {
                dis/=2;
    /////////////////////////////////////////////////
                int mid=v;
                for(int k=19;k>=0;k--){
                    if((dis>>k)&1) mid=grand[mid][k];
                }
    /////////////////////////找到 mid 
                int ans=1;
                if(mid==r)
                {
                    int preu=u,prev=v;
                    int du=deep[u]-deep[r];du--;
                    int dv=deep[v]-deep[r];dv--;
                    for(int k=19;k>=0;k--){
                        if((du>>k)&1) preu=grand[preu][k];
                        if((dv>>k)&1) prev=grand[prev][k];
                    }
                    ans=n-size[preu]-size[prev];
                }
                else
                {
                    int prev=v,preu=u;
                    int dv=deep[v]-deep[mid];
                    dv--;
                    for(int k=19;k>=0;k--){
                        if((dv>>k)&1) prev=grand[prev][k];
                    }
                    ans=size[mid]-size[prev];
                }
                cout<<ans<<endl;
            }
        }
    }
  • 相关阅读:
    PHP无限极分类生成树方法
    如何用c#本地代码实现与Webbrowser中的JavaScript交互
    在sqlite中,如何删除字段? how to drop a column in sqlite
    如何在centos下配置redis开机自启动
    WinForm中的图表控件Chart
    XPath高级用法(冰山一角)
    windows下MySQL的安装(非安装包)
    WinFrom下Webbrowser加载自定义页面的技巧
    .net下使用最小堆实现TopN算法
    .net程序中http请求的超时配置
  • 原文地址:https://www.cnblogs.com/719666a/p/10226317.html
Copyright © 2020-2023  润新知