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 ...
随机推荐
- easyUI Form表单的密码验证是否相同
一.js文件中的代码: $(function(){ $.extend($.fn.validatebox.defaults.rules, { equals: {//定义一个比较相等与否的函数 valid ...
- C# 文件操作大全
1.创建文件夹//using System.IO;Directory.CreateDirectory(%%1); 2.创建文件//using System.IO;File.Create(%%1); 3 ...
- 自然语言18.1_Named Entity Recognition with NLTK
QQ:231469242 欢迎nltk爱好者交流 https://www.pythonprogramming.net/named-entity-recognition-nltk-tutorial/?c ...
- Java数据库——JDBC 2.0操作
可滚动的结果集 让结果集滚动起来 //================================================= // File Name : JDBC20_demo //-- ...
- Spot光照资料
http://forums.autodesk.com/t5/FBX-SDK/EmissiveFactor-AmbientFactor-DiffuseFactor/td-p/4230572http:// ...
- JS判断是否是IE浏览器
前最短的IE判定借助于IE不支持垂直制表符的特性搞出来的. var ie = !+"\v1"; 仅仅需要7bytes!参见这篇文章,<32 bytes, ehr ... 9, ...
- 学海无涯的整理Ing..........
1.文章:Understanding JavaScript Function Invocation and “this” http://yehudakatz.com/2011/08/11/unders ...
- basePath = request.getScheme()+"://"+request.getServerName()+":"+r
basePath = request.getScheme()+"://"+request.getServerName()+":"+r (2014-06-30 1 ...
- Winform打包工具SetupFactory 9 的使用
写了个WinForm的小程序..以前没打过包..只是直接把Bin里的东西复制出来使用..自己使用是足够.但是发给别人毕竟不太好看(不牛逼)..所以就想着打包.. Vs2012自带的有打包的功能..相信 ...
- select2搜索框查询加遍历
<div class="form-group"> <label class="control-label col-sm-1 no-padding-rig ...