题目描述

Snow终于得知母亲是谁,他现在要出发寻找母亲。

王国中的路由于某种特殊原因,成为了一棵有n个节点的根节点为1的树,但由于”Birds are everywhere.”,他得到了种种不一样的消息,每份消息中都会告诉他有两棵子树是禁忌之地,于是他向你求助了。

他给出了q个形如”x y”的询问,表示他不能走到x和y的子树中,由于走的路径越长他遇见母亲的概率越大但是他只能走一条不经过重复节点的路径,现在他想知道对于每组询问他能走的最长路径是多少,如果没有,输出零。

输入

第一行两个正整数n和q(1≤n,q≤100000)

第二到第n行每行两个整数u,v表示u和v之间有一条边连接,边的长度为1。

接下来q行每行两个x,y表示一组询问,意义如题目描述。

输出

q行,输出见题目描述

样例输入

5 2

1 3

3 2

3 4

2 5

2 4

5 4

样例输出

1

2

样例解释

询问1中2和4的子树不能走,最长路径为(1,3)长度为1

询问2中5和4的子树不能走,最长路径为(1,3,2)长度为2

数据范围

对于20%的数据1<=n,q<=100

对于50%的数据1<=n,q<=2000

对于100%的数据1≤n≤100000,1<=q<=50000

【题解】

100%

很明显的每个询问就是在求将两棵子树去掉后剩下的树的直径。我们先可以得出该树的dfs序,那么对于一颗子树就变成了序列上的一个区间,那么我们可以用线段树,维护一个区间表示的点的直径,对于两个区间,直径的合并就是从四个端点中任选两个连成的路径,选出其中长度最长的,即为合并后的直径,时间复杂度O(nlog2 n)。

不过,听学长说,题解给的是最慢的方法,还可以用线段树搞,LCT搞。。。(有时间再弄吧)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#define lson num<<1
#define rson num<<1|1
using namespace std;
const int N=300010;
bool flg;
int cnt,tt;
int w[N],id[N],dfn[N],ed[N],head[N],to[N],nxt[N];
inline int gi() {
int x=0,o=1;
char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') ch=getchar(),o=-1;
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*o;
}
struct Tree {
int tr,l1,l2,r1,r2,a,b;
inline void init(int x) {
tr=a=b=0;
l1=l2=r1=r2=-1<<30;
if(w[x]==1) b=1;
else if(w[x]==-1) a=1;
else l1=l2=r1=r2=0;
}
inline void merge(Tree &x,Tree &y) {
a=max(x.a,x.a+y.a-x.b);
b=max(y.b,y.b+x.b-y.a);
l1=max(x.l1,max(x.a-x.b+y.l1,x.a+x.b+y.l2));
r1=max(y.r1,max(y.b-y.a+x.r1,y.a+y.b+x.r2));
l2=max(x.l2,y.l2+x.b-x.a);
r2=max(y.r2,x.r2+y.a-y.b);
tr=max(x.tr,max(y.tr,max(x.r1+y.l2,x.r2+y.l1)));
}
} T[N*4],A,B;
inline void dfs(int x,int fa) {
if(x!=1) w[dfn[x]=++cnt]=1;
id[x]=++cnt;
for(int i=head[x];i;i=nxt[i])
if(to[i]!=fa) dfs(to[i],x);
if(x!=1) w[ed[x]=++cnt]=-1;
}
inline void build(int l,int r,int num) {
if(l==r) {T[num].init(l);return;}
int mid=(l+r)>>1;
build(l,mid,lson),build(mid+1,r,rson);
T[num].merge(T[lson],T[rson]);
}
inline void query(int l,int r,int L,int R,int num) {
if(L>R) return;
if(L<=l&&r<=R) {
if(!flg) A=B=T[num],flg=1;
else A.merge(B,T[num]),B=A;
return;
}
int mid=(l+r)>>1;
if(L<=mid) query(l,mid,L,R,lson);
if(R>mid) query(mid+1,r,L,R,rson);
}
int main() {
freopen("snow.in","r",stdin);
freopen("snow.out","w",stdout);
int n,q,x,y;
cin>>n>>q;
for(int i=1;i<n;i++) {
x=gi(),y=gi();
to[++tt]=y,nxt[tt]=head[x],head[x]=tt;
to[++tt]=x,nxt[tt]=head[y],head[y]=tt;
}
dfs(1,0);
build(1,cnt,1);
while(q--) {
x=gi(),y=gi();
if(dfn[x]>dfn[y]) swap(x,y);
if(x==1) {puts("0");continue;}
flg=0;
query(1,cnt,1,dfn[x]-1,1);
if(id[y]>=dfn[x]&&id[y]<=ed[x]) query(1,cnt,ed[x]+1,cnt,1);
else query(1,cnt,ed[x]+1,dfn[y]-1,1),query(1,cnt,ed[y]+1,cnt,1);
printf("%d\n",A.tr);
}
return 0;
}

最新文章

  1. [bzoj3932][CQOI2015][任务查询系统] (主席树)
  2. tomcat部署web应用的4种方法
  3. linux hugepage
  4. PostScript的简单例子-用粗线画一个圆
  5. DataTable转List&lt;Model&gt;通用类
  6. iso中AutoLayout和Autoresizing Mask的区别
  7. python常错: join() 方法
  8. C# 网卡IP(网上资料整理)
  9. UVA 11134 Fabled Rooks
  10. POJ3307+找规律
  11. Android学习笔记:多个AsyncTask实例的并发问题
  12. 斯坦福ML公开课笔记14——主成分分析
  13. STM32的USART DMA传输(转)
  14. C# Linq基本常用用法
  15. nyoj 对决
  16. springboot 2.1.4 源码默认logback-spring.xml
  17. 算法-动态规划 Dynamic Programming--从菜鸟到老鸟
  18. [Memcached] telnet命令
  19. MEF 插件式开发之 DotNetCore 中强大的 DI
  20. 使用Eclipse Memory Analyzer 进行JAVA内存泄露分析

热门文章

  1. 用Docker构建MySQL镜像
  2. RESTful API设计的简单例子
  3. scala学习(1)----map和flatMap的区别
  4. XGBoost参数中文翻译以及参数调优
  5. Ztree加载完成后显示勾选节点
  6. 基于虚拟机的centos6.5 搭建本地光盘yum源
  7. linux网络不通,如何解决
  8. 1002 A+B for Polynomials (PAT (Advanced Level) Practice)
  9. ssh中将常用的命令做别名
  10. LINUX-用户和群组