【链接】 我是链接,点我呀:)

【题意】

在这里输入题意

【题解】

最近公共祖先。
(树上倍增

一开始统计出每个子树的节点个数_size[i]

如果x和y相同。

那么直接输出n.

否则求出x和y的最近公共祖先。z

(假定y的深度大于x

【1】如果z等于x或y中的一个。

那么久就找到x..y的路径(长度设为L)中的中点u。

显然,u和它的其他len-1个子树上的任意一个节点都是可行的(除了那个包含y的子树

设_get(x,step)表示x节点往上走step步到达的节点

则输出_sum[中点]-_sum[ _get(y,L/2) ]即可

【2】如果z不等于x和y中的任意一个。

①x和y往上走的距离是一样的。

->那么z的子树中,除了这两个节点往上走上来的子树,

其他子树里面的节点都是可行的

②x和y往上走的距离不一样。

->那么就还是和【1】中的情况一样,找出中点即可。

_get(x,step)函数可以用树上倍增的p数组实现。

【代码】

#include <bits/stdc++.h>
using namespace std; const int MAXN = 1e5+10;
const int MAX = 17; vector <int> son[MAXN],g[MAXN];
int n,p[MAXN][MAX+5],dep[MAXN],pre[MAX+5],m;
int _size[MAXN]; void dfs(int x,int f)
{
_size[x] = 1;
dep[x] = dep[f] + 1;
p[x][0] = f;
for (int i = 1; i <= MAX; i++) p[x][i] = p[p[x][i - 1]][i - 1];
int len = g[x].size();
for (int i = 0; i <= len - 1; i++)
{
int y = g[x][i];
if (y != f) {
son[x].push_back(y);
dfs(y, x);
_size[x]+=_size[y];
}
}
} int _get(int x,int ma){
for (int i = MAX;i>=0;i--){
if (ma>=pre[i]){
ma-=pre[i];
x = p[x][i];
}
}
return x;
} int main()
{
ios::sync_with_stdio(0),cin.tie(0);
#ifdef LOCAL_DEFINE
freopen("rush.txt","r",stdin);
#endif
pre[0] = 1;
for (int i = 1; i <= MAX; i++)
pre[i] = pre[i - 1] << 1;
cin >> n;
for (int i = 1; i <= n; i++)
son[i].clear();
for (int i = 1; i <= n - 1; i++)
{
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1, 0);
cin >> m;
for (int i = 1; i <= m; i++)
{
int t0, t1,pret1,pret0;
cin >> t0 >> t1;
if(t0==t1){
cout<<n<<endl;
continue;
}
if (dep[t0] > dep[t1]) swap(t0, t1);
pret1 = t1;
pret0 = t0;
int dist0 = 0,dist1 = 0;
for (int i = MAX; i >= 0; i--)
if (dep[t0] <= dep[t1] - pre[i]){
t1 = p[t1][i];
dist1 += pre[i];
}
//t1��t0����ͬһ�߶�
if (t1 == t0)
{
if ((dist0+dist1)%2==0){
int dis = (dist0+dist1)/2;
int special = _get(pret1,dis);
cout << _size[special] - _size[_get(pret1,dis-1)]<<endl;
}else{
cout<<0<<endl;
}
continue;
}
for (int i = MAX; i >= 0; i--)
{
if (p[t0][i] == p[t1][i]) continue;
dist0+=pre[i];dist1+=pre[i];
t0 = p[t0][i], t1 = p[t1][i];
}
dist0+=pre[0],dist1+=pre[0];
t0 = p[t0][0];
if (dist0==dist1){
cout << _size[1]-_size[_get(pret0,dist0-1)]-_size[_get(pret1,dist1-1)] << endl;
}else{
if ((dist0+dist1)%2==0){
int dis = (dist0+dist1)/2;
int special = _get(pret1,dis);
cout << _size[special] - _size[_get(pret1,dis-1)]<<endl;
}else{
cout<<0<<endl;
}
}
} return 0;
}

【Henu ACM Round#15 E】 A and B and Lecture Rooms的更多相关文章

  1. 【Henu ACM Round#15 F】Arthur and Questions

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] a1+a2+...+ak<a2+a3+...ak+1 ->a1<ak+1 a2+a3+...+ak+1<a3 ...

  2. 【Henu ACM Round#15 D】Ilya and Escalator

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 概率DP; 设f[i][j]表示前i个单位时间,j个人进入房间的概率是多少 然后想一下和i-1秒的时候要怎么转移就可以了. i-1秒 ...

  3. 【Henu ACM Round#15 C】 A and B and Team Training

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举第一种方法. 剩下的全都个第二种方法. 看看能组成多少个队伍就可以了. [代码] #include <bits/stdc+ ...

  4. 【Henu ACM Round#15 B】A and B and Compilation Errors

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 开3个map, 存在map里面: 然后迭代第一个和第二个map; 分别与第二个和第三个map比较就可以了 [代码] #include ...

  5. 【Henu ACM Round#15 A】 A and B and Chess

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 统计大写和小写的个数. 比较答案.输出即可. [代码] #include <bits/stdc++.h> using n ...

  6. 【Henu ACM Round#16 A】 Bear and Game

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 看看什么时候t[i]-t[i-1]>15. 输出t[i-1]+15就好. 不存在这样的i就输出min(t[n]+15,90) ...

  7. 【Henu ACM Round#24 E】Connected Components

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 要求把连续的一段li..ri的边全都删掉. 然后求剩下的图的联通数 如果暴力的话 复杂度显然是O(k*m)级别的. 考虑我们把li. ...

  8. 【Henu ACM Round#24 D】Iterated Linear Function

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把B提取出来就是一个等比数列了. 求和一下会发现是这种形式. \(B*\frac{(A^n-1)}{A-1}+A^n*x\) 则求一 ...

  9. 【Henu ACM Round#24 C】Quiz

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 肯定是这样 先放k-1个,然后空1个,然后再放k-1个.然后再空1个.. 以此类推. 然后如果(n/k)*(k-1)+n%k> ...

随机推荐

  1. 8、java高级面向对象-重载、构造器重载、初始化块、this、super、对象构造和初始化分析、覆盖、toString

    1.方法的重载(overload) 同一个类中同时存在一个以上的同名函数,参数个数或类型不同或顺序不同,称为方法的重载. 和返回值无关! 构造器重载:非默认构造器和默认构造器其实就是方法的重载. 2. ...

  2. Laravel-数据库队列

    Laravel-数据库队列 标签(空格分隔): php 介绍 Laravel队列为不同的后台队列服务提供统一的API,例如Beanstalk,Amazon SQS, Redis,甚至其他基于关系型数据 ...

  3. 4、Go for循环

    package main import "fmt" func main(){ //for 循环是go语言唯一的循环结构,分为三种类型 //第一种 类似while i:=1 for ...

  4. Ubuntu下哪个PDF阅读器更好使???

    根据windows系统上的经验,果断选择了foxit reader的linux版本: 从 http://www.foxitsoftware.com/downloads/ 选择 “Desktop Lin ...

  5. shell判断变量是字符还是数字

    ok,以后最好是每天一个shell小脚本吧,这样以后工作时还可以直接套用,嗯,比较不错,顺便还可以带给刚入门shell的朋友一些帮助,好了,废话不多说,下面是我两种判断的实现方式: 1.通过grep去 ...

  6. Configure Tomcat 7 to run Python CGI scripts in windows(Win7系统配置tomcat服务器,使用python进行cgi编程)

    Pre-installation requirements1. Java2. Python steps1. Download latest version of Tomcat (Tomcat 7) f ...

  7. Ehcache学习总结(2)--Ehcache整合spring配置

    首先需要的maven依赖为: [html] view plain copy <!--ehcache--> <dependency> <groupId>com.goo ...

  8. glLoadIdentity

    参考文章:百度百科 OpenGL函数思考-glLoadIdentity OpenGL为我们提供了一个非常简单的恢复初始坐标系的手段,那就是调用glLoadIdentity()命令.该命令是一个无参的无 ...

  9. new期间的异常

    new包含两步,调用operator new申请空间,以及调用构造函数. 如果第一步结束之后,第二步发生异常,需要归还第一步的空间. 编译器帮我们做了这件事情,并且会调用对应的delete. 另外 n ...

  10. 从串口设置、读取、并分析um220模块的数据

    转载请注明:http://blog.csdn.net/wang_zheng_kai 导航制导与控制实验室 2014年11月10日 好久没有写博客了,先从一个小小的程序開始一段新的历程吧. 近期的项目主 ...