Codeforces Round #294 (Div. 2) E LCA倍增

E. A and B and Lecture Rooms
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 - 1 corridors 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.

#include <algorithm>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string.h>

using namespace std;

const int N=(int)1e5+10;

vector<int> vec[N];
int siz[N],depth[N];
int pre[N][20];
bool haspre[N];

void init(int n){
    for(int i=1;i<=n;i++){
        vec[i].clear();
    }
    memset(siz,0,sizeof(siz));
    memset(depth,0,sizeof(depth));
    memset(pre,0,sizeof(pre));
    memset(haspre,false,sizeof(haspre));
}
void dfs(int u,int p,int d){
    siz[u]=1;
    depth[u]=d;
    pre[u][0]=p;
    for(int i=0;i<vec[u].size();i++){
        int v=vec[u][i];
        if(v==p) continue;
        dfs(v,u,d+1);
        siz[u]+=siz[v];
    }
}
void gether(int n){
    for(int j=1;(1<<j)<=n;j++){
        for(int i=1;i<=n;i++){
            pre[i][j]=pre[pre[i][j-1]][j-1];
        }
    }
}
int Find_pre(int u,int h){
    for(int i=0;i<=20;i++){
        if((1<<i)&h) u=pre[u][i];
    }
    return u;
}
int LCA(int u,int v){
    if(depth[u]>depth[v]) swap(u,v);
    v=Find_pre(v,depth[v]-depth[u]);
    if(u!=v){
        for(int i=(int)log2(N);i>=0;i--){
            if(pre[v][i]!=pre[u][i]){
                v=pre[v][i];
                u=pre[u][i];
            }
        }
        u=pre[u][0];
    }
    return u;
}
int main(){
    int n;
    scanf("%d",&n);
    init(n);
    for(int i=1;i<n;i++){
        int u,v;
        scanf("%d %d",&u,&v);
        vec[u].push_back(v);
        vec[v].push_back(u);
        haspre[v]=true;
    }
    int root;
    for(int i=1;i<=n;i++){
        if(!haspre[i]){
            root=i;
            break;
        }
    }
    dfs(root,0,0);
    gether(n);
    int q;
    scanf("%d",&q);
    while(q--){
        int u,v;
        scanf("%d %d",&u,&v);
        if(u==v){
            printf("%d\n",n);
            continue;
        }
        int lca=LCA(u,v);
        int du=depth[u];
        int dv=depth[v];
        int dlca=depth[lca];
        int dis=du+dv-2*dlca;
        if(dis&1) {
            puts("0");
        } else {
            du-=dlca;
            dv-=dlca;
            if(du==dv){
                int uu=Find_pre(u,du-1);
                int vv=Find_pre(v,dv-1);
                printf("%d\n",n-siz[uu]-siz[vv]);
            } else {
                if(depth[u]>depth[v]) swap(u,v);
                int uu=Find_pre(v,dis/2);
                int vv=Find_pre(v,dis/2-1);
                printf("%d\n",siz[uu]-siz[vv]);
            }
        }

    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
图像识别技术在病虫害检测中的应用是一个快速发展的领域,它结合了计算机视觉和机器学习算法来自动识别和分类植物上的病虫害。以下是这一技术的一些关键步骤和组成部分: 1. **数据收集**:首先需要收集大量的植物图像数据,这些数据包括健康植物的图像以及受不同病虫害影响的植物图像。 2. **图像预处理**:对收集到的图像进行处理,以提高后续分析的准确性。这可能包括调整亮度、对比度、去噪、裁剪、缩放等。 3. **特征提取**:从图像中提取有助于识别病虫害的特征。这些特征可能包括颜色、纹理、形状、边缘等。 4. **模型训练**:使用机器学习算法(如支持向量机、随机森林、卷积神经网络等)来训练模型。训练过程中,算法会学习如何根据提取的特征来识别不同的病虫害。 5. **模型验证和测试**:在独立的测试集上验证模型的性能,以确保其准确性和泛化能力。 6. **部署和应用**:将训练好的模型部署到实际的病虫害检测系统中,可以是移动应用、网页服务或集成到智能农业设备中。 7. **实时监测**:在实际应用中,系统可以实时接收植物图像,并快速给出病虫害的检测结果。 8. **持续学习**:随着时间的推移,系统可以不断学习新的病虫害样本,以提高其识别能力。 9. **用户界面**:为了方便用户使用,通常会有一个用户友好的界面,显示检测结果,并提供进一步的指导或建议。 这项技术的优势在于它可以快速、准确地识别出病虫害,甚至在早期阶段就能发现问题,从而及时采取措施。此外,它还可以减少对化学农药的依赖,支持可持续农业发展。随着技术的不断进步,图像识别在病虫害检测中的应用将越来越广泛。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值