Codeforces 519 E. A and B and Lecture Rooms
Description
询问一个树上与两点距离相等的点的个数.
Sol
倍增求LCA.
一棵树上距离两点相等,要么就只有两点的中点,要么就是与中点相连的所有点.
有些结论很容易证明,如果距离是偶数,那么他们没有中点,树上不存在距离两点相等的点.
如果中点恰好是两点LCA,那么答案就是\(n-size_x-size_y\) ,\(size_x\) 和 \(size_y\) 表示LCA的子节点中子树包含 \(u,v\) 的子节点的\(size\)
如果不是LCA,那么答案就是 \(size_{LCA}-size_x\) 是深度靠下的点.
PS:Loli的破机子跑的真tm慢.
Code
#include<cstdio>
#include<utility>
#include<vector>
#include<iostream>
using namespace std; #define mpr(a,b) make_pair(a,b)
#define debug(a) cout<<#a<<"="<<a<<" "
const int N = 100005;
const int M = 21; int n;
int f[N][M],d[N],sz[N],pow2[M];
vector<int> g[N]; inline int in(int x=0,char ch=getchar()){ while(ch>'9'|| ch<'0') ch=getchar();
while(ch>='0' && ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x; }
void DFS(int u,int fa,int dep){
f[u][0]=fa,sz[u]=1,d[u]=dep;
for(int i=0,v;i<g[u].size();i++) if((v=g[u][i])!=fa){
DFS(v,u,dep+1);
sz[u]+=sz[v];
}
}
pair<int,int> LCA(int u,int v){
if(d[u]<d[v]) swap(u,v);
int l=d[u]-d[v],res=0;
for(int i=0;i<M;i++){
if(l&pow2[i]) res=res+pow2[i],u=f[u][i];
}
if(u==v) return mpr(res,u);
for(int i=M-1;~i;i--) if(f[u][i]!=f[v][i]) u=f[u][i],v=f[v][i],res+=pow2[i]*2;
return mpr(res+2,f[u][0]);
}
int Getp(int u,int d){
for(int i=0;i<M;i++) if(d & pow2[i]) u=f[u][i];
return u;
}
void solve(int u,int v){
if(d[u]<d[v]) swap(u,v);
pair<int,int> pr=LCA(u,v); // debug(pr.first),debug(pr.second)<<endl; if(u==v){ printf("%d\n",n);return; }
if(pr.first & 1){ printf("%d\n",0);return; } int mid=Getp(u,pr.first/2);
int uu=Getp(u,pr.first/2-1),vv=Getp(v,pr.first/2-1); // debug(mid),debug(uu),debug(vv)<<endl; if(mid == pr.second){
printf("%d\n",n-sz[uu]-sz[vv]);
}else{
printf("%d\n",sz[mid]-(f[uu][0]==mid ? sz[uu] : 0) - (f[vv][0]==mid ? sz[vv] : 0));
}
}
int main(){ n=in();
for(int i=1,u,v;i<n;i++) u=in(),v=in(),g[u].push_back(v),g[v].push_back(u);
pow2[0]=1;for(int i=1;i<M;i++) pow2[i]=pow2[i-1]<<1; DFS(1,0,1);
for(int j=1;j<M;j++) for(int i=1;i<=n;i++) f[i][j]=f[f[i][j-1]][j-1]; for(int m=in(),u,v;m--;) u=in(),v=in(),solve(u,v);
return 0;
}
Codeforces 519 E. A and B and Lecture Rooms的更多相关文章
- [codeforces 519E]E. A and B and Lecture Rooms(树上倍增)
题目:http://codeforces.com/problemset/problem/519/E 题意:给你一个n个点的树,有m个询问(x,y),对于每个询问回答树上有多少个点和x,y点的距离相等 ...
- 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 ...
- [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
http://codeforces.com/contest/519/problem/E 题意: 给出一棵树和m次询问,每次询问给出两个点,求出到这两个点距离相等的点的个数. 思路: lca...然后直 ...
- 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 [倍增法LCA]
题意: 给你一棵有n个节点的树,给你m次询问,查询给两个点,问树上有多少个点到这两个点的距离是相等的.树上所有边的边权是1. 思路: 很容易想到通过记录dep和找到lca来找到两个点之间的距离,然后分 ...
- 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 ...
- CodeForces 519E 树形DP A and B and Lecture Rooms
给出一棵树,有若干次询问,每次询问距两个点u, v距离相等的点的个数. 情况还挺多的,少侠不妨去看官方题解.^_^ #include <iostream> #include <cst ...
随机推荐
- (转)Java字符串
转自:http://blog.sina.com.cn/s/blog_899678b90101brz0.html 创建字符串有两种方式:两种内存区域(字符串池,堆)1," " 引号创 ...
- codeforces 709B Checkpoints
题目链接:http://codeforces.com/problemset/problem/709/B 题目大意: 第一行给出两个数 n,x.第二行 输入 n 个数. 要求:从x位置遍历 n-1 个位 ...
- web页面的加载顺序
1.页面顺序 一个典型的web页面由于三个部分组成:html.css和JS.执行的顺序是: 在构造完HTML的dom结构时.触发DOMContentLoaded事件. 整个执行过程安装html的顺序来 ...
- 找到一款不错的网站压力测试工具webbench
webbench最多可以模拟3万个并发连接去测试网站的负载能力,个人感觉要比Apache自带的ab压力测试工具好,安装使用也特别方便. 1.适用系统:Linux 2.编译安装: 引用 wget htt ...
- 解决 linux下编译make文件报错“/bin/bash^M: 坏的解释器:没有那个文件或目录” 问题
PS背景:我在公司做sdk 的pc端开发,所以经常会在win下编译通过之后跑到linux下再运行一次已确保能支持多平台. 今儿在win下跑完一程序,然后放到linux下跑的时候,我用指令: [plai ...
- <meta>标签元素的属性理解
meta是用来在HTML文档中模拟HTTP协议的响应头报文.meta 标签用于网页的<head>与</head>中,meta 标签的用处很多.meta 的属性有两种:name和 ...
- Python基础之--常用模块
Python 模块 为了实现对程序特定功能的调用和存储,人们将代码封装起来,可以供其他程序调用,可以称之为模块. 如:os 是系统相关的模块:file是文件操作相关的模块:sys是访问python解释 ...
- Python开发【第十七篇】:MySQL(一)
一.概述 1.什么是数据库 ? 答:数据的仓库,如:在ATM的示例中我们创建了一个 db 目录,称其为数据库 2.什么是 MySQL.Oracle.SQLite.Access.MS SQL Serve ...
- NSURLSession & NSCache
用于替代 NSURLConnection 支持后台运行的网络任务 暂停.停止.重启网络任务,不再需要 NSOperation 封装 请求可以使用同样的配置容器 不同的 session 可以使用不同的私 ...
- 【转】Kafka producer原理 (Scala版同步producer)
转载自:http://www.cnblogs.com/huxi2b/p/4583249.html 供参考 本文分析的Kafka代码为kafka-0.8.2.1.另外,由于Kafka目前提供了两 ...