loj2001「SDOI2017」樹點塗色

這道題是真的妙,感覺做完這一題,我對LCT的理解又加深了一點。
題解:

操作一

我們發現這一個操作就是把樹中某個節點到根節點的路徑上的所有節點變成一樣的顏色,又因爲這棵樹上有一個性質,同樣顏色的點連接起來一定會是一條鏈,就可以想到LCT的access函數。所以我們將同種顏色的點看成LCT中同一棵splay上的點。

操作二

因爲LCT已經維護了顏色了,所以我們不能再用LCT來維護路徑之間的權值了。於是就可以想到樹上差分,設dis[x]dis[x]爲點x的權值,這個值就等於在LCT上該點到根節點所經過虛邊的條數+1,x到y路徑的權值就是dis[x]+dis[y]2dis[lca]+1dis[x]+dis[y]-2*dis[lca]+1,這條公式不多解釋,畫一個圖試試就出來了。

然後,我們就要考慮如何維護disdis數組了。在access中,設當前節點爲xx,它的父親爲fafa(不在同一棵splay中),因爲我們將xxfafa中原來的虛邊變成了實邊,所以x爲根節點的子樹中所有節點的disdis-1。設sonsonfafa原來的兒子,那麼就會因爲sonsonfafa中的實邊變成虛邊使得以sonson爲根節點的子樹中所以節點的disdis-1。於是又現這些要+1或-1的節點都在同一棵子樹裏面,就很容易想到樹的dfs序,我們按照這些點的dfs序建一顆線段樹,每次就只需要修改連續的編號即可。

操作三

最複雜的操作二都搞定了,操作三還難嗎,直接在線段樹中求最大值不就行了唄。

參考代碼

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
inline void read(int &x) {
	x = 0; int f = 0; char s = getchar();
	while (!isdigit(s)) f |= s=='-', s = getchar();
	while ( isdigit(s)) x = x * 10 + s - 48, s = getchar();
	x = f ? -x : x;
}

int ss = 0, buf[31];
inline void write(int x) {
	do {buf[++ss] = x % 10, x /= 10;} while(x);
	while (ss) putchar(buf[ss--]+'0'); puts("");
}

const int N = 1e5 + 6;
int L[N], R[N], rev[N], n, m, tot;//L,R,rev,tot用來維護樹的dfs序 
int fath[N][21], dep[N];//dep也可以看成最開始的dis
int cnt, ver[N<<1], Head[N], Next[N<<1];
namespace Tree {//線段樹 
	#define lc p << 1
	#define rc p << 1 | 1
	struct T {
		int l, r, mark, mx;
	} tr[N<<2];
	void pushup(int p) {
		if (tr[p].mark) {
			tr[lc].mark += tr[p].mark, tr[lc].mx += tr[p].mark;
			tr[rc].mark += tr[p].mark, tr[rc].mx += tr[p].mark;
			tr[p].mark = 0;
		}
	}
	void build(int p, int l, int r) {
		tr[p].l = l, tr[p].r = r;
		if (l == r) tr[p].mx = dep[rev[l]];
		else {
			int mid = (l + r)>>1;
			build(lc, l, mid), build(rc, mid + 1, r);
			tr[p].mx = max(tr[lc].mx, tr[rc].mx);
		}
	}
	int query(int p, int l, int r) {
		if (r < tr[p].l || tr[p].r < l) 
			return 0;
		if (l <= tr[p].l && tr[p].r <= r) 
			return tr[p].mx;
		pushup(p);
		int mid = (tr[p].l + tr[p].r)>>1;
		return max(query(lc, l, r), query(rc, l, r));
	}
	void update(int p, int l, int r, int add) {
		if (r < tr[p].l || tr[p].r < l) return;
		if (l <= tr[p].l && tr[p].r <= r) {
			tr[p].mark += add, tr[p].mx += add;
			return;
		}
		int mid = (tr[p].l + tr[p].r)>>1;
		pushup(p);
		update(lc, l, r, add), update(rc, l, r, add);
		tr[p].mx = max(tr[lc].mx, tr[rc].mx);
	}
}
using namespace Tree;

