对于询问(x,y),恰经过一条非树边且不经过树上两点间路径的路径数为:

·若x与y成祖先-孩子关系,假设y是x的祖先,z是y到x方向的第一个节点,则

ans=起点在x的子树里,且终点不在z的子树里的非树边数

·若x与y不成祖先-孩子关系,则

ans=起点在x的子树里,且终点在y的子树里的非树边数

由于不经过任何非树边也有一解,所以答案要加1

求出DFS序后线段树合并即可解决

#include<cstdio>
const int N=100010,BUF=10500100;
int n,m,i,x,y,ans;char Buf[BUF],*buf=Buf;
int g[N],nxt[N<<1],v[N<<1],ed,d[N],size[N],son[N],top[N],f[N],st[N],en[N],dfn;
int G[N],NXT[N<<1],V[N<<1],sum[N];
struct Node{int v;Node*l,*r;}pool[7200010],*cur=pool,*T[N];
inline void read(int&a){for(a=0;*buf<48;buf++);while(*buf>47)a=a*10+*buf++-48;}
inline void swap(int&x,int&y){int z=x;x=y;y=z;}
inline void add(int x,int y){v[++ed]=y;nxt[ed]=g[x];g[x]=ed;}
inline void ADD(int x,int y){V[++ed]=y;NXT[ed]=G[x];G[x]=ed;}
void dfs(int x){
size[x]=1;
for(int i=g[x];i;i=nxt[i])if(v[i]!=f[x]){
d[v[i]]=d[f[v[i]]=x]+1;dfs(v[i]);size[x]+=size[v[i]];
if(size[v[i]]>size[son[x]])son[x]=v[i];
}
}
void dfs2(int x,int y){
st[x]=++dfn;top[x]=y;
if(son[x])dfs2(son[x],y);
for(int i=g[x];i;i=nxt[i])if(v[i]!=son[x]&&v[i]!=f[x])dfs2(v[i],v[i]);
en[x]=dfn;
}
inline int lca2(int x,int y){
int t;
while(top[x]!=top[y])t=top[y],y=f[top[y]];
return x==y?t:son[x];
}
Node*merge(Node*x,Node*y,int a,int b){
if(!x)return y;
if(!y)return x;
Node*z=cur++;z->v=x->v+y->v;
if(a==b)return z;
int mid=(a+b)>>1;
return z->l=merge(x->l,y->l,a,mid),z->r=merge(x->r,y->r,mid+1,b),z;
}
void ins(Node*&x,int a,int b,int c){
if(!x)x=cur++;x->v++;
if(a==b)return;
int mid=(a+b)>>1;
if(c<=mid)ins(x->l,a,mid,c);else ins(x->r,mid+1,b,c);
}
int ask(Node*x,int a,int b,int c,int d){
if(!x)return 0;
if(c<=a&&b<=d)return x->v;
int mid=(a+b)>>1,t=0;
if(c<=mid)t=ask(x->l,a,mid,c,d);
if(d>mid)t+=ask(x->r,mid+1,b,c,d);
return t;
}
void dfs3(int x){
T[x]=cur++;
for(int i=G[x];i;i=NXT[i])ins(T[x],1,n,V[i]),sum[x]++;
for(int i=g[x];i;i=nxt[i])if(v[i]!=f[x])dfs3(v[i]),T[x]=merge(T[x],T[v[i]],1,n),sum[x]+=sum[v[i]];
}
int main(){
fread(Buf,1,BUF,stdin),read(n);
for(i=1;i<n;i++)read(x),read(y),add(x,y),add(y,x);
for(dfs(1),dfs2(1,1),ed=0,read(m);m--;ADD(x,st[y]),ADD(y,st[x]))read(x),read(y);
for(dfs3(1),read(m);m--;printf("%d\n",ans+1)){
read(x),read(y);
if(d[x]<d[y])swap(x,y);
if(st[y]<=st[x]&&en[x]<=en[y])y=lca2(y,x),ans=sum[x]-ask(T[x],1,n,st[y],en[y]);
else ans=ask(T[x],1,n,st[y],en[y]);
}
return 0;
}

  

