都是动态树的基本操作。。。
#include <iostream> #include <queue> #include <stack> #include <map> #include <set> #include <bitset> #include <cstdio> #include <algorithm> #include <cstring> #include <climits> #include <cstdlib> #include <cmath> #include <time.h> #define maxn 300005 #define maxm 600005 #define eps 1e-10 #define mod 1000000007 #define INF 0x3f3f3f3f #define PI (acos(-1.0)) #define lowbit(x) (x&(-x)) #define mp make_pair #define ls o<<1 #define rs o<<1 | 1 #define lson o<<1, L, mid #define rson o<<1 | 1, mid+1, R #define pii pair<int, int> //#pragma comment(linker, "/STACK:16777216") typedef long long LL; typedef unsigned long long ULL; //typedef int LL; using namespace std; LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;} LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;} // head struct node *null; struct node { int key, rise, Max, rev; node *fa, *ch[2]; inline bool d() { return fa->ch[1] == this; } inline bool isroot() { return fa == null || fa->ch[0] != this && fa->ch[1] != this; } inline void setc(node *p, int d) { ch[d] = p; p->fa = this; } inline void flip() { if(this == null) return; swap(ch[0], ch[1]); rev ^= 1; } inline void add(int x) { if(this == null) return; key += x; Max += x; rise += x; } void pushup() { Max = max(key, max(ch[0]->Max, ch[1]->Max)); } void pushdown() { if(rev) { ch[0]->flip(); ch[1]->flip(); rev = 0; } if(rise) { ch[0]->add(rise); ch[1]->add(rise); rise = 0; } } void rot() { node *f = fa, *ff = fa->fa; int c = d(), cc = fa->d(); f->setc(ch[!c], c); this->setc(f, !c); if(ff->ch[cc] == f) ff->setc(this, cc); else this->fa = ff; f->pushup(); } inline void go() { if(!isroot()) fa->go(); pushdown(); } node* splay() { go(); while(!isroot()) { if(!fa->isroot()) d() == fa->d() ? fa->rot() : rot(); rot(); } pushup(); return this; } node* access() { for(node *p = this, *q = null; p != null; q = p, p = p->fa) { p->splay()->setc(q, 1); p->pushup(); } return splay(); } node* findroot() { node *p; for(p = access(); p->ch[0] != null; p = p->ch[0]); return p; } void makeroot() { access()->flip(); } void link(node *p) { if(findroot() == p->findroot()) printf("-1\n"); else { makeroot(); fa = p; } } void cut() { access(); ch[0]->fa = null; ch[0] = null; pushup(); } void cut(node *p) { if(p == this || findroot() != p->findroot()) printf("-1\n"); else { p->makeroot(); cut(); } } }; struct Edge { int v; Edge *next; }E[maxm], *H[maxn], *edges; node *Node[maxn]; node pool[maxm]; queue<int> q; node *tail; int vis[maxn]; int n, m; void addedges(int u, int v) { edges->v = v; edges->next = H[u]; H[u] = edges++; } void init() { tail = pool; null = tail++; null->fa = null->ch[0] = null->ch[1] = null; null->key = null->rev = null->rise = null->Max = 0; edges = E; memset(H, 0, sizeof H); memset(vis, 0, sizeof vis); } void read() { int u, v, x; for(int i = 1; i < n; i++) { scanf("%d%d", &u, &v); addedges(u, v); addedges(v, u); } for(int i = 1; i <= n; i++) { scanf("%d", &x); tail->key = tail->Max = x; tail->rev = tail->rise = 0; tail->fa = tail->ch[0] = tail->ch[1] = null; Node[i] = tail++; } scanf("%d", &m); } void bfs() { vis[1] = 1; q.push(1); while(!q.empty()) { int u = q.front(); q.pop(); for(Edge *e = H[u]; e; e = e->next) { int v = e->v; if(vis[v]) continue; vis[v] = 1; q.push(v); Node[v]->fa = Node[u]; } } } int query(node *a, node *b) { a->access(); for(a = null; b != null; a = b, b = b->fa) { b->splay(); if(b->fa == null) return max(b->key, max(b->ch[1]->Max, a->Max)); b->setc(a, 1); b->pushup(); } } void update(node *a, node *b, int c) { a->access(); for(a = null; b != null; a = b, b = b->fa) { b->splay(); if(b->fa == null) { if(a != null) a->add(c); if(b->ch[1] != null) b->ch[1]->add(c); b->key += c; b->Max += c; return; } b->setc(a, 1); b->pushup(); } } void work() { int k, a, b, c; bfs(); while(m--) { scanf("%d", &k); if(k == 1) { scanf("%d%d", &a, &b); Node[a]->link(Node[b]); } if(k == 2) { scanf("%d%d", &a, &b); Node[b]->cut(Node[a]); } if(k == 3) { scanf("%d%d%d", &c, &a, &b); if(Node[a]->findroot() != Node[b]->findroot()) printf("-1\n"); else update(Node[a], Node[b], c); } if(k == 4) { scanf("%d%d", &a, &b); if(Node[a]->findroot() != Node[b]->findroot()) printf("-1\n"); else printf("%d\n", query(Node[a], Node[b])); } } printf("\n"); } int main(void) { while(scanf("%d", &n)!=EOF) { init(); read(); work(); } return 0; }