# 相遇【省选模拟赛】

## 树上路径求交

Posted by yjjr's blog on March 16, 2018

# 分析

• 同向：判断最大边长度和他们的时间差
• 相向：判断是否会在端点相遇，若不在端点相遇，那么一定会在边上相遇

# code

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
#define reg(x) for(int i=last[x];i;i=e[i].next)
using namespace std;
ll f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int maxn=1e5+6;
int n,Q,cnt,last[maxn],deep[maxn],fa[maxn][18],g[maxn][18];
ll dis[maxn];
struct edge{int to,next,w;}e[maxn<<2];
void insert(int u,int v,int w){
e[++cnt]=(edge){v,last[u],w};last[u]=cnt;
e[++cnt]=(edge){u,last[v],w};last[v]=cnt;
}
void dfs(int x,int Fa){
deep[x]=deep[Fa]+1;fa[x][0]=Fa;
rep(i,1,17)
if(fa[x][i-1]&&fa[fa[x][i-1]][i-1]){
fa[x][i]=fa[fa[x][i-1]][i-1];
g[x][i]=max(g[x][i-1],g[fa[x][i-1]][i-1]);
}
reg(x){
if(e[i].to==Fa)continue;
g[e[i].to][0]=e[i].w;
dis[e[i].to]=dis[x]+e[i].w,dfs(e[i].to,x);
}
}
inline int lca(int x,int y){
if(deep[x]<deep[y])swap(x,y);
int t=deep[x]-deep[y];
rep(i,0,17)
if((1<<i)&t)x=fa[x][i];
dep(i,17,0)
if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
if(x==y)return x;else return fa[x][0];
}
inline int query(int x,int y){
if(deep[x]<deep[y])swap(x,y);
int re=0;
dep(i,17,0)
if(fa[x][i]&&deep[fa[x][i]]>=deep[y]){
re=max(re,g[x][i]);
x=fa[x][i];
}
if(x==y)return re;
dep(i,17,0)
if(fa[x][i]&&fa[y][i]&&fa[x][i]!=fa[y][i]){
re=max(re,max(g[x][i],g[y][i]));
x=fa[x][i],y=fa[y][i];
}
re=max(re,max(g[x][0],g[y][0]));
return re;
}
inline bool find(int x,ll s){
s=dis[x]-s;
dep(i,17,0)if(fa[x][i]&&dis[fa[x][i]]>=s)x=fa[x][i];
if(dis[x]==s)return 1;else return 0;
}
void printans0(int x,int y,ll t1,ll t2){
if(x==y){puts("NO");return;}
int s=query(x,y);
if(abs(t1-t2)<s)puts("YES");else puts("NO");
}
void printans1(int x,int y,ll t1,ll t2){
if(x==y){puts("NO");return;}
int l=lca(x,y);ll s=dis[x]+dis[y]-dis[l]*2,S=s+t2-t1;
if(S<0||S>s*2){puts("NO");return;}
if(S&1){puts("YES");return;}
S/=2;bool flag=0;
if(dis[x]-dis[l]>=S)flag=find(x,S);else flag=find(y,s-S);
if(flag)puts("NO");else puts("YES");
}
int main(){
rep(i,2,n){
insert(u,v,w);
}
dfs(1,0);//rep(i,1,n)cout<<deep[i]<<' ';cout<<endl;
while(Q--){
int lx1y1=lca(x1,y1),lx2y2=lca(x2,y2);
if(deep[lx1y1]>deep[lx2y2]){swap(t1,t2);swap(x1,x2);swap(y1,y2);swap(lx1y1,lx2y2);}
int lx1x2=lca(x1,x2),lx1y2=lca(x1,y2),ly1x2=lca(y1,x2),ly1y2=lca(y1,y2);
if(lx1y1==lx2y2){
int L=lx1y1;
if(lx1x2==L&&ly1y2==L)printans1(lx1y2,ly1x2,t1+dis[x1]-dis[lx1y2],t2+dis[x2]-dis[ly1x2]);
else printans0(lx1x2,ly1y2,t1+dis[x1]-dis[lx1x2],t2+dis[x2]-dis[lx1x2]);
continue;
}
int lll=lca(lx1y1,lx2y2);
if(lll!=lx1y1){puts("NO");continue;}
if(lca(x1,lx2y2)==lx2y2){
int L=lx2y2;
if(lx1x2==L)printans1(lx1y2,L,t1+dis[x1]-dis[lx1y2],t2+dis[x2]-dis[L]);
else printans0(lx1x2,L,t1+dis[x1]-dis[lx1x2],t2+dis[x2]-dis[lx1x2]);
continue;
}
if(lca(y1,lx2y2)==lx2y2){
int L=lx2y2;
if(ly1y2==L)printans1(L,ly1x2,t1+dis[x1]+dis[L]-dis[lll]*2,t2+dis[x2]-dis[ly1x2]);
else printans0(L,ly1y2,t1+dis[x1]+dis[L]-dis[lll]*2,t2+dis[x2]-dis[L]);
continue;
}
puts("NO");
}
}