BZOJ3488 : [ONTAK2010]Highways的更多相关文章

  1. 【BZOJ3488】[ONTAK2010]Highways 扫描线+树状数组

    [BZOJ3488][ONTAK2010]Highways Description 给一棵n个点的树以及m条额外的双向边q次询问,统计满足以下条件的u到v的路径:恰经过一条额外的边不经过树上u到v的路 ...

  2. 【bzoj3488】[ONTAK2010]Highways DFS序+树上倍增+树状数组

    题目描述 一棵n个点的树,给定m条路径,q次询问包含一条路径的给定路径的个数+1 输入 The first line of input contains a single integer N(1< ...

  3. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  4. H:Highways

    总时间限制: 1000ms 内存限制: 65536kB描述The island nation of Flatopia is perfectly flat. Unfortunately, Flatopi ...

  5. Highways(prim & MST)

    Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23421   Accepted: 10826 Descri ...

  6. poj2485 Highways

    Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...

  7. poj 2485 Highways 最小生成树

    点击打开链接 Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19004   Accepted: 8815 ...

  8. poj 2485 Highways

    题目连接 http://poj.org/problem?id=2485 Highways Description The island nation of Flatopia is perfectly ...

  9. 【BZOJ】【3550】【ONTAK2010】Vacation

    网络流/费用流 Orz太神犇了这题…… 我一开始想成跟Intervals那题一样了……每个数a[i]相当于覆盖了(a[i]-n,a[i]+n)这个区间……但是这样是错的!!随便就找出反例了……我居然还 ...

随机推荐

  1. [codeforces 528]B. Clique Problem

    [codeforces 528]B. Clique Problem 试题描述 The clique problem is one of the most well-known NP-complete ...

  2. 从零开始写一个武侠冒险游戏-8-用GPU提升性能(3)

    从零开始写一个武侠冒险游戏-8-用GPU提升性能(3) ----解决因绘制雷达图导致的帧速下降问题 作者:FreeBlues 修订记录 2016.06.23 初稿完成. 2016.08.07 增加对 ...

  3. 最短路&&最小生成树水题

    训练赛20151122 5:00:00     Overview Problem Status Rank Discuss Current Time: 2015-11-23 17:33:18 Conte ...

  4. jquery博客收集的IE6中CSS常见BUG全集及解决方案

    今天的样式调的纠结,一会这边一会那么把jquery博客折腾的头大,浏览器兼容性.晚上闲着收集一些常见IE6中的BUG 3像素问题及解决办法 当使用float浮动容器后,在IE6下会产生3px的空隙,有 ...

  5. MyBatis3: There is no getter for property named 'code' in 'class java.lang.String'

    mybatis3  : mysql文如下,传入参数为string类型时‘preCode’,运行报错为:There is no getter for property named 'preCode' i ...

  6. cas单点注销失败Error Sending message to url endpoint

    最近在做cas单点登录时,由于是单点登录.必然会涉及到单点注销,然而在做单点注销时由于对cas注销机制不了解加之测试条件所致,所有测试都是在本机下完成(机器性能较低,没用虚拟机):导致折腾了很久.网上 ...

  7. 设置windows网络连接别名和linux网络连接别名

    windows网络连接别名 C:\Windows\System32\drivers\etc目录下的hosts文件中添加 127.0.0.1 localhost 192.168.1.100 proxy. ...

  8. 利用 FFmpeg 和 ImageMagick, AVI 转 GIF(不失真)

    利用[TMPGEnc 4.0 XPress] 或 [TMPGEnc Video Mastering Works 5] 生成 AVI 这个视频编辑软件,可对每个帧进行操作 1.生成每个帧的 PNG ff ...

  9. Java for LeetCode 154 Find Minimum in Rotated Sorted Array II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  10. Java for LeetCode 046 Permutations

    Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the follo ...