全Wa求助。。。(树剖)
查看原帖
全Wa求助。。。(树剖)
272314
Autonomier楼主2020/7/1 17:55
#include<bits/stdc++.h>
using namespace std;
const int maxn=3e5+1;
inline int read(){
   int s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}
int cnt,n,q;
int head[maxn];
int dep[maxn],siz[maxn],top[maxn],fa[maxn],son[maxn];
struct Eage
{
	int to;
	int nt;
}e[maxn*2];
inline void update(int x,int y)
{
	e[++cnt].nt=head[x];
	e[cnt].to=y;
	head[x]=cnt;
}
inline void dfs1(int rt,int f,int pre_dep)
{
	dep[rt]=pre_dep+1;
	fa[rt]=f;
	siz[rt]=1;
	for(int i=head[rt];i;i=e[i].nt)
	{
		int y=e[i].to;
		if(y==fa[rt])
			continue;
		dfs1(y,rt,dep[rt]);
		siz[rt]+=siz[y];
		if(siz[y]>siz[son[rt]]||son[rt]==0)
			son[rt]=y;
	}
}
inline void dfs2(int rt,int pre_top)
{
	for(int i=head[rt];i;i=e[i].nt)
	{
		int y=e[i].to;
		if(y==fa[rt])
			continue;
		if(y==son[rt])
		{
			top[y]=pre_top;
			dfs2(y,pre_top);
		}
		else
		{
			top[y]=y;
			dfs2(y,y);
		}
	}
}
inline int lca(int x,int y)
{
	while(top[x]!=top[y])
	{
		if(dep[top[x]]>=dep[top[y]])
			x=fa[top[x]];
		else
			y=fa[top[y]];
	}
	return dep[x]<dep[y]? x:y;
}
int main()
{
	n=read(),q=read();
	for(int i=1;i<n;i++)
	{
		int x,y;
		x=read(),y=read();
		update(x,y);
		update(y,x);
	}
	dfs1(1,1,0);
	top[1]=1;
	dfs2(1,1);
//	cout<<lca(8,5)<<endl;
//	cout<<lca(5,1)<<endl;
//	cout<<lca(5,1)<<endl;
	for(int i=1;i<=q;i++)
	{
		int x,y,u,v;
		x=read(),y=read(),u=read(),v=read();
		int t1=lca(x,y);
		int t2=lca(u,v);
		if(dep[t1]>=dep[t2]&&(lca(t1,u)==t1||lca(t1,v)==t1))
			cout<<"Y"<<endl;
		else
			cout<<"N"<<endl;
	}
	return 0;
}
/*8 0
1 2
1 3
2 4
2 5
4 7
4 6
4 8*/
2020/7/1 17:55
加载中...