Topic links
Island Niang out of the problem, or relatively easy
#include <iostream> #include <fstream> #include <string> #include <time.h> #include <vector > #include <map> #include <queue> #include <algorithm> #include <stack> #include <cstring > #include <cmath> #include <set> #include <vector>using namespace std;template <class t> inline BOOL Rd (T &ret) {char c; int sgn;if (c = GetChar (), c = = EOF) return 0;while (c! = '-' && (c< ' 0 ' | | C> ' 9 ')) C = GetChar (); sgn = (c = = '-')? -1:1;ret = (c = = '-')? 0: (C-' 0 '); while (c = GetChar (), C >= ' 0 ' &&c <= ' 9 ') ret = ret * + (C-' 0 '); ret *= Sgn;return 1;} Template <class t>inline void pt (T x) {if (x <0) {Putchar ('-'); x = x;} if (x>9) pt (X/10);p Utchar (x% 10 + ' 0 ');} typedef long Long Ll;typedef pair<int, int> pii;const int N = 100005;const int inf = 10000000;struct Node *null;stru CT Node {node *fa, *ch[2];int id;int s[2];//s[0] This virtual edge is the sum of consecutive white points of all subtrees (not chains) int col;int ls[2], rs[2], Siz;//ls[0] is the top-down continuous white point int ls[2] for this chain, Rs[2];//ls[0] is the contiguous white point of the subtrees tree connected to the topmost point bool rev;inline void Clear (int _col, int _id ) {FA = ch[0] = ch[1] = Null;siz = 1;rev = 0;id = _id;col = _col;for (int i = 0; i < 2; i++) {ls[i] = rs[i] = S[i] = 0; }}inline void Push_up () {if (this = = null) Return;siz = ch[0]->siz + ch[1]->siz + 1;for (int i = 0; i < 2; i++) {L S[i] = Ch[0]->ls[i], rs[i] = ch[1]->rs[i]; Ls[i] = Ch[0]->ls[i], rs[i] = ch[1]->rs[i];if (ch[0]->ls[i] = = Ch[0]->siz && i = = col) {Ls[i] = ch[0]- >siz + 1 + ch[1]->ls[i]; ls[i]++; Ls[i] + = s[i]; Ls[i] + = Ch[1]->ls[i];} if (ch[1]->rs[i] = = Ch[1]->siz && i = = col) {Rs[i] = ch[1]->siz + 1 + ch[0]->rs[i]; rs[i]++; Rs[i] + = s[i]; Rs[i] + = Ch[0]->rs[i];}} inline void Push_down () {if (rev.) {ch[0]->flip (); Ch[1]->flip (); rev = 0;}} inline void SetC (Node *p, int d) {Ch[d] = P;p->fa = this;} inline bool D () {return fa->ch[1] = = this;} inline bool IsRoot () {return FA = = NULL | | FA->CH[0]! = this && fa->ch[1]! = this; inline void Flip () {if (this = null) Return;swap (ch[0], ch[1]); rev ^= 1;} The inline void Go () {//is updated from the chain header to Thisif (!isroot ()) Fa->go ();p ush_down ();} inline void rot () {Node *f = fa, *ff = fa->fa;int C = d (), CC = Fa->d (), F->setc (Ch[!c], C), This->setc (f,!c); F (ff->ch[cc] = = f) ff->setc (this, cc); else This->fa = Ff;f->push_up ();} Inline Node*splay () {go (); while (!isroot ()) {if (!fa->isroot ()) d () = = Fa->d ()? Fa->rot (): Rot (); Rot ();} Push_up (); return this;} Inline node* access () {//access After this is a splay to the root, and this is already the root of the splay for (Node *p = this, *q = NULL; P! = null; q = p, p = P->FA) {p->splay (); if (p->ch[1]! = null) for (int i = 0;i < 2;i++) P->s[i] + = p->ch[1]->ls[i];if (q! = NULL) for (int i = 0; i < 2; i++) p->s[i] = Q->ls[i];p->setc (q, 1);p->push_up ();} return splay ();} Inline node* find_root () {Node *x;for (x = Access (); X->push_down (), x->ch[0]! = null; x = x->ch[0]);return x;} void Make_root () {access ()->flip ();} void Cut () {//Leave the sub-tree of this point out of access (); Ch[0]->fa = null;ch[0] = Null;push_up ();} void Cut (Node *x) {if (this = = X | | find_root ()! = X->find_root ()) Return;else {x->make_root (); Cut ();}} void link (Node *x) {if (find_root () = = X->find_root ()) Return;else {make_root (); fa = x;}}}; Node Pool[n], *tail; Node *node[n];int N, q;struct Edge {int to, NEX;} Edge[n << 1];int Head[n], edgenum;void Add (int u, int v) {Edge E = {V, head[u]};edge[edgenum] = e;head[u] = Edgenu m++;} void Dfs (int u, int fa) {for (int i = head[u]; ~i; i = edge[i].nex) {int v = edge[i].to; if (v = = FA) continue;node[v]-> FA = Node[u];d fs (V, U), for (int j = 0; J < 2; j + +) Node[u]->s[j] + = Node[v]->ls[j];} Node[u]->push_up ();} int main () {while (CIN >> N) {memset (head,-1, sizeof head); edgenum = 0;for (int i = 1, u, v; i < n; i++) {rd (U) ; RD (v); Add (U, v); Add (v, u);} tail = pool; NULL = Tail++;null->clear (-1, 0); Null->siz = 0;for (int i = 1; I <= N; i++) {Node[i] = tail++;node[i]->clear (1, i);} DFS (1, 1); Rd (Q); int u, v;while (q--) {rd (U), RD (v), if (!u) {node[v]->access ();p t (Max (node[v]->rs[0], node[v]->rs[1]), puts ("" );} else {node[v]->access (); Node[v]->col ^= 1;node[v]->push_up ();}}} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Spoj QTREE6 LCT Nude questions