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
4
1 2
1 3
2 4
1
2 3
output

Copy
1
input

Copy
4
1 2
2 3
2 4
2
1 2
1 3
output

Copy
0
2 题意:给出一棵树,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[][],deep[],size[];
vector<int> g[]; void dfs(int now,int f,int dep)
{
deep[now]=dep;
fa[][now]=f;
size[now]=;
for(int i=;i<=;i++)
{
fa[i][now]=fa[i-][fa[i-][now]];
}
for(int i=;i<g[now].size();i++)
{
if(g[now][i]==f) continue;
dfs(g[now][i],now,dep+);
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=;i>=;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=,dis2=;
for(int i=;i>=;i--)
{
if(deep[fa[i][x]]>=deep[y])
{
x=fa[i][x];
dis1+=pow(,i);
}
}
if(x==y)
{
if(dis1%==) return ;
int need=dis1/;
need--;
for(int i=;i>=;i--)
{
if(need&(<<i))
{
u=fa[i][u];
}
}
return size[fa[][u]]-size[u];
}
else
{
for(int i=;i>=;i--)
{
if(fa[i][x]!=fa[i][y])
{
x=fa[i][x];
dis1+=pow(,i);
y=fa[i][y];
dis2+=pow(,i);
}
}
if((dis1+dis2+)%==) return ;
int need=(dis1+dis2+)/;
need--;
for(int i=;i>=;i--)
{
if(need&(<<i))
{
u=fa[i][u];
}
}
// printf("nowu:%d\n",u);
return size[fa[][u]]-size[u];
}
} int main()
{
scanf("%d",&n);
int from,to;
for(int i=;i<=n-;i++)
{
scanf("%d%d",&from,&to);
g[from].push_back(to);
g[to].push_back(from);
}
dfs(,,);
scanf("%d",&m);
while(m--)
{
scanf("%d%d",&from,&to);
printf("%d\n",get(from,to));
}
}


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

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

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

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

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

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

  4. Codeforces 519E A and B and Lecture Rooms

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

  5. 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 ...

  6. [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= ...

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

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

  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. ubuntu时区设置

    local-gen zh_CN.UTF-8 UTF-8 /var/lib/locales/supported.d/local可以看到如下内容: zh_CN.UTF-8 UTF-8 en_US.UTF- ...

  2. 关于git的reset、checkout、revert

    https://www.atlassian.com/git/tutorials/resetting-checking-out-and-reverting/file-level-operations 最 ...

  3. python查找文件相同的和包含汉字的

    #!/usr/bin/env python # Version = 3.5.2 import os import time d_path = '/data/media' log_file = 'res ...

  4. fileinput模块可以循环一个或多个文本文件的内容

    fileinput模块可以循环一个或多个文本文件的内容. [默认格式] fileinput.input (files=None, inplace=False, backup='', bufsize=0 ...

  5. js 阻止事件捕获

    1.支持W3C标准的浏览器在添加事件时用addEventListener(event,fn,useCapture)方法,基中第3个参数 useCapture是一个Boolean值,用来设置事件是在事件 ...

  6. onlevelwasloaded的调用时机

    并不是任何场景加载完成后都会触发该事件,必须使用场景加载API才能触发onlevelwasloaded函数,场景加载API如 scenemanager.loadscene等,additive模式不触发 ...

  7. Innobackupex MySQL 全备、增备及恢复

    简介: 在这之前都是通过 mysqldump 来备份数据库的,由于是逻辑备份,所以采用这种备份方式数据是很安全的,跨平台.版本都很容易. 凡事有利必有弊,逻辑备份在你数据库比较大时,备份.恢复数据所耗 ...

  8. powerdesigner 数据库表定义导出到excel

    shift+ctrl+X,打开脚本运行,脚本如下:'************************************************************************** ...

  9. 在java中导出excel

    package com.huawei.controller; import java.io.File;import java.io.IOException;import java.util.HashM ...

  10. 前端开发之CSS篇四

    一.相对定位 二.绝对定位 三.固定定位 四.z-index 前言 定位有三种:1.相对定位 2.绝对定位 3.固定定位 这三种定位,每种都暗藏玄机,所以要每个单独剖析. 1️⃣   相对定位 1.三 ...