【hdu4010】Query on The Trees

本文介绍了一道关于树形数据结构的算法题,包括四种操作:连接不同树、树的分割、更新节点权重及查询最大权重。使用了链接切割树(LCT)这一高效的数据结构来解决该问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description
We have met so many problems on the tree, so today we will have a query problem on a set of trees.
There are N nodes, each node will have a unique weight Wi. We will have four kinds of operations on it and you should solve them efficiently. Wish you have fun!

Input
There are multiple test cases in our dataset.
For each case, the first line contains only one integer N.(1 ≤ N ≤ 300000) The next N‐1 lines each contains two integers x, y which means there is an edge between them. It also means we will give you one tree initially.
The next line will contains N integers which means the weight Wi of each node. (0 ≤ Wi ≤ 3000)
The next line will contains an integer Q. (1 ≤ Q ≤ 300000) The next Q lines will start with an integer 1, 2, 3 or 4 means the kind of this operation.
1. Given two integer x, y, you should make a new edge between these two node x and y. So after this operation, two trees will be connected to a new one.
2. Given two integer x, y, you should find the tree in the tree set who contain node x, and you should make the node x be the root of this tree, and then you should cut the edge between node y and its parent. So after this operation, a tree will be separate into two parts.
3. Given three integer w, x, y, for the x, y and all nodes between the path from x to y, you should increase their weight by w.
4. Given two integer x, y, you should check the node weights on the path between x and y, and you should output the maximum weight on it.

Output
For each query you should output the correct answer of it. If you find this query is an illegal operation, you should output ‐1.
You should output a blank line after each test case.

Sample Input
5
1 2
2 4
2 5
1 3
1 2 3 4 5
6
4 2 3
2 1 2
4 2 3
1 3 5
3 2 1 4
4 1 4

Sample Output
3
-1
7

Hint

We define the illegal situation of different operations:
In first operation: if node x and y belong to a same tree, we think it’s illegal.
In second operation: if x = y or x and y not belong to a same tree, we think it’s illegal.
In third operation: if x and y not belong to a same tree, we think it’s illegal.
In fourth operation: if x and y not belong to a same tree, we think it’s illegal.

题意:
给出一颗树,有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裸题。

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define ll long long 
#define inf 2000000000
#define mod 1000000007
#define N 300005
int n,m,top,cnt;
int c[N][2];
int mx[N],fa[N],v[N],tag[N];
int q[N],a[N],b[N],Head[N],ret[N],Next[N];
bool rev[N];
using namespace std;
int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

void insert(int u,int v)
{
    cnt++;
    ret[cnt]=v;
    Next[cnt]=Head[u];
    Head[u]=cnt;
}

void dfs(int u,int f)
{
    fa[u]=f;
    for (int i=Head[u];i;i=Next[i])
    {
        int v=ret[i];
        if (v!=f) dfs(v,u);
    }
}

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

void update(int x)
{
    int l=c[x][0],r=c[x][1];
    mx[x]=max(mx[l],mx[r]);
    mx[x]=max(mx[x],v[x]);
}

void pushdown(int x)
{
    int l=c[x][0],r=c[x][1];
    if (rev[x])
    {
        rev[l]^=1;rev[r]^=1;rev[x]^=1;
        swap(c[x][0],c[x][1]);
    }
    if (tag[x])
    {
        if (l){tag[l]+=tag[x];mx[l]+=tag[x];v[l]+=tag[x];}
        if (r){tag[r]+=tag[x];mx[r]+=tag[x];v[r]+=tag[x];}
        tag[x]=0;
    }
}

void rotate(int x)
{
    int l,r,y=fa[x],z=fa[y];
    if (c[y][0]==x)l=0;else l=1;r=l^1;
    if (!isroot(y))
    {
        if (c[z][0]==y) c[z][0]=x;else c[z][1]=x;
    }
    fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
    c[y][l]=c[x][r];c[x][r]=y;
    update(y);update(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--]);
    int y,z;
    while (!isroot(x))
    {
        y=fa[x];z=fa[y];
        if (!isroot(y))
        {
            if (c[z][0]==y^c[y][0]==x) rotate(x);else rotate(y);
        }
        rotate(x);
    }
}

void access(int x)
{
    int t=0;
    while (x)
    {
        splay(x);
        c[x][1]=t;
        update(x);
        t=x;
        x=fa[x];
    }
}

void makeroot(int x)
{
    access(x);
    splay(x);
    rev[x]^=1;
}

void link(int x,int y)
{
    makeroot(x);
    fa[x]=y;
}

void cut(int x,int y)
{
    makeroot(x);
    access(y);
    splay(y);
    fa[c[y][0]]=0;c[y][0]=0;
    update(y);
}

void add(int x,int y,int w)
{
    makeroot(x);
    access(y);
    splay(y);
    tag[y]+=w;
    mx[y]+=w;
    v[y]+=w;
}

int find(int x)
{
    access(x);
    splay(x);
    for (int i=x;i;i=c[i][0])
    {
        if (c[i][0]==0) return i;
    }
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<=n;i++)
            Head[i]=tag[i]=rev[i]=fa[i]=c[i][0]=c[i][1]=0;
        mx[0]=-inf;cnt=0;
        for (int i=1;i<n;i++)
        {
            int u=read(),v=read();
            insert(u,v);insert(v,u);
        }
        for (int i=1;i<=n;i++)mx[i]=v[i]=read();
        dfs(1,0);
        m=read();
        while (m--)
        {
            int opt=read(),x=read(),y=read(),w;
            if (opt==1)
            {
                if (find(x)==find(y)) puts("-1");
                else link(x,y);
            }
            if (opt==2)
            {
                if (find(x)!=find(y)||x==y) puts("-1");
                else cut(x,y);
            }
            if (opt==3)
            {
                w=x;x=y;y=read();
                if (find(x)!=find(y)) puts("-1");
                else add(x,y,w);
            }
            if (opt==4)
            {
                if (find(x)!=find(y)) puts("-1");
                else
                {
                    makeroot(x);
                    access(y);
                    splay(y);
                    printf("%d\n",mx[y]);
                }
            }
        }
        puts("");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值