namespace Link_Cut_Tree {//LCT
	#define ls(x) t[x].son[0]
	#define rs(x) t[x].son[1]
	struct LCT {
		int son[2], fa, val; bool mark;
	} t[N<<1];
	int isroot(int x) {
		return (ls(t[x].fa) != x) && (rs(t[x].fa) != x);
	}
	void pushmark(int x) {
		if (t[x].mark) {
			swap(ls(x), rs(x));
			t[x].mark = 0;
			t[ls(x)].mark ^= 1, t[rs(x)].mark ^= 1;
		}
	}
	void rotate(int x) {
		int f = t[x].fa, ff = t[f].fa, qwq = (rs(t[x].fa) == x); 
		t[x].fa = ff;
		if (!isroot(f)) t[ff].son[rs(ff)==f] = x;
		t[f].son[qwq] = t[x].son[qwq^1];
		if (t[x].son[qwq^1]) t[t[x].son[qwq^1]].fa = f;
		t[x].son[qwq^1] = f, t[f].fa = x;
	}
	int st[N];
	void splay(int x) {
		int top = 0, now = x; st[++top] = now;
		while (!isroot(now)) st[++top] = (now = t[now].fa);
		while (top) pushmark(st[top--]);
		while (!isroot(x)) {
			int f = t[x].fa, ff = t[f].fa;
			if (!isroot(f))
				((rs(f) == x) ^ (rs(ff) == f)) ? rotate(x) : rotate(f);
			rotate(x);
		}
	}
	int findrt(int x) {//這裏要特別注意一下,splay最左邊的點(編號最小)纔是真正的根 
		while (ls(x)) x = ls(x);
		return x;
	}
	void access(int x) {//這裏的access和普通的access稍微有一點差別 
		int son;
		for (int y = 0; x; y = x, x = t[y].fa) {
			splay(x);
			if (rs(x)) son = findrt(rs(x)), update(1, L[son], R[son], 1);
			if (rs(x) = y) son = findrt(y), update(1, L[son], R[son], -1);
		}
	}
}
using namespace Link_Cut_Tree;

void add(int x, int y) {
	cnt++;
	ver[cnt] = y;
	Next[cnt] = Head[x];
	Head[x] = cnt;
}

void dfs(int x, int fa) {
	L[x] = ++tot, rev[tot] = x;
	t[x].fa = fath[x][0] = fa, dep[x] = dep[fa] + 1;
	for (int i = 1; i <= 19; i++)
		fath[x][i] = fath[fath[x][i-1]][i-1];
	for (int i = Head[x]; i; i = Next[i]) {
		int y = ver[i];
		if (y == fa) continue;
		dfs(y, x);
	}
	R[x] = tot;
}

int LCA(int x, int y) {
	if (dep[x] < dep[y]) swap(x, y);
	for (int i = 19; i >= 0; i--)
		if (dep[fath[x][i]] >= dep[y])
			x = fath[x][i];
	if (x == y) return x;
	for (int i = 19; i >= 0; i--)
		if (fath[x][i] != fath[y][i])
			x = fath[x][i], y = fath[y][i];
	return fath[x][0];
}

int main() {
	read(n), read(m);
	int opt, x, y, ans;
	for (int i = 1; i < n; i++) {
		read(x), read(y);
		add(x, y), add(y, x);
	}
	dfs(1, 0); t[1].fa = 0;
	build(1, 1, n);
	for (int i = 1; i <= m; i++) {
		read(opt), read(x);
		if (opt == 1) access(x);
		else if (opt == 2) {
			read(y);
			int lca = LCA(x, y);
			int ans1 = query(1, L[x], L[x]);
			int ans2 = query(1, L[y], L[y]);
			int ans3 = query(1, L[lca], L[lca]);
			write(ans1 + ans2 - 2 * ans3 + 1);
		}
		else {
			ans = query(1, L[x], R[x]);
			write(ans);
		}
	}
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章