【Henu ACM Round#15 E】 A and B and Lecture Rooms
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
最近公共祖先。
(树上倍增
一开始统计出每个子树的节点个数_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的更多相关文章
- 【Henu ACM Round#15 F】Arthur and Questions
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] a1+a2+...+ak<a2+a3+...ak+1 ->a1<ak+1 a2+a3+...+ak+1<a3 ...
- 【Henu ACM Round#15 D】Ilya and Escalator
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 概率DP; 设f[i][j]表示前i个单位时间,j个人进入房间的概率是多少 然后想一下和i-1秒的时候要怎么转移就可以了. i-1秒 ...
- 【Henu ACM Round#15 C】 A and B and Team Training
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举第一种方法. 剩下的全都个第二种方法. 看看能组成多少个队伍就可以了. [代码] #include <bits/stdc+ ...
- 【Henu ACM Round#15 B】A and B and Compilation Errors
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 开3个map, 存在map里面: 然后迭代第一个和第二个map; 分别与第二个和第三个map比较就可以了 [代码] #include ...
- 【Henu ACM Round#15 A】 A and B and Chess
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 统计大写和小写的个数. 比较答案.输出即可. [代码] #include <bits/stdc++.h> using n ...
- 【Henu ACM Round#16 A】 Bear and Game
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 看看什么时候t[i]-t[i-1]>15. 输出t[i-1]+15就好. 不存在这样的i就输出min(t[n]+15,90) ...
- 【Henu ACM Round#24 E】Connected Components
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 要求把连续的一段li..ri的边全都删掉. 然后求剩下的图的联通数 如果暴力的话 复杂度显然是O(k*m)级别的. 考虑我们把li. ...
- 【Henu ACM Round#24 D】Iterated Linear Function
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把B提取出来就是一个等比数列了. 求和一下会发现是这种形式. \(B*\frac{(A^n-1)}{A-1}+A^n*x\) 则求一 ...
- 【Henu ACM Round#24 C】Quiz
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 肯定是这样 先放k-1个,然后空1个,然后再放k-1个.然后再空1个.. 以此类推. 然后如果(n/k)*(k-1)+n%k> ...
随机推荐
- 深入C++ new/delete,malloc/free解析
深入C++ new/delete,malloc/free解析 1.malloc与free是C++/C语言的标准库函数.new/delete是C++的运算符. 它们都可用于申请动态内存和释放内存 2.对 ...
- 在 Win8.1 上安装 Dedup
install-package Microsoft-Windows-ServerCore-FullServer-Package~31bf3856ad364e35~amd64~~6.3.9600.163 ...
- h5登录
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta http-equiv="Con ...
- emmet教程
https://www.zfanw.com/blog/zencoding-vim-tutorial-chinese.html https://www.zfanw.com/blog/zencoding- ...
- thinkphp5的Illegal string offset 'id'错误
thinkphp5的Illegal string offset 'id'错误 问题 解答 数组同名了,一个html页面传进来两个cateres的数组,所以在找id的时候不知道找这两个里面的哪一个 第一 ...
- IE9+ BUG汇总
CSS透明没有继承 css opacity is not inherited in internet explorer 今儿遇到一个问题源于同事写的一个页面,发现父级明明 opacity:0 了,但子 ...
- iOS-RAC学习笔记(一)——RACStream
RACStream是RACSignal和RACSequence的父类,定义了一些流的操作方法.从名字上可以看出来,这个对象就像流一样可以往任何一个出口流,同时也可以给这个流设计一道道关卡,改变流(这里 ...
- CF37E Trial for Chief(最短路)
题意 题意是给你一张 NMNMNM 的图,每个点有黑色和白色,初始全为白色,每次可以把一个相同颜色的连续区域染色,求最少的染色次数:(n,m<=50) 题解 转化为最短路.对于每一个点与它相邻的 ...
- 结合Vue 的滚动底部加载
项目手机端分页跳转不理想,自己做了一个滚动加载的一个Demo 核心Dom结构 <body> <div id="Content"> <div> & ...
- 题解 CF327C 【Magic Five】
这道题带坑,假如没有发现肯定会爆. 首先先搜索一遍0和5,储存在数组a里面. 那么应当有2 ^ a1 +2 ^ a2 +...+ 2 ^ an. 然而这道题没那么简单,数串还可以重复k次. 因此,需要 ...