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.



一开始题意读错了,坑死了。题意:给定一棵树,m次询问,每次询问给两个点a,b,求这棵树上有多少个到这两点距离相等的点,注意是最短距离相等,不可重复走一条边。首先肯定要LCA啦,然后分情况讨论,比如a==b这时候输出n。

root=LCA(a,b);

然后求一下a到root的距离加上b到root的距离k。如果K是奇数则输出0.

如果K是偶数,则要继续分情况讨论。第一种情况是a和b的深度不等,这时候要求出中间那个点,然后答案就是这个点的 不包含a和b 的子树,就是比如这个点有k个儿子,其中肯定有一个儿子下面有a或者b,总的子树去掉这个儿子包含的子树,就是答案。

第二种情况,a和b的深度相等,那么答案就是root上面的所有点和root的不包含a和b的儿子的子树。

然后先预处理出以每个点为根的树有多少个节点,然后再用ST表来求某个点的第几个祖先是多少来找那个中间点和中间的的特殊儿子节点。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#define maxn 100005
using   namespace std;
int n,m;
int final_ans;
int flagup[3];
int flagdown;
int tmpp[maxn];
vector<int>p[maxn];
int fat[100005][20],lev[100005],father[maxn];
void dfs(int x,int fa){
    father[x]=fa;
    fat[x][0]=fa;
    lev[x]=lev[fa]+1;
    int e=p[x].size();
    for(int i=0;i<e;i++){
        int y=p[x][i];
        if(y==fa) continue;
        dfs(y,x);
    }
}

int lca(int x,int y){
    if(lev[x]>lev[y]) swap(x,y);
    for(int i=19;i>=0;i--){
        if(lev[fat[y][i]]>=lev[x]){
            y=fat[y][i];
        }
    }//拉到同一高度
    for(int i=19;i>=0;i--){
        if(fat[x][i]!=fat[y][i]){
            x=fat[x][i];
            y=fat[y][i];
        }
    }//一起往上找
    return (x==y) ? x:fat[x][0];
}

void intit()
{
     	int i, j;
	dfs(1,0);
        for(i=1;i<=19;i++){
            for(j=0;j<=n;j++){
                fat[j][i]=fat[fat[j][i-1]][i-1];
            }
        }
}//初始化处理

int find_dfs(int now,int fa)
{
    ///////////////
    int e=p[now].size();
     for(int i=0;i<e;i++){
        int y=p[now][i];
        if(y==fa) continue;
        tmpp[now]+=find_dfs(y,now);
    }
    return tmpp[now];
}
int find_ans(int now,int kk)
{
    int j=0;

    while(kk)
        {
            if(kk%2==1)
            {
            now=fat[now][j];
            }
            kk>>=1;
            j++;
        }
     return now;

}
int main()
{
    int u,v;

    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<maxn;i++)
        {
            p[i].clear();
        }
        for(int i=0;i<n-1;i++)
        {
            scanf("%d%d",&u,&v);
            p[u].push_back(v);
            p[v].push_back(u);

        }
        intit();
        scanf("%d",&m);
        for(int i=1;i<=n;i++)
            tmpp[i]=1;
            find_dfs(1,0);


    int now;

        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&u,&v);
            int root=lca(u,v);
            int k=lev[u]-lev[root]+lev[v]-lev[root];
            if(lev[u]<lev[v])
                swap(u,v);//u深一点
            if(u==v)
            {
                printf("%d\n",n);
                continue;
            }
            if(k%2)
            {
                printf("0\n");
                continue;
            }
            if(lev[u]!=lev[v])//需要在k点找,k在LCA的子树中
            {
                now=find_ans(u,k/2);
                k=find_ans(u,k/2-1);
                printf("%d\n",tmpp[now]-tmpp[k]);
                continue;}

            if(lev[u]==lev[v])
            {
               now=find_ans(u,k/2);
               int son1=find_ans(u,k/2-1);
               int son2=find_ans(v,k/2-1);
               printf("%d\n",tmpp[1]-tmpp[now]+tmpp[now]-tmpp[son1]-tmpp[son2]);
            }

        }

    }
}


基于STM32 F4的永磁同步电机无位置传感器控制策略研究内容概要:本文围绕基于STM32 F4的永磁同步电机(PMSM)无位置传感器控制策略展开研究,重点探讨在不依赖物理位置传感器的情况下,如何通过算法实现对电机转子位置和速度的精确估计与控制。文中结合嵌入式开发平台STM32 F4,采用如滑模观测器、扩展卡尔曼滤波或高频注入法等先进观测技术,实现对电机反电动势或磁链的估算,进而完成无传感器矢量控制(FOC)。同时,研究涵盖系统建模、控制算法设计、仿真验证(可能使用Simulink)以及在STM32硬件平台上的代码实现与调试,旨在提高电机控制系统的可靠性、降低成本并增强环境适应性。; 适合人群:具备一定电力电子、自动控制理论基础和嵌入式开发经验的电气工程、自动化及相关专业的研究生、科研人员及从事电机驱动开发的工程师。; 使用场景及目标:①掌握永磁同步电机无位置传感器控制的核心原理与实现方法;②学习如何在STM32平台上进行电机控制算法的移植与优化;③为开发高性能、低成本的电机驱动系统提供技术参考与实践指导。; 阅读建议:建议读者结合文中提到的控制理论、仿真模型与实际代码实现进行系统学习,有条件者应在实验平台上进行验证,重点关注观测器设计、参数整定及系统稳定性分析等关键环节。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值