Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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.

Sample Input

Input
41 21 32 412 3
Output
1
Input
41 22 32 421 21 3
Output
02

Hint

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

思路:

题意:给你一棵有n个节点的树,m个询问,每个询问a,b,求树中到a,b距离相等的节点个数

我们先使得deg[a] >= deg[b]

首先,若a~b之间的距离为奇数的话,找不到唯一的中点,此时答案为0

(怎么求a~b之间的距离呢?预处理节点i到根的距离(此处为深度)deg[i],求出lcaq = lca(a,b);

那么dist(a,b) = deg[a] - deg[lcaq] + deg[b] - deg[lcaq];)

否则:找出ab路径的中点mid,calc(u, d)可以找出节点u的深度为d的祖先,也就是我们求出中点的深度就可以找出中点!

怎么求中点的深度dmid?通过画图可知:

dmid = dist(a,b) / 2  - deg[b] + 2 * deg[lcaq];

最终答案的计算:num[i]表示以节点i为根的子树的节点总数

若a和b的深度相同,那么ans = num[lcaq] - num[xa] - num[xb]; 其中xa是lcaq在a~lcaq的下一个节点,xb是lcaq在b~lcaq的下一个节点

若a和b深度不同,那么ans = num[mid] - num[k], 其中k表示ab的中点的下一位置(在a和b路径上靠近a的下一位置),画图可以理解。

#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 5;
struct edge {
    int v, to;
    edge() {};
    edge(int v, int to) : v(v), to(to) {};
}e[N << 1];
int head[N], num[N], deg[N], tot, n, m;
int p[N][30];

void init()
{
    memset(head, -1, sizeof head);
    memset(p, -1, sizeof p);
    tot = 0;
}
void addedge(int u, int v)
{
    e[tot] = edge(v, head[u]);
    head[u] = tot++;
}
void dfs(int u, int fa, int d)
{
    deg[u] = d; p[u][0] = fa; num[u] = 1;
    for(int i = head[u]; ~i; i = e[i].to) {
        int v = e[i].v;
        if(v != fa) {
            dfs(v, u, d + 1);
            num[u] += num[v];
        }
    }
}
void pre()
{
    for(int j = 0; (1 << j) <= n; ++j)
        for(int i = 1; i <= n; ++i) {
            if(p[i][j - 1] != -1)
            p[i][j] = p[ p[i][j - 1] ][j - 1];
    }
}

int calc(int u, int d) ///返回节点u的深度为d的祖先
{
    for(int j = 25; j >= 0; --j) {
        if(deg[u] - (1 << j) >= d) u = p[u][j];
    }
    return u;
}
int lca(int a, int b)
{
    a = calc(a, deg[b]);///使a和b处在同一层
    if(a == b) return a; ///若此时a和b相等,那么lca就是a
    ///否则a和b同时向上爬
    for(int j = 25; j >= 0; --j) {
        if(p[a][j] != -1 && p[a][j] != p[b][j]) {
            a = p[a][j];
            b = p[b][j];
        }
    }
    return p[a][0];
}
int main()
{
    while(~scanf("%d", &n))
    {
        init();
        int u, v;
        for(int i = 1; i < n; ++i) {
            scanf("%d%d", &u, &v);
            addedge(u, v);
            addedge(v, u);
        }
        dfs(1, -1, 0);
        pre();

        scanf("%d", &m);
        int a, b;
        while(m --)
        {
            scanf("%d%d", &a, &b);
            if(deg[a] < deg[b]) swap(a, b);
            if(a == b) { printf("%d\n", n); continue; }
            int lcaq = lca(a, b);
            int dist = deg[a] + deg[b] - 2 * deg[lcaq]; ///a和b之间的距离
            if(dist & 1) { puts("0"); continue; }
            if(deg[a] == deg[b]) { ///xa和xb分别是在a~lca和b~lca的路径上距离lca为1的点
                int xa = calc(a, deg[lcaq] + 1);
                int xb = calc(b, deg[lcaq] + 1);
                printf("%d\n", n - num[xa] - num[xb]);
            }
            else {
                int mid = dist / 2 - deg[b] + 2 * deg[lcaq]; ///mid为ab之间的路径的中点的深度
                printf("%d\n", num[ calc(a, mid) ] - num[ calc(a, mid + 1) ]);
            }
        }
    }
    return 0;
}

  

