Codeforces519 E. A and B and Lecture Rooms
传送门:>Here<
题意:询问给出一棵无根树上任意两点$a,b$,求关于所有点$i$,$dist(a,i) = dist(b,i)$的点的数量。要求每一次询问在$O(log n)$的时间复杂度内完成。
解题思路
由于在树上求距离,并且还要$O(log n)$,自然会联想到$LCA$。由于边权是$1$,那么点到根的距离就是该点的深度。这个深度可以在$dfs$预处理的过程中处理完成。那么两个点之间的距离就是两个点到根节点的距离减去两点的LCA到根节点距离的两倍。这个随便yy一下就好了。
得到$a,b$间的距离$D$以后,分类讨论。(设$a$的深度$\geq \ b$的深度)
(1)若$D$为奇数,则一定不存在任何一个点到$a,b$的距离相等。因此得到$0$.
(2)若$D$为偶数:
(一)$a,b$两点分别在$LCA$的两棵子树上。
①$a,b$两点深度相同。此时很简单,最近的一个距离相等的点就是$a,b$的$LCA$。也很容易想到$LCA$的祖先也全都符合。但真的只有这些吗?$LCA$的祖先的其他儿子好像也满足诶……$LCA$的其他子树(除了$a,b$)好像也满足诶……因此我们得到结论,在这种情况下得到的答案应当是$n - size[LCA的左子树] - size[LCA的右子树]$
②深度不同。那么我们找到中间节点$Mid$,$Mid$里除有$a$的子树外其他子树都符合,并且$Mid$以上的节点都不会符合,因此答案是$size[Mid] - size[有a的那棵子树]$
(二)$a,b$在同一条链上,即$b$就是$LCA$
和①类似,中间深度的节点减去含$a$的子树即可
因此我们要做的不过是在$dfs$的过程中维护好$size$和$dep$。但一直困惑我的是有$a$的那个子树怎么快速得到?答案其实很暴力……再倍增一遍……
Code
太坑了!调试了一个多小时竟然是因为$LCA$的预处理dfs中$(1<<i)$打成了$i$,导致$TLE$得莫名其妙。还是$LCA$板子不熟啊……
/** This Program is written by QiXingZhi **/
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#include <cmath>
#define r read()
#define Max(a,b) (((a)>(b)) ? (a) : (b))
#define Min(a,b) (((a)<(b)) ? (a) : (b))
using namespace std;
typedef long long ll;
const int N = ;
const int INF = ;
inline int read(){
int x = ; int w = ; register int c = getchar();
while(c ^ '-' && (c < '' || c > '')) c = getchar();
if(c == '-') w = -, c = getchar();
while(c >= '' && c <= '') x = (x << ) +(x << ) + c - '', c = getchar();
return x * w;
}
int n,x,y,ans;
int f[N][],dep[N],size[N];
vector <int> G[N];
inline void AddEdge(int u, int v){
G[u].push_back(v);
}
void LcaInit(int x, int father, int _d){
dep[x] = _d;
f[x][] = father;
size[x] = ;
for(int i = ; (<<i) <= _d; ++i){
f[x][i] = f[f[x][i-]][i-];
}
int sz,to;
sz = G[x].size();
for(int i = ; i < sz; ++i){
to = G[x][i];
if(to == father) continue;
LcaInit(to,x,_d+);
size[x] += size[to];
}
}
inline int GetDepNode(int x, int _d){
int tmp = x;
for(int i = ; i >= ; --i){
if(dep[tmp]-(<<i) < _d) continue;
tmp = f[tmp][i];
}
return tmp;
}
inline void LCA(int a, int b){
if(dep[a] < dep[b]){
swap(a,b);
}
int _a = a, _b = b;
for(int i = ; i >= ; --i){
if(dep[a]-(<<i) < dep[b]) continue;
a = f[a][i];
}
int LCA;
if(a == b){
LCA = a;
}
else{
for(int i = ; i >= ; --i){
if(f[a][i] == f[b][i]) continue;
a = f[a][i];
b = f[b][i];
}
LCA = f[a][];
}
int Dist = dep[_a]-dep[LCA]+dep[_b]-dep[LCA];
if(Dist & ){
ans = ;
return;
}
else{
if(_b == LCA){
int dep_Mid = (dep[_a] + dep[_b]) / ;
ans = size[GetDepNode(_a,dep_Mid)] - size[GetDepNode(_a,dep_Mid+)];
}
else{
if(dep[_a] != dep[_b]){
int dep_Mid = dep[_a] - (Dist/);
ans = size[GetDepNode(_a,dep_Mid)] - size[GetDepNode(_a,dep_Mid+)];
}
else{
ans = n - size[GetDepNode(_a,dep[LCA]+)] - size[GetDepNode(_b,dep[LCA]+)];
}
}
}
}
int main(){
n = r;
for(int i = ; i < n; ++i){
x = r, y = r;
AddEdge(x,y);
AddEdge(y,x);
}
LcaInit(,,);
int Q = r;
while(Q--){
x = r, y = r;
ans = ;
if(x != y){
LCA(x,y);
printf("%d\n",ans);
}
else{
printf("%d\n",n);
}
}
return ;
}
Codeforces519 E. A and B and Lecture Rooms的更多相关文章
- [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= ...
- 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 ...
- 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 ...
- codeforces 519E A and B and Lecture Rooms LCA倍增
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit Status Prac ...
- 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 ...
- A and B and Lecture Rooms(LCA)
题目描述 A and B are preparing themselves for programming contests. The University where A and B study i ...
- [codeforces 519E]E. A and B and Lecture Rooms(树上倍增)
题目:http://codeforces.com/problemset/problem/519/E 题意:给你一个n个点的树,有m个询问(x,y),对于每个询问回答树上有多少个点和x,y点的距离相等 ...
- Codeforces 519 E. A and B and Lecture Rooms
Description 询问一个树上与两点距离相等的点的个数. Sol 倍增求LCA. 一棵树上距离两点相等,要么就只有两点的中点,要么就是与中点相连的所有点. 有些结论很容易证明,如果距离是偶数,那 ...
- Codeforces 519E A and B and Lecture Rooms [倍增法LCA]
题意: 给你一棵有n个节点的树,给你m次询问,查询给两个点,问树上有多少个点到这两个点的距离是相等的.树上所有边的边权是1. 思路: 很容易想到通过记录dep和找到lca来找到两个点之间的距离,然后分 ...
随机推荐
- leveldb和fork的初始化顺序
我们服务器内用leveldb存一些不是很重要的, 但是又需要(半)持久化的东西. 可是自从2016到现在, 碰见好几次不同类型的死锁. 直到今天, 才发现真正的原因, 那就是leveldb不支持for ...
- Python全栈开发之路 【第七篇】:面向对象编程设计与开发(1)
本节内容 一.编程范式 编程指的是写程序.敲代码,就是指程序员用特定的语法.数据结构和算法编写的代码,目的是来告诉计算机如何执行任务的. 在编程的世界里最常见的两大流派是:面向过程与面向对象.“功夫的 ...
- Python学习第十四篇——类初步使用及面向对象思想
class Restaurant(): def __init__(self,restaurant_name,cuisine_type): self.name = restaurant_name sel ...
- 1177: LFX学橙啦!题解
问题如下:先给你一个含有N个整数的数组数组中的每一个元素只为1或者0而N的大小为1~100你可以删除一些元素(也可以选择不删除),使剩下的数组中,没有一个元素0在1后面出现.并且要使剩下的元素的数量最 ...
- iOS QRcode识别及相册图片二维码读取识别
https://www.jianshu.com/p/48e44fe67c1d 2016.03.30 10:32* 字数 892 阅读 16197评论 5喜欢 34赞赏 1 最近碰到一个用户 在使用我们 ...
- 福州大学软件工程1816 | W班 第10次作业[软件工程实践总结]
作业链接 个人作业--软件工程实践总结 评分细则 本次由五个问题(每个十分)+创意照片(五分)+附加题(十分)组成 评分统计图 千帆竞发图 汇总成绩排名链接 汇总链接
- git [command line] fatal: Authentication failed for
fatal: Authentication failed for https://www.jianshu.com/p/8a7f257e07b8 git.exe fetch -v --progress ...
- react中如何使用动画效果
在react中想要加入动画效果 需要引入 import {CSSTransitionGroup} from 'react-transition-group' //加入react 动画包 import ...
- [转帖]前端-chromeF12 谷歌开发者工具详解 Console篇
前端-chromeF12 谷歌开发者工具详解 Console篇 https://blog.csdn.net/qq_39892932/article/details/82655866 趁着搞 cloud ...
- 谷歌浏览器报错 Active resource loading counts reached to a per-frame
Active resource loading counts reached to a per-frame limit while the tab is in background. Network ...