WA #29 求调
查看原帖
WA #29 求调
701408
lao_wang楼主2024/8/31 14:17
#include<bits/stdc++.h>
#define N 2123456
using namespace std ;
int n , T , dep[N] , son[N] , sz[N] , top[N] , dfs[N] , fa[N] , head[N] , cnt=1 , tot=0 ;
struct user8341006{
	int a , b , c ;
}q[N];
struct node{
	int to , next ;
}e[N];
void use(int u,int v){
	e[cnt].to = v ;
	e[cnt].next = head[u] ;
	head[u] = cnt++ ;
}
void newnet(int u,int v){
	use(u,v) ;
	use(v,u) ;
}
void dfs1(int u,int f){
	fa[u] = f ;
	dep[u] = dep[f]+1 ;
	sz[u]=1 ;
	for(int i=head[u];i;i=e[i].next){
		int x=e[i].to ;
		if(x==f) continue ;
		dfs1(x,u) ;
		sz[u] += sz[x] ;
		if(!son[u]||sz[son[u]]<sz[x]) son[u] = x ;
	}
}
void dfs2(int u,int t){
	top[u] = t ;
	dfs[u] = ++tot ;
	if(son[u]) dfs2(son[u],t) ;
	for(int i=head[u];i;i=e[i].next){
		int x=e[i].to ;
		if(son[u]==x||x==fa[u]) continue ;
		dfs2(x,x) ;
	}
}
int lca_long(int x,int y){
	int ans=0 ;
	while(top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]]) swap(x,y) ;
		ans += dfs[x]-dfs[top[x]]+1 ;
		x = fa[top[x]] ;
	}
	if(dep[x]>dep[y]) swap(x,y) ;
	ans += dfs[y]-dfs[x]+1 ;
	return ans ;
}
bool cmp(tlh a,tlh b){
	return a.c>b.c ;
}
struct nnode{
	int num , l , r ;
	bool lazy1 , lazy2 ;
}tr[N<<2];
void push_up(int x){
	tr[x].num = tr[x<<1].num+tr[x<<1|1].num ;
}
void push_down(int x){
	if(tr[x].lazy1){
		tr[x<<1].num = tr[x<<1].r-tr[x<<1].l+1 ;
		tr[x<<1|1].num = tr[x<<1|1].r-tr[x<<1|1].l+1 ;
		tr[x<<1].lazy1 = tr[x<<1|1].lazy1 = 1 ;
		tr[x<<1].lazy2 = tr[x<<1|1].lazy2 = 0 ;
		tr[x].lazy1 = 0 ;
	}else if(tr[x].lazy2){
		tr[x<<1].num = tr[x<<1|1].num = 0 ;
		tr[x<<1].lazy2 = tr[x<<1|1].lazy2 = 1 ;
		tr[x].lazy2 = 0 ;
		tr[x<<1].lazy1 = tr[x<<1|1].lazy1 = 0 ;
	}
}
void made(int num,int l,int r){
	tr[num].l = l ;
	tr[num].r = r ;
	if(l==r) return ;
	int mid=(l+r)>>1 ;
	made(num<<1,l,mid) ;
	made(num<<1|1,mid+1,r) ;
}
void change(int num,int l,int r){
	if(tr[num].l>=l&&tr[num].r<=r){
		tr[num].num = tr[num].r-tr[num].l+1 ;
		tr[num].lazy1 = 1 ;
		tr[num].lazy2 = 0 ;
		return ;
	}
	push_down(num) ;
	int mid=(tr[num].l+tr[num].r)>>1 ;
	if(mid>=l) change(num<<1,l,r) ;
	if(mid<r) change(num<<1|1,l,r) ;
	push_up(num) ;
}
int ask(int num,int l,int r){
	if(tr[num].l>=l&&tr[num].r<=r) return tr[num].num ;
	push_down(num) ;
	int mid=(tr[num].l+tr[num].r)>>1 , ans=0 ;
	if(mid>=l) ans += ask(num<<1,l,r) ;
	if(mid<r) ans += ask(num<<1|1,l,r) ;
	return ans ;
}
void LCA_change(int x,int y){
	while(top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]]) swap(x,y) ;
		change(1,dfs[top[x]],dfs[x]) ;
		x = fa[top[x]] ;
	}
	if(dep[x]>dep[y]) swap(x,y) ;
	change(1,dfs[x],dfs[y]) ;
}
int LCA_ask(int x,int y){
	int ans=0 ;
	while(top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]]) swap(x,y) ;
		ans += ask(1,dfs[top[x]],dfs[x]) ;
		x = fa[top[x]] ;
	}
	if(dep[x]>dep[y]) swap(x,y) ;
	ans += ask(1,dfs[x],dfs[y]) ;
	return ans ;
}
signed main(){
	cin >> n >> T ;
	for(int i=1;i<n;i++){
		int u , v ;
		scanf("%d%d",&u,&v) ;
		newnet(u,v) ;
	}
	dfs1(1,0) ;
	dfs2(1,1) ;
	made(1,1,n) ;
	while(1){
		int m=T ;
		bool ant=1 ;
		for(int i=1;i<=m;i++) scanf("%d%d",&q[i].a,&q[i].b) , q[i].c = lca_long(q[i].a,q[i].b) ;
		sort(q+1,q+1+m,cmp) ;
		for(int i=1;i<=m;i++){
			int temp=LCA_ask(q[i].a,q[i].b) ;
			if(temp&&temp!=q[i].c){
				ant=0 ;
				break ;
			}
			LCA_change(q[i].a,q[i].b) ;
		}
		if(!ant) puts("No") ;
		else puts("Yes") ;
		break ;
	}
	return 0 ;
}
2024/8/31 14:17
加载中...