1. 程式人生 > >Codeforces Round #294 (Div. 2) E. A and B and Lecture Rooms(lca+思維,樹上尋找與給定兩個點距離相等的點的個數)

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",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\n",ans);
    }
    return 0;
}