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来找到两个点之间的距离,然后分 ...
随机推荐
- H5 19-序选择器下
19-序选择器下 我是项目 我是项目 我是项目 我是项目 我是项目 我是项目 我是项目 <!DOCTYPE html> <html lang="en"> & ...
- 最长递增子序列(lis)最长公共子序列(lcs) 最长公共上升子序列(lics)
lis: 复杂度nlgn #include<iostream> #include<cstdio> using namespace std; ],lis[],res=; int ...
- python模块详解
什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写的代码(.p ...
- jdk环境变量配置注意事项
cmd 运行java -version 显示错误 Registry key 'Software\JavaSoft\Java Runtime Environment\CurrentVersion'has ...
- Linux下面将windows写的脚本转换成 Linux 格式的文件
1.接着上一篇blog 发现有一个问题 vim 打开文件 发现最下面有一行信息: 2. 里面有一些 不一样的地方. linux里面没法执行 ,如果想执行的话 可以输入命令 :set fileforma ...
- 逻辑斯特回归tensorflow实现
calss #!/usr/bin/python2.7 #coding:utf-8 from __future__ import print_function import tensorflow as ...
- 网络编程--使用UDP发送接收数据
package com.zhangxueliang.udp; import java.io.IOException; import java.net.DatagramPacket; import ja ...
- Oracle创建表空间、用户以及给用户赋权
--创建表空间 create tablespace waterboss datafile 'd:\waterboss.dbf' size 100m autoextend on next 10m --创 ...
- AngularJS基于模块化的MVC实现
AngularJS基于模块化的MVC实现 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&qu ...
- 日期选择器date、week、time、datetime、datetime-local类型
下面只写两个类型的代码案例,其他都大同小异 date类型: <!DOCTYPE html> <html> <head> <meta charset=" ...