题解 树上的棋局

传送门

发现自己不会算 SG 函数

对于本题,一个点的 SG 值是子树中每个点 SG 值的 mex
归纳可证每个点的 SG 值为向子树内的最长链长度
因为有换根的操作,处理 \(f, g\) 为最长,次长链长度
但这样还是不好统计答案

  • 树上最长链相关问题一个可能的突破口:
    贡献 \(dis_{u, v}\) 的最大值的那个点,一定是直径的某个端点 ,并且从 \(u\) 走到 \(v\) 的路径,必定包含直径的中点

所以将直径的中点提根
所有操作可以树剖实现
特判一下中点有两个的情况就可以了
复杂度 \(O(n\log^2 n)\)

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 200010
#define fir first
#define sec second
#define pb push_back
#define ll long long
//#define int long long

char buf[1<<21], *p1=buf, *p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf, 1, 1<<21, stdin)), p1==p2?EOF:*p1++)
inline int read() {
	int ans=0, f=1; char c=getchar();
	while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
	while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
	return ans*f;
}

int n, m;
bool isrt[N];
pair<int, int> fir[N], sec[N];
int head[N], fa[21][N], dep[N], mdep[N], siz[N], msiz[N], mson[N], lg[N], top[N], rk[N], id[N], ecnt, rot=1, ctr, tot;
struct edge{int to, next;}e[N<<1];
inline void add(int s, int t) {e[++ecnt]={t, head[s]}; head[s]=ecnt;}
#define tl(p) tl[p]
#define tr(p) tr[p]
#define valf1(p) valf1[p]
#define valf2(p) valf2[p]
#define valg1(p) valg1[p]
#define valg2(p) valg2[p]
#define tag(p) tag[p]
int tl[N<<2], tr[N<<2], valf1[N<<2], valf2[N<<2], valg1[N<<2], valg2[N<<2], tag[N<<2];
void pushup(int p) {
	valf1(p)=valf1(p<<1)^valf1(p<<1|1);
	valf2(p)=valf2(p<<1)^valf2(p<<1|1);
	valg1(p)=valg1(p<<1)^valg1(p<<1|1);
	valg2(p)=valg2(p<<1)^valg2(p<<1|1);
}
void spread(int p) {
	if (!tag(p)) return ;
	swap(valf1(p<<1), valf2(p<<1)); swap(valg1(p<<1), valg2(p<<1)); tag(p<<1)^=1;
	swap(valf1(p<<1|1), valf2(p<<1|1)); swap(valg1(p<<1|1), valg2(p<<1|1)); tag(p<<1|1)^=1;
	tag(p)=0;
}
void build(int p, int l, int r) {
	tl(p)=l; tr(p)=r;
	if (l==r) {valg1(p)=sec[rk[l]].fir; valf1(p)=fir[rk[l]].fir; return ;}
	int mid=(l+r)>>1;
	build(p<<1, l, mid);
	build(p<<1|1, mid+1, r);
	pushup(p);
}
void upd(int p, int l, int r) {
	if (l<=tl(p)&&r>=tr(p)) {swap(valf1(p), valf2(p)); swap(valg1(p), valg2(p)); tag(p)^=1; return ;}
	spread(p);
	int mid=(tl(p)+tr(p))>>1;
	if (l<=mid) upd(p<<1, l, r);
	if (r>mid) upd(p<<1|1, l, r);
	pushup(p);
}
int queryf(int p, int l, int r) {
	if (l<=tl(p)&&r>=tr(p)) return valf1(p);
	spread(p);
	int mid=(tl(p)+tr(p))>>1, ans=0;
	if (l<=mid) ans=ans^queryf(p<<1, l, r);
	if (r>mid) ans=ans^queryf(p<<1|1, l, r);
	return ans;
}
int queryg(int p, int l, int r) {
	if (l<=tl(p)&&r>=tr(p)) return valg1(p);
	spread(p);
	int mid=(tl(p)+tr(p))>>1, ans=0;
	if (l<=mid) ans=ans^queryg(p<<1, l, r);
	if (r>mid) ans=ans^queryg(p<<1|1, l, r);
	return ans;
}

void dfs1(int u, int pa) {
	mdep[u]=dep[u];
	for (int i=head[u],v; ~i; i=e[i].next) {
		v = e[i].to;
		if (v==pa) continue;
		dep[v]=dep[u]+1;
		dfs1(v, u);
		int dis=mdep[v]-dep[u];
		if (dis>fir[u].fir) sec[u]=fir[u], fir[u]={dis, v};
		else if (dis>sec[u].fir) sec[u]={dis, v};
		mdep[u]=max(mdep[u], mdep[v]);
	}
}

void dfs2(int u, int fa, pair<int, int> down) {
	if (down.fir>fir[u].fir) sec[u]=fir[u], fir[u]=down;
	else if (down.fir>sec[u].fir) sec[u]=down;
	for (int i=head[u],v; ~i; i=e[i].next) {
		v = e[i].to;
		if (v==fa) continue;
		pair<int, int> t=(v==fir[u].sec)?sec[u]:fir[u];
		if (down.fir>t.fir) t=down;
		++t.fir;
		dfs2(v, u, t);
	}
}

int lca(int a, int b) {
	if (dep[a]<dep[b]) swap(a, b);
	while (dep[a]>dep[b]) a=fa[lg[dep[a]-dep[b]]-1][a];
	if (a==b) return a;
	for (int i=lg[dep[a]]-1; ~i; --i)
		if (fa[i][a]!=fa[i][b])
			a=fa[i][a], b=fa[i][b];
	return fa[0][a];
}

