"BZOJ1095" [Zjoi2007]hide Hide-and-Seek "dynamic tree Partition" _ Divide and conquer

Source: Internet
Author: User

"Topic link"

Learn to delete a heap.


Find the center of gravity where forget to write!del[g[i].v],re countless hair.

/* Pigonometry/#include <cstdio> #include <cstring> #include <algorithm> #include <queue> usin

G namespace Std;

const int MAXN = 100005, MAXM = MAXN << 1, maxk =, inf = 0x3f3f3f3f;

int n, HEAD[MAXN], CNT;
int root, CLO, tot, bin[maxk], LG[MAXM];

int SIZE[MAXN], DEPTH[MAXN], SM[MAXN], PRE[MAXN], ST[MAXK][MAXM], DFN[MAXN], DEL[MAXN], COL[MAXN];

	struct _heap {priority_queue<int> A, B; void Update () {for (; B.size () && a.top () = = B.top ();
	A.pop (), B.pop ());
	void push (int x) {a.push (x);
		} void Pop () {update ();
	A.pop ();
	} void Erase (int x) {b.push (x);
	int size () {return a.size ()-b.size ();
		INT-I () {update (); Return A.size ()?
	A.top (): 0;
		int second () {if (Size () < 2) return 0; int x = i ();
		Pop (); int y = i ();
		push (x);
	return y;

} A, B[MAXN], C[MAXN];

struct _edge {int V, next;} G[MAXN << 1]; inline int iread () {int f = 1, x = 0; char ch = getchar (); for (; Ch < ' 0 ' | | | ch > ' 9 '; ch = getchar ()) F = ch = = '-'?
	-1:1;
	for (; Ch >= ' 0 ' && ch <= ' 9 '; ch = getchar ()) x = x * + ch-' 0 ';
return f * x;
	} inline void Add (int u, int v) {g[cnt] = (_edge) {V, head[u]};
Head[u] = cnt++;
	} inline void Dfs (int x, int f) {St[0][++clo] = depth[x];
	DFN[X] = CLO;
		for (int i = head[x]; ~i i = g[i].next) if (g[i].v ^ f) {depth[g[i].v] = depth[x] + 1;
		DFS (G[I].V, x);
	St[0][++clo] = depth[x];
	} inline int Getlca (int u, int v) {u = dfn[u]; v = dfn[v];
	if (U > v) swap (U, v);
	int t = lg[v-u + 1];
return min (St[t][u], st[t][v-bin[t] + 1]);

inline int dis (int u, int v) {return depth[u] + depth[v]-(GETLCA (U, v) << 1);}
	inline void Find (int x, int f) {size[x] = 1; sm[x] = 0;
		for (int i = head[x]; ~i i = g[i].next) if (g[i].v ^ f &&!del[g[i].v]) {Find (G[I].V, x);
		SIZE[X] + = SIZE[G[I].V];	
	SM[X] = max (sm[x], size[g[i].v]);	
	} Sm[x] = max (sm[x], tot-size[x]); if (Sm[x]< Sm[root]) root = x;
	} inline void Divide (int x, int f) {pre[x] = f; del[x] = 1;
		for (int i = head[x]; ~i i = g[i].next) if (g[i].v ^ f &&!del[g[i].v]) {root = 0; tot = size[g[i].v]; Find (G[I].V, x);
	Divide (root, x);
		} inline void turnoff (int x, int f) {if (x = = f) {b[x].push (0);
	if (b[x].size () = = 2) A.push (B[x].first ());

	} if (!pre[f]) return;
	int p = pre[f], d = dis (x, p), tmp = C[f].first ();

	C[f].push (d);
		if (d > tmp) {int mx = B[p].first () + B[p].second (), Sz = B[p].size ();
		B[p].push (d);
		if (TMP) b[p].erase (TMP);
		int now = B[p].first () + B[p].second ();
			if (now > mx) {if (SZ >= 2) a.erase (MX);
		if (B[p].size () >= 2) A.push (now);
} turnoff (x, p);
		} inline void Turnon (int x, int f) {if (x = = f) {if (b[x].size () = = 2) a.erase (B[x].first ());
	B[x].erase (0);

	} if (!pre[f]) return;
	int p = pre[f], d = dis (x, p), tmp = C[f].first ();

	C[f].erase (d); if (d = = tmp) {int mx = B[p].first () + B[p].second (), Sz = B[p].Size ();
		B[p].erase (d);
		if (C[f].first ()) B[p].push (C[f].first ());
		int now = B[p].first () + B[p].second ();
			if (now < MX) {if (SZ >= 2) a.erase (MX);
		if (B[p].size () >= 2) A.push (now);
} turnon (x, p);
	int main () {n = iread (); for (int i = 0; I <= N; i++) Head[i] =-1;

	CNT = 0;
	Bin[0] = 1;
	for (int i = 1; i < maxk i++) bin[i] = bin[i-1] << 1;
	Lg[0] =-1;


	for (int i = 1; i < MAXM i++) lg[i] = lg[i >> 1] + 1;
		for (int i = 1; i < n; i++) {int u = iread (), V = iread (); Add (U, v);
	Add (V, u);
	DFS (1, 0); for (int j = 1; j < Maxk; J +) for (int i = 1; I <= clo i++) if (i + bin[j]-1 <= clo) st[j][i] = min (st[j-1][

	I], st[j-1][i + bin[j-1]); Sm[0] = inf; root = 0;
	tot = n; Find (1, 0);

	Divide (root, 0);
	for (int i = 1; I <= n; i++) C[i].push (0), col[i] = 1;

	for (int i = 1; I <= n; i++) turnoff (i, I);
	int node = n; for (int T = Iread (); T t--) {Char ch = getchar (); for (; Ch!= ' C ' && ch!)= ' G ';
		ch = getchar ());
			if (ch = = ' G ') {if (node <= 1) printf ("%d\n", node-1);
		else printf ("%d\n", A.first ());
			else if (ch = = ' C ') {int u = iread ();
			if (col[u] = = 1) node--, turnon (U, u);
			else node++, turnoff (U, u);
		Col[u] ^= 1;
} return 0;
 }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.