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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值