codeforces 519E A and B and Lecture Rooms LCA倍增的更多相关文章

  1. codeforces 519E A and B and Lecture Rooms(LCA,倍增)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud E. A and B and Lecture Rooms A and B are ...

  2. Codeforces 519E A and B and Lecture Rooms

    http://codeforces.com/contest/519/problem/E 题意: 给出一棵树和m次询问,每次询问给出两个点,求出到这两个点距离相等的点的个数. 思路: lca...然后直 ...

  3. 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 s ...

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

    题意: 给你一棵有n个节点的树,给你m次询问,查询给两个点,问树上有多少个点到这两个点的距离是相等的.树上所有边的边权是1. 思路: 很容易想到通过记录dep和找到lca来找到两个点之间的距离,然后分 ...

  5. [codeforces 519E]E. A and B and Lecture Rooms(树上倍增)

    题目:http://codeforces.com/problemset/problem/519/E 题意:给你一个n个点的树,有m个询问(x,y),对于每个询问回答树上有多少个点和x,y点的距离相等 ...

  6. Codeforces Round #294 (Div. 2) A and B and Lecture Rooms(LCA 倍增)

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

  7. [CF Round #294 div2] E. A and B and Lecture Rooms 【树上倍增】

    题目链接:E. A and B and Lecture Rooms 题目大意 给定一颗节点数10^5的树,有10^5个询问,每次询问树上到xi, yi这两个点距离相等的点有多少个. 题目分析 若 x= ...

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

    给出一棵树,有若干次询问,每次询问距两个点u, v距离相等的点的个数. 情况还挺多的,少侠不妨去看官方题解.^_^ #include <iostream> #include <cst ...

  9. Codeforces 519 E. A and B and Lecture Rooms

    Description 询问一个树上与两点距离相等的点的个数. Sol 倍增求LCA. 一棵树上距离两点相等,要么就只有两点的中点,要么就是与中点相连的所有点. 有些结论很容易证明,如果距离是偶数,那 ...

随机推荐

  1. 【单元测试】NUint使用详解及Visual Studio配置

    阅读目录 什么是单元测试? 为什么使用单元测试? NUint使用详解: 示例 属性 断言 简单测试 VS配置: External Tools Visual Nunit 2010 NUnit Test ...

  2. windows下实现Git在局域网使用

    1.首先在主机A上创建一个文件夹用于存放你要公开的版本库.然后进入这个文件夹,右键->Git create repository here,弹出的窗口中勾选Make it Bare!之后将这个文 ...

  3. Convertion of grey code and binary 格雷码和二进制数之间的转换

    以下转换代码摘自维基百科 Wikipedia: /* The purpose of this function is to convert an unsigned binary number to r ...

  4. Tomcat6.0的Thisisverylikelytocreateamemoryleak异常

    從Apache Tomcat 5.5升級到6.0,通常不用太大的修改,原有的Web Application就能繼續運作.不過在server.xml中設定MySQL Datasource,卻出現一串惱人 ...

  5. encodeURIComponent()编码和decodeURIComponent()解码

    html1: <!DOCTYPE HTML> <meta charset=utf-8> <meta http-equiv="X-UA-Compatible&qu ...

  6. IIS配置及防黑

    安装IIS.部署网站(发布或者拷贝都可以).修改连接字符串,compilation设为false,删掉cs代码 上传文件夹不给执行权限: 在iis管理器中找到上传文件夹,选择属性--执行权限,设置为“ ...

  7. uva-699 Not so Mobile (杠杆,巧妙递归)

      Not so Mobile  Before being an ubiquous communications gadget, a mobile was just a structure made ...

  8. 如何把一个vue组件改为ionic/angular组件

    同是mvvm框架,他们之间是很相似的,如何你已经熟悉其中的一个,那么另一个也就基本上也就会的差不多了. 一.动态属性.值.事件绑定 vue中使用v-bind:或者之间分号:进行绑定 ng中左括号[]进 ...

  9. Filebeat 快速开始

    Filebeat可以做什么 条目 filebeat 编写语言 GO 是否支持多输出 支持 是否支持多输入 支持 是否支持修改日志内容 支持 是否会丢数据 不会 对多行文件的合并 支持 对多层目录的模糊 ...

  10. Linux文件系统命令 mkdir/rmdir

    命令名:mkdir 功能:创建一个文件夹,和touch的区别是,touch是创建一个文件,后面可以跟绝对路径和相对路径 eg: mkdir ren 命令名:rmdir 功能:删除一个文件夹