hdu4010:Query on The Trees

题意:

  给出一颗树,有4种操作:

 1、如果x和y不在同一棵树上则在xy连边

 2、如果x和y在同一棵树上并且x!=y则把x换为树根并把y和y的父亲分离

 3、如果x和y在同一棵树上则x到y的路径上所有的点权值+w

 4、如果x和y在同一棵树上则输出x到y路径上的最大值

第一道LCT(学了一整天啊。。太蠢了!)
推荐两份资料:
1.杨哲集训队作业《QTREE解法的一些研究》
2. link-cut trees (讲的很详细)

【代码】

(打的比较长,但是应该比较清晰)

#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#define N 300005 
#define M 2001005
#define INF 1e9
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pa;

int read()
{
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}
    while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
    return x*f;
}

int n,m,top,cnt;
int b[N<<1],p[N],nextedge[N<<1];
int mx[N],tag[N],a[N],son[N][2],q[N],fa[N];
bool rev[N];

void Add(int x,int y)
{
    cnt++;
    b[cnt]=y;
    nextedge[cnt]=p[x];
    p[x]=cnt;
}

void Anode(int x,int y){
    Add(x,y);Add(y,x);
}

void dfs(int x)
{
    for(int i=p[x];i;i=nextedge[i])
    {
        int v=b[i];
        if(v==fa[x]) continue;
        fa[v]=x;dfs(v);
    }
}

void Input_Init()
{
    for(int i=0;i<=n;i++) p[i]=rev[i]=tag[i]=fa[i]=son[i][0]=son[i][1]=0;
    mx[0]=-INF;cnt=0;
    for(int i=1;i<n;i++)
    {
        static int x,y;
        x=read(),y=read();
        Anode(x,y);
    }
    for(int i=1;i<=n;i++) mx[i]=a[i]=read();
    dfs(1);
}

void pushup(int x){
    mx[x]=max(a[x],max(mx[son[x][0]],mx[son[x][1]]));
}

void pushdown(int p)
{
    int l=son[p][0],r=son[p][1];
    if(rev[p])
    {
        rev[p]=0,rev[l]^=1,rev[r]^=1;
        swap(son[p][0],son[p][1]);  
    }
    if(tag[p])
    {
        if(l) tag[l]+=tag[p],mx[l]+=tag[p],a[l]+=tag[p];
        if(r) tag[r]+=tag[p],mx[r]+=tag[p],a[r]+=tag[p];
        tag[p]=0;
    }
}

bool isroot(int x){
    return son[fa[x]][0]!=x&&son[fa[x]][1]!=x;
}

void Rotate(int x)
{
    int y=fa[x],z=fa[y],l,r;
    l=son[y][1]==x,r=l^1;
    if(!isroot(y)) son[z][son[z][1]==y]=x;
    fa[x]=z,fa[y]=x;fa[son[x][r]]=y;
    son[y][l]=son[x][r];son[x][r]=y;
    pushup(y),pushup(x);
}

void Splay(int x)
{
    top=0;q[++top]=x;
    for(int i=x;!isroot(i);i=fa[i])
        q[++top]=fa[i];
    while(top) pushdown(q[top--]);
    while(!isroot(x))
    {
        int y=fa[x],z=fa[y];
        if(!isroot(y))
        {
            if(son[z][0]==y^son[y][0]==x) Rotate(x);
            else Rotate(y);
        }
        Rotate(x);
    }
}

void Access(int x)
{
    for(int t=0;x;t=x,x=fa[x])
        Splay(x),son[x][1]=t,pushup(x);
}

int Find(int x)
{
    Access(x);Splay(x);
    while(son[x][0]) x=son[x][0];
    return x;
}

void Make_Root(int x){
    Access(x);Splay(x);rev[x]^=1;
}

void Link(int x,int y){
    Make_Root(x);fa[x]=y;
}

void Solve_Link()
{
    static int x,y;
    x=read(),y=read();
    if(Find(x)==Find(y)) printf("-1\n");
    else Link(x,y);
}

void Cut(int x,int y)
{
    Make_Root(x);Access(y);Splay(y);
    son[y][0]=fa[son[y][0]]=0;pushup(y);
}

void Solve_Cut()
{
    static int x,y;
    x=read(),y=read();
    if(Find(x)!=Find(y)||x==y) printf("-1\n");
    else Cut(x,y);
}

void Increase(int x,int y,int z)
{
    Make_Root(x);Access(y);Splay(y);
    tag[y]+=z;mx[y]+=z;a[y]+=z;
}

void Solve_Add()
{
    static int x,y,z;
    z=read(),x=read(),y=read();
    if(Find(x)!=Find(y)) printf("-1\n");
    else Increase(x,y,z);
}

void Query(int x,int y)
{
    Make_Root(x);Access(y);Splay(y);
    printf("%d\n",mx[y]);
}

void Solve_Query()
{
    static int x,y;
    x=read(),y=read();
    if(Find(x)!=Find(y)) printf("-1\n");
    else Query(x,y);
}

void Solve()
{
    m=read();
    while(m--)
    {
        static int f;
        f=read();
        if(f==1) Solve_Link();
        else if(f==2) Solve_Cut();
        else if(f==3) Solve_Add();
        else Solve_Query();
    }
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        Input_Init();
        Solve();
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值