题目:
http://acm.hdu.edu.cn/showproblem.php?pid=4010
题意:
给一棵树,四种操作
1:添加(x,y)边,若x,y在同一棵树上,则操作非法
2:删除(x,y)边,若x = y 或者 x,y不在同一棵树上,则操作非法
3:x--y的路径上每一个结点的权值增加w,若x,y不在同一棵树上,则操作非法(注意这里给的顺序是w x y 不是x y w...ORZ看题不仔细又给跪了)
4:输出路径x--y上的结点最大值,若x,y不在同一棵树上,则操作非法
对非法操作均输出-1,合法操作123不输出值
思路:
动态树之Link-Cut Trees模板题,其实这题我写了两遍,第一次碰动态树的时候写了一次,写完hdu5002另一道动态树之后又回来优化了下之前的代码
都是LCT的基本操作,没什么好讲的,这里简单说一下怎么在LCT树上获得两点之间的路径,三步走
changeRoot(nodes + x); //旋转【树】,使得 x 结点成为【树】的根节点
access(nodes + y);<span style="white-space:pre"> </span>//使 y 到根节点的路径处于同一【伸展树】上(该【伸展树】包含也只包含 y 到根节点路径上的所有结点)
splay(nodes + x);<span style="white-space:pre"> </span>//旋转【伸展树】,使得 x 结点成为【伸展树】的根节点
这个操作很重要,很重要,很重要,重要的事情说三遍,要分清楚哪些是【树】上的操作,哪些是【伸展树】上的操作,如果树链剖分用线段树维护树链一样,这里的伸展树也只是一种链的维护工具。经过这三步之后,以x为根的伸展树上的所有结点即为树上 x 到 y 的路径上的结点,要对这条路径进行某些操作,都可以通过打标记的方式,在x结点上处理,在通过旋转时的push_down延伸到路径结点上去,个人认为这才是LCT的核心思想,使用懒标记进行路径操作处理
代码:
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
const int MAXSIZE = 3 * 1e6 + 100;
struct Node{
Node *ch[2], *pnt;
bool rt;
bool rev;
int add;
int wei;
int maximum;
};
Node nodes[MAXSIZE];
Node *null = &nodes[0];
int ncnt;
void push_up(Node *x){
x->maximum = x->wei;
if (x->ch[0] != null)
x->maximum = max(x->maximum, x->ch[0]->maximum + x->ch[0]->add);
if (x->ch[1] != null)
x->maximum = max(x->maximum, x->ch[1]->maximum + x->ch[1]->add);
}
void push_down(Node *x){
if (x == null) return;
if (x->add != 0){
if (x->ch[0] != null) x->ch[0]->add += x->add;
if (x->ch[1] != null) x->ch[1]->add += x->add;
x->wei += x->add;
x->add = 0;
}
if (x->rev){
swap(x->ch[0],x->ch[1]);
x->ch[0]->rev = !x->ch[0]->rev;
x->ch[1]->rev = !x->ch[1]->rev;
x->rev = false;
}
}
Node* newNode(int wei){
Node* t = &nodes[ncnt++];
t->pnt = t->ch[0] = t->ch[1] = null;
t->rt = true;
t->wei = t->maximum = wei;
t->add = 0;
t->rev = false;
return t;
}
void init(){
ncnt = 1;
null->pnt = null->ch[0] = null->ch[1] = null;
}
void srotate(Node *x, bool d){
Node *y = x->pnt;
y->ch[!d] = x->ch[d];
if (x->ch[d] != null)
x->ch[d]->pnt = y;
x->pnt = y->pnt;
if (!y->rt){
if (y == y->pnt->ch[d])
y->pnt->ch[d] = x;
else
y->pnt->ch[!d] = x;
}
x->ch[d] = y;
y->pnt = x;
if (y->rt){
y->rt = false;
x->rt = true;
}
push_up(y);
}
void pushdownAll(Node *x){
if (!x->rt) pushdownAll(x->pnt);
push_down(x);
}
void splay(Node *x){
Node *y;
pushdownAll(x);
while (!x->rt){
y = x->pnt;
if (x == y->ch[0]){
if (!y->rt && y == y->pnt->ch[0])
srotate(y, true);
srotate(x, true);
}
else{
if (!y->rt && y == y->pnt->ch[1])
srotate(y, false);
srotate(x, false);
}
}
push_up(x);
}
void access(Node* u){
Node *v = null;
while (u != null){
splay(u);
u->ch[1]->rt = true;
(u->ch[1] = v)->rt = false;
push_up(u);
v = u;
u = u->pnt;
}
}
void cut(Node* v) {
access(v);
splay(v);
v->ch[0]->rt = true;
v->ch[0]->pnt = null;
v->ch[0] = null;
push_up(v);
}
void changeRoot(Node *x){
access(x);
splay(x);
x->rev = !x->rev;
}
void link(Node *v, Node *u){
changeRoot(v);
v->pnt = u;
}
Node* findRoot(Node* v) {
access(v);
splay(v);
while (v->ch[0] != null){
v = v->ch[0];
push_down(v);
}
return v;
}
int arr[MAXSIZE];
int edges[MAXSIZE][2];
int main(){
int n;
int i,j,k;
int a,b,c;
while (scanf("%d",&n)!=EOF){
init();
for (i=1;i<n;++i)
scanf("%d %d",&edges[i][0],&edges[i][1]);
for (i=0;i<n;++i){
scanf("%d",arr+i);
newNode(arr[i]);
}
for (i=1;i<n;++i)
link(nodes + edges[i][0], nodes + edges[i][1]);
int q;
scanf("%d",&q);
while (q--){
scanf("%d",&a);
switch (a){
case 1:
scanf("%d %d",&a,&b);
if (findRoot(nodes + a) == findRoot(nodes + b))
printf("-1\n");
else link(nodes + a, nodes+b);
break;
case 2:
scanf("%d %d",&a,&b);
if (a==b || findRoot(nodes + a) != findRoot(nodes + b))
printf("-1\n");
else {
changeRoot(nodes + a);
cut(nodes + b);
}
break;
case 3:
scanf("%d %d %d",&a,&b,&c);
if (findRoot(nodes + b) != findRoot(nodes + c))
printf("-1\n");
else {
changeRoot(nodes + b);
access(nodes + c);
splay(nodes + b);
(nodes + b)->add += a;
}
break;
case 4:
scanf("%d %d",&a,&b);
if (findRoot(nodes + a) != findRoot(nodes + b))
printf("-1\n");
else {
changeRoot(nodes + a);
access(nodes + b);
splay(nodes + a);
printf("%d\n",(nodes+a)->maximum + (nodes + a)->add);
}
break;
}
}
putchar(10);
}
return 0;
}