HDU 4010 Query on The Trees(动态树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4010

题意:一棵树,四种操作:

(1)若x和y不在一棵树上,将x和y连边;

(2)若x和y在一棵树上,将x变成树根,将y从x树上分离;

(3)若x和y在一棵树上,将x到y路径上的所有值增加det;

(4)若x和y在一棵树上,输出x到y路径上的最大值。

思路:1操作用link维护,2操作用cut,34操作先split(x,y),然后对y做tag,并且记录路径的max值。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
int tot,go[],next[],first[];
int ch[][],rev[],tag[],q[];
int st[],mx[],fa[],v[];
bool pd(int x){
return ch[fa[x]][]!=x&&ch[fa[x]][]!=x;
}
void insert(int x,int y){
tot++;go[tot]=y;next[tot]=first[x];first[x]=tot;
}
void addedge(int x,int y){
insert(x,y);insert(y,x);
}
void pushdown(int x){
int l=ch[x][],r=ch[x][];
if (rev[x]){
rev[x]^=;rev[l]^=;rev[r]^=;
std::swap(ch[x][],ch[x][]);
}
if (tag[x]){
if (l) tag[l]+=tag[x],v[l]+=tag[x],mx[l]+=tag[x];
if (r) tag[r]+=tag[x],mx[r]+=tag[x],v[r]+=tag[x];
tag[x]=;
}
}
void updata(int x){
int l=ch[x][],r=ch[x][];
mx[x]=std::max(v[x],std::max(mx[l],mx[r]));
}
void rotate(int x){
int y=fa[x],z=fa[y],l,r;
if (ch[y][]==x) l=;else l=;r=l^;
if (!pd(y)){
if (ch[z][]==y) ch[z][]=x;else ch[z][]=x;
}
fa[x]=z;fa[y]=x;fa[ch[x][r]]=y;
ch[y][l]=ch[x][r];ch[x][r]=y;
updata(y);updata(x);
}
void splay(int x){
int top=;st[++top]=x;
for (int i=x;!pd(i);i=fa[i])
st[++top]=fa[i];
for (int i=top;i;i--)
pushdown(st[i]);
while (!pd(x)){
int y=fa[x],z=fa[y];
if (!pd(y)){
if (ch[y][]==x^ch[z][]==y) rotate(x);
else rotate(y);
}
rotate(x);
}
}
void access(int x){
for (int t=;x;t=x,x=fa[x]){
splay(x);
ch[x][]=t;
updata(x);
}
}
void makeroot(int x){
access(x);splay(x);rev[x]^=;
}
void cut(int x,int y){
makeroot(x);access(y);splay(y);ch[y][]=fa[ch[y][]]=;updata(y);
}
void link(int x,int y){
makeroot(x);
fa[x]=y;
}
int find(int x){
access(x);splay(x);
while (ch[x][]) x=ch[x][];
return x;
}
void add(int x,int y,int val){
makeroot(x);access(y);splay(y);
mx[y]+=val;v[y]+=val;tag[y]+=val;
}
int main(){
int n,x,y,m,opt,w;
while (scanf("%d",&n)!=EOF){
mx[]=-;
for (int i=;i<=n;i++)
mx[i]=tag[i]=rev[i]=v[i]=ch[i][]=ch[i][]=fa[i]=;
memset(first,,sizeof first);tot=;
for (int i=;i<n;i++){
scanf("%d%d",&x,&y);
addedge(x,y);
}
for (int i=;i<=n;i++){
scanf("%d",&v[i]);
mx[i]=v[i];
}
int top=;
q[top]=;
for (int k=;k<=top;k++){
for (int i=first[q[k]];i;i=next[i]){
int pur=go[i];
if (pur==fa[q[k]]) continue;
fa[pur]=q[k];
q[++top]=pur;
}
}
top=;
scanf("%d",&m);
while (m--){
scanf("%d",&opt);
if (opt==){
scanf("%d%d",&x,&y);
if (find(x)==find(y)) {puts("-1");continue;}
link(x,y);
}
else
if (opt==){
scanf("%d%d",&x,&y);
if (find(x)!=find(y)||x==y) {puts("-1");continue;}
cut(x,y);
}
else
if (opt==){
scanf("%d%d%d",&w,&x,&y);
if (find(x)!=find(y)){puts("-1");continue;}
add(x,y,w);
}
else
if (opt==)
{
scanf("%d%d",&x,&y);
if (find(x)!=find(y)){
printf("-1\n");
continue;
}
makeroot(x);access(y);splay(y);
printf("%d\n",mx[y]);
}
}
printf("\n");
}
}
上一篇:HDU 4010.Query on The Trees 解题报告


下一篇:动态树(LCT):HDU 4010 Query on The Trees