CodeForces 519E A and B and Lecture Rooms(倍增)

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

Copy

41 21 32 412 3

output

Copy

1

input

Copy

41 22 32 421 21 3

output

Copy

02

题意:给出一棵树,m组询问,询问与点u和点v距离相等的点的个数

题解:首先最容易胡出来的是如果一个点到u和v的距离相等,他们肯定是u到v路径上中点的非u、v链以外的全部子树大小之和然后考虑倍增计算u到v的距离,显然中心会在深度较大的那个点到u与vlca的路径上,高度为距离除二很明显,如果距离为奇数就无解,为偶数则可以从较低的点直接倍增跳上去,得到这个中点的子树中含点u的那个,直接减去至于含点v的肯定是父节点那条,不予考虑因为上面的前提是两点深度不等,所以深度相等的时候要特判,显然u和v都要倍增往上跳,不存在一条路径来自其父亲然后两点相等最好也特判一下,大概就能A了

代码如下:
#include<map>
#include<set>
#include<cmath>
#include<cstdio>
#include<vector>
#include<cctype>
#include<string>
#include<sstream>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int n,m,fa[18][100010],deep[100010],size[100010];
vector<int> g[100010];

void dfs(int now,int f,int dep)
{
    deep[now]=dep;
    fa[0][now]=f;
    size[now]=1;
    for(int i=1;i<=17;i++)
    {
        fa[i][now]=fa[i-1][fa[i-1][now]];
    }
    for(int i=0;i<g[now].size();i++)
    {
        if(g[now][i]==f) continue;
        dfs(g[now][i],now,dep+1);
        size[now]+=size[g[now][i]];
    }
}

int get(int u,int v)
{
    if(u==v) return n;
    if(deep[u]==deep[v])
    {
        for(int i=17;i>=0;i--)
        {
            if(fa[i][u]!=fa[i][v])
            {
                u=fa[i][u];
                v=fa[i][v];
            }
        }
        return n-size[u]-size[v];
    }
    if(deep[u]<deep[v]) swap(u,v);
    int x=u,y=v,dis1=0,dis2=0;
    for(int i=17;i>=0;i--)
    {
        if(deep[fa[i][x]]>=deep[y])
        {
            x=fa[i][x];
            dis1+=pow(2,i);
        }
    }
    if(x==y)
    {
        if(dis1%2==1) return 0;
        int need=dis1/2;
        need--;
        for(int i=17;i>=0;i--)
        {
            if(need&(1<<i))
            {
                u=fa[i][u];
            }
        }
        return size[fa[0][u]]-size[u];
    }
    else
    {
        for(int i=17;i>=0;i--)
        {
            if(fa[i][x]!=fa[i][y])
            {
                x=fa[i][x];
                dis1+=pow(2,i);
                y=fa[i][y];
                dis2+=pow(2,i);
            }
        }
        if((dis1+dis2+2)%2==1) return 0;
        int need=(dis1+dis2+2)/2;
        need--;
        for(int i=17;i>=0;i--)
        {
            if(need&(1<<i))
            {
                u=fa[i][u];
            }
        }
//        printf("nowu:%d\n",u);
        return size[fa[0][u]]-size[u];
    }
}

int main()
{
    scanf("%d",&n);
    int from,to;
    for(int i=1;i<=n-1;i++)
    {
        scanf("%d%d",&from,&to);
        g[from].push_back(to);
        g[to].push_back(from);
    }
    dfs(1,0,1);
    scanf("%d",&m);
    while(m--)
    {
        scanf("%d%d",&from,&to);
        printf("%d\n",get(from,to));
    }
}


原文地址:https://www.cnblogs.com/stxy-ferryman/p/9363209.html

时间: 2024-10-05 10:53:52

CodeForces 519E A and B and Lecture Rooms(倍增)的相关文章

Codeforces 519E A and B and Lecture Rooms [倍增法LCA]

