请教为什么会re
查看原帖
请教为什么会re
684285
Makerlike楼主2024/8/27 07:52

样例没过,但是神奇的AC了20分,求问为啥会re

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e6+10;
int T,col,n,m;
struct edge{
	int v,nxt;
}e[N];
int hd[N],tt;
void init();
void add(int x,int y){
	e[++tt].v=y;
	e[tt].nxt=hd[x];
	hd[x]=tt;
}
int fa[N],son[N],siz[N],dep[N];
void dfs1(int u){
	siz[u]=1;
	for(int i=hd[u];~i;i=e[i].nxt){
		int v=e[i].v;
		if(v==fa[u])
			continue;
		fa[v]=u,dep[v]=dep[u]+1;
		dfs1(v);
		siz[u]+=siz[v];
		if(siz[v]>siz[son[u]])
			son[u]=v;
	}
}
int top[N],rk[N],ic;
void dfs2(int u,int t){
	top[u]=t;
	rk[++ic]=u;
	if(son[u])
		dfs2(son[u],t);
	for(int i=hd[u];~i;i=e[i].nxt){
		int v=e[i].v;
		if(v==fa[u]||v==son[u])
			continue;
		dfs2(v,v);
	}
}

struct tree{
	int lc,rc,v;
	tree(){}
	tree(int _lc,int _rc,int _v){
		lc=_lc,rc=_rc,v=_v;
	}
	void clear(){
		v=lc=rc=0;
	}
}tr[N<<2];
#define ls p<<1
#define rs p<<1|1
int lz[N<<2];
tree merge(tree A,tree B){
	return tree(A.lc,B.rc,A.v+B.v+(A.rc==B.lc));
}
void spread(int p,int l,int r){
	if(lz[p]!=0){
		int mid=(l+r)>>1;
		tr[ls]=tree(lz[p],lz[p],mid-l);
		tr[rs]=tree(lz[p],lz[p],r-mid-1);
		lz[ls]=lz[rs]=lz[p];
		lz[p]=0;
	}
}
void build(int p,int l,int r){
	tr[p].clear();
	lz[p]=0;
	if(l==r)
		return;
	int mid=(l+r)>>1;
	build(ls,l,mid);
	build(rs,mid+1,r);
}
void update(int p,int l,int r,int x,int y,int c){
	if(x<=l&&y>=r){
		tr[p]=tree(c,c,r-l),lz[p]=c;
		return;
	}
	spread(p,l,r);
	int mid=(l+r)>>1;
	if(x<=mid)
		update(ls,l,mid,x,y,c);
	if(y>mid)
		update(rs,mid+1,r,x,y,c);
	tr[p]=merge(tr[ls],tr[rs]);
}
tree query(int p,int l,int r,int x,int y){
	if(x<=l&&y>=r)
		return tr[p];
	spread(p,l,r);
	int mid=(l+r)>>1;
	if(y<=mid)
		return query(ls,l,mid,x,y);
	if(x>mid)
		return query(rs,mid+1,r,x,y);
	return merge(query(ls,l,mid,x,y),query(rs,mid+1,r,x,y));
}
void update_way(int x,int y,int c){
	while(top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]])
			swap(x,y);
		update(1,1,n,rk[top[x]],rk[x],c);
		x=fa[top[x]];
	}
	if(dep[x]>dep[y])
		swap(x,y);
	update(1,1,n,rk[x],rk[y],c);
}
int query_way(int x,int y){
	tree ans1=tree(0,0,0),ans2=tree(0,0,0),tmp;
	bool fl=0;
	while(top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]])
			x^=y^=x^=y,fl^=1;
		tmp=query(1,1,n,rk[top[x]],rk[x]);
		if(fl)
			ans2=merge(tmp,ans2);
		else
			ans1=tree(ans1.lc,tmp.lc,ans1.v+tmp.v+(ans1.rc==tmp.rc));
		x=fa[top[x]];
	}
	if(dep[x]<dep[y])
		x^=y^=x^=y,fl^=1;
	tmp=query(1,1,n,rk[y],rk[x]);
	if(fl)
		ans2=merge(tmp,ans2);
	else
		ans1=tree(ans1.lc,tmp.lc,ans1.v+tmp.v+(ans1.rc==tmp.rc));
	return merge(ans1,ans2).v;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>T;
	while(T--){
		init();
		cin>>n>>m;
		for(int i=1,x,y;i<n;i++){
			cin>>x>>y;
			add(x,y),add(y,x);
		}
		dfs1(1);
		dfs2(1,1);
		build(1,1,n);
		for(int i=1;i<=n;i++){
			update(1,1,n,rk[i],rk[i],++col);
		}
		while(m--){
			int op,x,y;
			cin>>op>>x>>y;
			if(op==1)
				update_way(x,y,++col); 
			else
				cout<<query_way(x,y)<<'\n';
/*			
			for(int i=1;i<=n;i++){
				cout<<query(1,1,n,rk[i],rk[i]).lc<<' ';
			}
			cout<<'\n';
*/
		}
	}
	return 0; 
}
void init(){
	ic=tt=0;
	col=0;
	memset(e,0,sizeof(e));
	memset(hd,-1,sizeof(hd));
	memset(fa,0,sizeof(fa));
	memset(son,0,sizeof(son));
	memset(siz,0,sizeof(siz));
}
2024/8/27 07:52
加载中...