1. 程式人生 > >【CodeChef】Adi and the Tree

【CodeChef】Adi and the Tree

【題目連結】

【思路要點】

  • 首先,一條邊不會在一種方案中被計算 x   ( x > 1
    ) x\ (x>1)
    次,否則我們可以構造出一種只計算這條邊 x % 2 x\%2
    次的方案,方案會變優。
  • 考慮一條邊何時會被計算,顯然當其兩側的點數均為奇數時,這條邊會被計算。
  • 由於題目保證了任意時刻總點數為偶數,我們只需要計運算元樹和為奇數的點數即可。
  • 樹鏈剖分 + 線段樹維護之,時間複雜度 O ( N +
    M L o g 2 N ) O(N+MLog^2N)

【程式碼】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 5;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
struct SegmentTree {
	struct Node {
		int lc, rc;
		int sum[2];
		bool tag;
	} a[MAXN * 2];
	int n, size, root;
	void update(int root) {
		a[root].sum[0] = a[root].sum[1] = 0;
		int tmp = a[root].lc;
		a[root].sum[0] += a[tmp].sum[0];
		a[root].sum[1] += a[tmp].sum[1];
		tmp = a[root].rc;
		a[root].sum[0] += a[tmp].sum[0];
		a[root].sum[1] += a[tmp].sum[1];
	}
	void build(int &root, int l, int r) {
		root = ++size;
		if (l == r) {
			a[root].sum[0] = 1;
			return;
		}
		int mid = (l + r) / 2;
		build(a[root].lc, l, mid);
		build(a[root].rc, mid + 1, r);
		update(root);
	}
	void init(int x) {
		n = x;
		root = size = 0;
		build(root, 1, n);
	}
	int query() {
		return a[root].sum[1];
	}
	void pushdown(int root) {
		if (a[root].tag) {
			int tmp = a[root].lc;
			a[tmp].tag ^= true;
			swap(a[tmp].sum[0], a[tmp].sum[1]);
			tmp = a[root].rc;
			a[tmp].tag ^= true;
			swap(a[tmp].sum[0], a[tmp].sum[1]);
			a[root].tag = false;
		}
	}
	void modify(int root, int l, int r, int ql, int qr) {
		if (l == ql && r == qr) {
			a[root].tag ^= true;
			swap(a[root].sum[0], a[root].sum[1]);
			return;
		}
		pushdown(root);
		int mid = (l + r) / 2;
		if (mid >= ql) modify(a[root].lc, l, mid, ql, min(qr, mid));
		if (mid + 1 <= qr) modify(a[root].rc, mid + 1, r, max(mid + 1, ql), qr);
		update(root);
	}
	void modify(int l, int r) {
		modify(root, 1, n, l, r);
	}
} ST;
int n, m, timer, dfn[MAXN], rit[MAXN], depth[MAXN];
int size[MAXN], son[MAXN], up[MAXN], father[MAXN];
vector <int> a[MAXN];
void modify(int pos) {
	while (pos != 0) {
		ST.modify(dfn[up[pos]], dfn[pos]);
		pos = father[up[pos]];
	}
}
void dfs(int pos, int fa) {
	size[pos] = 1, son[pos] = 0;
	depth[pos] = depth[fa] + 1;
	for (auto x : a[pos]) 
		if (x != fa) {
			dfs(x, pos);
			size[pos] += size[x];
			if (size[x] > size[son[pos]]) son[pos] = x;
		}
}
void efs(int pos, int fa, int from) {
	up[pos] = from;
	father[pos] = fa;
	dfn[pos] = ++timer;
	if (son[pos]) efs(son[pos], pos, from);
	for (auto x : a[pos])
		if (x != son[pos] && x != fa) efs(x, pos, x);
	rit[pos] = timer;
}
int main() {
	read(n);
	for (int i = 1; i <= n - 1; i++) {
		int x, y; read(x), read(y);
		a[x].push_back(y);
		a[y].push_back(x);
	}
	dfs(1, 0);
	efs(1, 0, 1);
	ST.init(n);
	read(m);
	for (int i = 1; i <= m; i++) {
		int x, y; read(x), read(y);
		modify(x), modify(y);
		writeln(ST.query());
	}
	return 0;
}