void dfs3(int u, int pa) {
	siz[u]=1;
	for (int i=1; i<21; ++i)
		if (dep[u]>=1<<i) fa[i][u]=fa[i-1][fa[i-1][u]];
		else break;
	for (int i=head[u],v; ~i; i=e[i].next) {
		v = e[i].to;
		if (v==pa) continue;
		dep[v]=dep[u]+1;
		fa[0][v]=u;
		dfs3(v, u);
		siz[u]+=siz[v];
		if (siz[v]>msiz[u]) msiz[u]=siz[v], mson[u]=v;
	}
}

void dfs4(int u, int fa, int t) {
	top[u]=t;
	rk[id[u]=++tot]=u;
	if (!mson[u]) return ;
	dfs4(mson[u], u, t);
	for (int i=head[u],v; ~i; i=e[i].next) {
		v = e[i].to;
		if (v==fa||v==mson[u]) continue;
		dfs4(v, u, v);
	}
}

void upd(int x, int y) {
	while (top[x]!=top[y]) {
		if (dep[top[x]]<dep[top[y]]) swap(x, y);
		upd(1, id[top[x]], id[x]); //, cout<<top[x]<<' '<<x<<endl;
		x=fa[0][top[x]];
	}
	if (dep[x]>dep[y]) swap(x, y);
	upd(1, id[x], id[y]); //, cout<<x<<' '<<y<<endl;
}

int anc(int u) {for (int i=lg[dep[u]]-1; ~i; --i) if (fa[i][u]&&fa[i][u]!=ctr) u=fa[i][u]; return u;}
int anc(int u, int k) {for (int i=20; ~i; --i) if (k&(1<<i)) u=fa[i][u]; return u;}

int queryf(int x, int y) {
	int ans=0;
	while (top[x]!=top[y]) {
		if (dep[x]<dep[y]) swap(x, y);
		ans=ans^queryf(1, id[top[x]], id[x]);
		x=fa[0][top[x]];
	}
	if (dep[x]>dep[y]) swap(x, y);
	ans=ans^queryf(1, id[x], id[y]);
	return ans;
}

int queryg(int x, int y) {
	int ans=0;
	while (top[x]!=top[y]) {
		if (dep[x]<dep[y]) swap(x, y);
		ans=ans^queryg(1, id[top[x]], id[x]);
		x=fa[0][top[x]];
	}
	if (dep[x]>dep[y]) swap(x, y);
	ans=ans^queryg(1, id[x], id[y]);
	return ans;
}

signed main()
{
	freopen("tree.in", "r", stdin);
	freopen("tree.out", "w", stdout);

	n=read(); m=read();
	memset(head, -1, sizeof(head));
	for (int i=1,u,v; i<n; ++i) {
		u=read(); v=read();
		add(u, v); add(v, u);
	}
	for (int i=1; i<=n; ++i) lg[i]=lg[i-1]+(1<<lg[i-1]==i);
	dep[1]=1; dfs1(1, 0); dfs2(1, 0, {0, 0});
	//cout<<"fir: "; for (int i=1; i<=n; ++i) cout<<fir[i].fir<<' '; cout<<endl;
	//cout<<"sec: "; for (int i=1; i<=n; ++i) cout<<sec[i].fir<<' '; cout<<endl;
	int len=0;
	for (int i=1; i<=n; ++i) len=max(len, fir[i].fir+sec[i].fir);
	//cout<<"len: "<<len<<endl;
	for (int i=1; i<=n; ++i) if (fir[i].fir<=(len+1)/2) isrt[ctr=i]=1;
	//cout<<"ctr: "<<ctr<<endl;
	dep[ctr]=1; dfs3(ctr, 0); dfs4(ctr, 0, ctr); build(1, 1, n);
	//cout<<queryg(1, 1, n)<<endl;
	//cout<<"id: "; for (int i=1; i<=n; ++i) cout<<id[i]<<' '; cout<<endl;
	for (int i=1,u,v,x; i<=m; ++i) {
		//cout<<"i: "<<i<<endl;
		if (read()&1) {
			u=read(); v=read(); x=read();
			upd(u, v);
		}
		else {
			u=read(); x=read();
			int t=lca(rot, u);
			if (t!=u) upd(1, id[u], id[u]+siz[u]-1);
			else {
				upd(1, 1, n);
				if (rot!=u) {
					t=anc(rot, dep[rot]-dep[u]-1);
					upd(1, id[t], id[t]+siz[t]-1);
				}
			}
		}
		rot=x;
		//cout<<"all_val: "<<queryg(1, 1, n)<<endl;
		if (isrt[rot]) printf("%d\n", queryg(1, 1, n)^queryg(rot, rot)^queryf(rot, rot));
		else {
			int t=anc(rot);
			//cout<<"t: "<<t<<endl;
			if (isrt[t]) printf("%d\n", queryg(1, 1, n)^queryg(rot, t)^queryf(rot, t)); //, cout<<queryg(rot, t)<<' '<<queryf(rot, t)<<endl;
			else printf("%d\n", queryg(1, 1, n)^queryg(rot, ctr)^queryf(rot, ctr));
		}
	}
	
	return 0;
}
posted @ 2022-02-15 10:33  Administrator-09  阅读(4)  评论(1编辑  收藏  举报