http://codeforces.com/contest/519/problem/E

题意:

给出一棵树和m次询问,每次询问给出两个点,求出到这两个点距离相等的点的个数。

思路:

lca...然后直接判就好了,挂dp标签的人是什么心态。。

 #include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iostream>
int tot,go[],first[],next[];
int son[],fa[][];
int deep[];
int n,bin[];
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void insert(int x,int y){
tot++;
go[tot]=y;
next[tot]=first[x];
first[x]=tot;
}
void add(int x,int y){
insert(x,y);insert(y,x);
}
void dfs(int x,int f){
son[x]=;
for (int i=;i<=;i++)
fa[x][i]=fa[fa[x][i-]][i-];
for (int i=first[x];i;i=next[i]){
int pur=go[i];
if (pur==f) continue;
deep[pur]=deep[x]+;
fa[pur][]=x;
dfs(pur,x);
son[x]+=son[pur];
}
}
int lca(int x,int y){
if (deep[x]<deep[y]) std::swap(x,y);
int t=deep[x]-deep[y];
for (int i=;i<=;i++)
if (t&bin[i])
x=fa[x][i];
if (x==y) return x;
for (int i=;i>=;i--)
if (fa[x][i]!=fa[y][i])
x=fa[x][i],y=fa[y][i];
if (x==y) return x;
else return fa[x][];
}
void up(int &x,int dep){
int t=deep[x]-dep;
for (int i=;i<=;i++)
if (t&bin[i])
x=fa[x][i];
}
void work(int x,int y){
int t=lca(x,y);
int len=deep[x]-deep[t]++deep[y]-deep[t]+-;
if (len%==){
printf("0\n");
return;
}
if (x==y){
printf("%d\n",n);
return;
}
if (t==x||t==y){
if (t==x) std::swap(x,y);
int tt=x;
up(tt,deep[tt]-len/);
up(x,deep[tt]+);
printf("%d\n",son[tt]-son[x]);
}else{
if (deep[x]<deep[y]) std::swap(x,y);
if (deep[x]==deep[y]){
up(x,deep[t]+);
up(y,deep[t]+);
printf("%d\n",n-son[x]-son[y]);
}else{
int tt=x;
up(tt,deep[tt]-len/);
up(x,deep[tt]+);
printf("%d\n",son[tt]-son[x]);
}
}
}
int main(){
n=read();bin[]=;for (int i=;i<=;i++) bin[i]=bin[i-]*;
for (int i=;i<n;i++){
int x=read(),y=read();
add(x,y);
}
dfs(,);
int T=read();
while (T--){
int x=read(),y=read();
work(x,y);
}
return ;
}

Codeforces 519E A and B and Lecture Rooms的更多相关文章

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

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

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

  3. CodeForces 519E A and B and Lecture Rooms(倍增)

    A and B are preparing themselves for programming contests. The University where A and B study is a s ...

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

    题意: 给你一棵有n个节点的树,给你m次询问,查询给两个点,问树上有多少个点到这两个点的距离是相等的.树上所有边的边权是1. 思路: 很容易想到通过记录dep和找到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. Redis应用场景 及其数据对象 string hash list set sortedset

    原文地址:http://www.cnblogs.com/shanyou/archive/2012/09/04/2670972.html Redis开创了一种新的数据存储思路,使用Redis,我们不用在 ...

  2. 理解i-node

    原文链接:http://www.ruanyifeng.com/blog/2011/12/inode.html 感觉讲得挺好,便做个记录.

  3. css实现居中的各种方法

    css垂直居中有很多种方法,可以参考下这个网站

  4. POJ_1321——棋盘问题,回溯+剪枝

    Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子 ...

  5. (转)20 个大大节省你时间的 HTML5 开发工具

    Rendera 如果你希望有个环境可以测试.浏览和体验各种不同的 CSS/HTML 和 JavaScript 代码,Rendera 为你提供了实时的运行结果.类似 RunJS. Patternizer ...

  6. 【架构师之路】依赖注入原理---IoC框架

    1 IoC理论的背景    我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实现系统的业务逻辑.  图1:软件系统中耦合的对象 如果我们 ...

  7. POJ 1637 混合图求欧拉回路 最大流实现

    前面讲过了无向图,有向图求欧拉回路,欧拉通路的做法.可以直接根据度数来判断,当然前提是这是一个连通图. 这道题既有无向边,又有有向边,然后求欧拉回路. 采用的方法是最大流. 具体处理方法. 首先,我们 ...

  8. 高性能WEB开发(11) - flush让页面分块,逐步呈现

    高性能WEB开发(11) - flush让页面分块,逐步呈现 在处理比較耗时的请求的时候,我们总希望先让用户先看到部分内容,让用户知道系统正在进行处理,而不是无响应.一般大家在处理这样的情况,都使用a ...

  9. openwrt上网配置的一些理解(二)

    上一篇里面遇到了只能静态上网的问题,动态不行.所以再接再励,问题总是要解决的,偷懒的下场就是一直停留在菜鸟的水平. 首先分析下问题,要动态上网,首先我要明确不是动态获取不了IP,是获取了,上不了外网. ...

  10. Duanxx的技术问题:word界面显示模糊

    今天打开word时,出现了word打开失败的现象,并且word的界面显示特别的模糊,找了好半天,才解决,问题见下图: 解决方式: 在word的文件->选项,这里面找到显示,然后勾选:禁用硬件图形 ...