zoukankan      html  css  js  c++  java
  • Codeforces Round #294 (Div. 2) E. A and B and Lecture Rooms(lca+思维,树上寻找与给定两个点距离相等的点的个数)

    题目链接

    E. A and B and Lecture Rooms
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 - 1corridors 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
    Note

    in the first sample there is only one room at the same distance from rooms number 2 and 3 — room number 1.

    题意:正如标题,给出n个结点的一棵树,给出m个询问u,v,问树上距离u和v距离相等的点的个数。

    题解:

    这题用到了求lca倍增的思想,先随便定一个根变成有根树,预处理每个结点的子树中结点个数,然后对询问u,v,得到它们的lca,记为f,然后可以求u-v路径上的“中点”,利用倍增求lca时往上跳的那段代码,然后随便搞搞。

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<vector>
    #include<queue>
    #include<stack>
    using namespace std;
    #define rep(i,a,n) for (int i=a;i<n;i++)
    #define per(i,a,n) for (int i=n-1;i>=a;i--)
    #define pb push_back
    #define fi first
    #define se second
    typedef vector<int> VI;
    typedef long long ll;
    typedef pair<int,int> PII;
    const int inf=0x3fffffff;
    const ll mod=1000000007;
    const int maxn=1e5+100;
    int head[maxn];
    int deep[maxn],fa[maxn][18];
    int f[maxn];
    struct edge
    {
        int from,to,next;
    }e[maxn*2];   //
    int tol=0;
    void add(int u,int v)
    {
        e[++tol].to=v,e[tol].next=head[u],head[u]=tol;
    }
    void bfs(int rt)
    {
        queue<int>q;
        deep[rt]=0;
        fa[rt][0]=rt;
        q.push(rt);
        while(!q.empty())
        {
            int t=q.front();
            q.pop();
            for(int i=1;i<=17;i++)
                fa[t][i] = fa[fa[t][i-1]][i-1];
            for(int i = head[t];i;i=e[i].next)
            {
                int v = e[i].to;
                if(v==fa[t][0])continue;
                deep[v]=deep[t]+1;
                fa[v][0]=t;
                q.push(v);
            }
        }
    }
    
    int lca(int u,int v)
    {
        if(deep[u]>deep[v])swap(u,v);
        int hu=deep[u],hv=deep[v];
        int tu=u,tv=v;
        for(int det = hv-hu, i = 0; det ;det>>=1, i++)
            if(det&1)
                tv = fa[tv][i];
        if(tu==tv)
            return tu;
        for(int i =17; i>=0; i--)
        {
            if(fa[tu][i] == fa[tv][i]) continue;
            tu = fa[tu][i];
            tv = fa[tv][i];
        }
        return fa[tu][0];
    }
    
    void dfs(int u,int fa)
    {
        f[u]=1;
        for(int i=head[u];i;i=e[i].next)
        {
            int v=e[i].to;
            if(v==fa) continue;
            dfs(v,u);
            f[u]+=f[v];
        }
    }
    
    int main()
    {
        int n;
        scanf("%d",&n);
        rep(i,1,n)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            add(u,v),add(v,u);
        }
        int m;
        scanf("%d",&m);
        bfs(1);
        dfs(1,0);
        while(m--)
        {
            int u,v;
            int ans=0;
            scanf("%d%d",&u,&v);
            if(u==v)
            {
                printf("%d
    ",n);
                continue;
            }
            int f1=lca(u,v);
            int d1=deep[u]+deep[v]-2*deep[f1],d2=deep[u]-deep[f1],d3=deep[v]-deep[f1];
            if(d1&1)
            {
                puts("0");
                continue;
            }
            if(d2==d3)
            {
                int dd=d2-1;
                int tu=u;
                for(int det=dd,i=0;det;det>>=1,i++)
                    if(det&1)
                        tu=fa[tu][i];
                int tv=v;
                for(int det=dd,i=0;det;det>>=1,i++)
                    if(det&1)
                        tv=fa[tv][i];
                ans=n-f[tv]-f[tu];
            }
            else
            {
                if(d2>d3)
                {
                    int d=d1/2;
                    int tv=u;
                    for(int det=d,i=0;det;det>>=1,i++)
                        if(det&1)
                            tv=fa[tv][i];
                    int dd=d1/2-1; //
                    int tu=u;
                    for(int det=dd,i=0;det;det>>=1,i++)
                        if(det&1)
                            tu=fa[tu][i];
                    ans=f[tv]-f[tu];
                }
                else if(d2<d3)
                {
                    int d=d1/2;
                    int tv=v;
                    for(int det=d,i=0;det;det>>=1,i++)
                        if(det&1)
                            tv=fa[tv][i];
                    int dd=d1/2-1; //
                    int tu=v;
                    for(int det=dd,i=0;det;det>>=1,i++)
                        if(det&1)
                            tu=fa[tu][i];
                    ans=f[tv]-f[tu];
                }
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    扩增子分析解读7物种分类统计 筛选进化树和其它
    R函数详解
    扩增子统计绘图1箱线图:Alpha多样性
    扩增子分析解读6进化树 Alpha Beta多样性
    扩增子分析解读5物种注释 OTU表操作
    扩增子分析解读4去嵌合体 非细菌序列 生成代表性序列和OTU表
    扩增子分析解读3格式转换 去冗余 聚类
    扩增子分析解读2提取barcode 质控及样品拆分 切除扩增引物
    执行join_paired_ends.py报错Cannot find fastq-join
    扩增子分析解读1质控 实验设计 双端序列合并
  • 原文地址:https://www.cnblogs.com/tarjan/p/7224176.html
Copyright © 2011-2022 走看看