题意: 给你一棵有n个节点的树,给你m次询问,查询给两个点,问树上有多少个点到这两个点的距离是相等的.树上所有边的边权是1. 思路: 很容易想到通过记录dep和找到lca来找到两个点之间的距离,然后分情况讨论. 一开始困扰我的问题是如果lca不是正中间的点,如何在比较低的复杂度的层面上求解中点. 倍增法lca不光可以在logn的时间复杂度内查询某两个点的lca,还可以实现在logm的时间复杂度能查询某个节点的第m个父亲节点. 算法的核心是用二进制的运算来实现查询. #include<bits/s

codeforces 519E A and B and Lecture Rooms LCA倍增

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 519E Description A and B are preparing themselves for programming contests. The University where A and B study is a set of rooms connected

[Codeforces#519E] A and B and Lecture Rooms

Codeforces题号:#519E 出处: Codeforces 主要算法:最近公共祖先LCA(倍增法) 难度:4.5 思路分析: 题意:询问给出一棵无根树上任意两点a,b,求关于所有点i,\(dist(a,i) = dist(b,i)\)的点的数量.要求每一次询问在O(log n)的时间复杂度内完成. 由于在树上求距离,并且还要O(log n),自然会联想到LCA.由于边权是1,那么点到根的距离就是该点的深度.这个深度可以在dfs预处理的过程中处理完成.那么两个点之间的距离就是两个点到根节点

Codeforces 519E A and B and Lecture Rooms LCA

题目链接:点击打开链接 题意: 给定n个点的树. 下面n-1行给出树 Q个询问. 每次询问 (u,v)问树上有多少个点到u点距离=到v点距离 思路: 首先这两个点的距离必须是偶数,若为奇数答案就是0 然后用lca找到中间节点即可. trick : u==v ans = n #include"cstdio" #include"iostream" #include"queue" #include"algorithm" #inclu

Codefources 519E. A and B and Lecture Rooms LCA

简单LCA: 求树上距离给定两个点a,b距离相等的点有多少个 先预处理出每个节点的孩子个数sum[x],求出a,b的LCA,根据深度就可以知道两个点的距离,距离为偶数的有解.... 根据lca在a,b之间的位置不同分情况讨论: 设a与lca距离为 ha , b与lca距离为 hb 1:lca在a,b正中间既a,b分别属于lca的两个子树中, 结果为: n-sum[ a往上距离lca ha-1 的点] - sum[ b往上距离lca hb-1 的点] 2:a,b两个点相对lca一上一下. c:a,

CodeForces 519E 树形DP A and B and Lecture Rooms

给出一棵树,有若干次询问,每次询问距两个点u, v距离相等的点的个数. 情况还挺多的,少侠不妨去看官方题解.^_^ 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 using namespace std; 7 8 const int maxn = 100000 + 10; 9 10

Codeforces Round #294 Div2 E(A and B and Lecture Rooms)

Problem 给一棵树,含有N个节点,N?1条边.进行M次查询,每次给定两个节点x,y,问树上有多少个节点到x,y的距离相同. Limits TimeLimit(ms):2000 MemoryLimit(MB):256 N,M∈[1,105] x,y∈[1,N] Look up Original Problem From here Solution 求出a,b两个节点的lca,再找到lca?>a或lca?>b上的某个节点v使得dis(v,a)=dis(v,b).分v=lca,v∈lca?&g

Codeforces Round #287 D.The Maths Lecture

The Maths Lecture 题意:求存在后缀Si mod k =0,的n位数的数目.(n <=1000,k<=100); 用f[i][j]代表 长为i位,模k等于j的数的个数. 可以用 f[i+1][(t*10i+j)%k]=∑f[i][j]+(j==0),(t*10i+j)%k!=0;动态规划 这样可以求出所有f[n][i] i>0 的值. 最后用9*10^(n-1)-∑f[n][i] 就可以得到 答案 #include <bits/stdc++.h> using

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 oth