[HDU4010]Query on The Trees-动态树LCT(Link Cut Tree)

Query on The Trees

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.


感觉咱快忘记LCT怎么打了……于是决定找一道模板题练一练……
然后咱就在连样例都过不去的状态下调了一个多小时(╯‵□′)╯︵┻━┻~
总算过了样例一交,居然还Presentation Error……
咱的心情可以用下面的一个表情形容:

(╯‵□′)╯︵┻━┻

思路:
模板题能有什么思路……
除了3操作外都是LCT必备的操作啊……2操作还很贴心地把Cut操作的过程口述了一遍……
3操作直接makeroot其中一个点,旋转另一个节点并给它打tag就好了~
模板题就是模板题~(然而连样例都过不去的你有什么资格说啊喂)

P.S:千万不要学习咱这长到了一种新高度的代码排版风格……

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>

using namespace std;

const int N=300233;

inline int maxx(int a,int b){if(a>b)return a;return b;}

inline int read()
{
    int x=0;
    char ch=getchar();
    while(ch<'0' || '9'<ch)ch=getchar();
    while('0'<=ch && ch<='9')
    {
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    return x;
}

int n;

int to[N],nxt[N],beg[N],tot;

void add(int u,int v)
{
    to[++tot]=v;
    nxt[tot]=beg[u];
    beg[u]=tot;
    to[++tot]=u;
    nxt[tot]=beg[v];
    beg[v]=tot;
}

struct LCT
{
    int fa[N],ch[N][2],rev[N];
    int mx[N],tag[N],w[N];
    int q[N],top;

    void init()
    {
        memset(tag,0,sizeof(tag));
        memset(rev,0,sizeof(rev));
        memset(fa,0,sizeof(fa));
        memset(ch,0,sizeof(ch));
        memset(mx,0,sizeof(mx));
        memset(w,0,sizeof(w));
        top=0;
    }

    void read(int val)
    {
        w[++top]=val;
    }

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

    void push_up(int x)
    {
        mx[x]=maxx(w[x],maxx(mx[ch[x][0]],mx[ch[x][1]]));
    }

    void push_down(int x)
    {
        int l=ch[x][0],r=ch[x][1];
        if(rev[x])
        {
            swap(ch[x][0],ch[x][1]);
            if(l)rev[l]^=1;
            if(r)rev[r]^=1;
            rev[x]^=1;
        }
        if(tag[x])
        {
            if(l)w[l]+=tag[x],mx[l]+=tag[x],tag[l]+=tag[x];
            if(r)w[r]+=tag[x],mx[r]+=tag[x],tag[r]+=tag[x];
            tag[x]=0;
        }
    }

    void rotate(int x)
    {
        int faa=fa[x],gra=fa[faa];
        int l=(ch[faa][0]!=x),r=l^1;

        if(!isroot(faa))
        {
            if(ch[gra][0]==faa)ch[gra][0]=x;
            else ch[gra][1]=x;
        }
        fa[x]=gra;fa[faa]=x;
        fa[ch[x][r]]=faa;
        ch[faa][l]=ch[x][r];
        ch[x][r]=faa;
        push_up(faa);push_up(x);
    }

    void splay(int x)
    {
        q[top=1]=x;
        for(int i=x;!isroot(i);i=fa[i])
            q[++top]=fa[i];
        while(top)push_down(q[top--]);

        while(!isroot(x))
        {
            int faa=fa[x],gra=fa[faa];
            if(!isroot(faa))
            {
                if((ch[gra][0]==faa)^(ch[faa][0]==x))rotate(x);
                else rotate(faa);
            }
            rotate(x);
        }
    }

    void access(int x)
    {
        for(int t=0;x;t=x,x=fa[x])
        {
            splay(x);
            ch[x][1]=t;
            push_up(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);
        ch[y][0]=fa[ch[y][0]]=0;
        push_up(y);
    }

    int find(int x)
    {
        access(x);
        splay(x);
        while(ch[x][0])x=ch[x][0];
        return x;
    }

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

    int query(int x,int y)
    {
        makeroot(x);
        access(y);
        splay(y);
        return mx[y];
    }
}koishi;

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        koishi.init();
        memset(beg,0,sizeof(beg));
        tot=0;

        for(int i=1;i<n;i++)
            add(read(),read());

        for(int i=1;i<=n;i++)
            koishi.read(read());

        koishi.q[koishi.top=1]=1;
        for(int j=1;j<=koishi.top;j++)
        {
            int now=koishi.q[j];
            for(int i=beg[now],v;i;i=nxt[i])
                if((v=to[i])!=koishi.fa[now])
                {
                    koishi.fa[v]=now;
                    koishi.q[++koishi.top]=v;
                }
        }

        int q=read();
        while(q--)
        {
            int type=read(),x=read(),y=read(),w;
            switch(type)
            {
                case 1:
                    if(koishi.find(x)==koishi.find(y))puts("-1");
                    else koishi.link(x,y);
                break;

                case 2:
                    if(x==y || koishi.find(x)!=koishi.find(y))puts("-1");
                    else koishi.cut(x,y);
                break;

                case 3:
                    if(koishi.find(y)!=koishi.find(w=read()))puts("-1");
                    else koishi.add(y,w,x);
                break;

                case 4:
                    if(koishi.find(x)!=koishi.find(y))puts("-1");
                    else printf("%d\n",koishi.query(x,y));
            }
        }
        puts("");
    }

    return 0;
}
Nano-ESG数据资源库的构建基于2023年初至2024年秋季期间采集的逾84万条新闻文本,从中系统提炼出企业环境、社会及治理维度的信息。其构建流程首先依据特定术语在德语与英语新闻平台上检索,初步锁定与德国DAX 40成分股企业相关联的报道。随后借助嵌入技术对文本段落执行去重操作,以降低内容冗余。继而采用GLiNER这一跨语言零样本实体识别系统,排除与目标企业无关的文档。在此基础上,通过GPT-3.5与GPT-4o等大规模语言模型对文本进行双重筛选:一方面判定其与ESG议题的相关性,另一方面生成简明的内容概要。最终环节由GPT-4o模型完成,它对每篇文献进行ESG情感倾向(正面、中性或负面)的判定,并标注所涉及的ESG具体维度,从而形成具备时序特征的ESG情感与维度标注数据集。 该数据集适用于多类企业可持续性研究,例如ESG情感趋势分析、ESG维度细分类别研究,以及企业可持续性事件的时序演变追踪。研究者可利用数据集内提供的新闻摘要、情感标签与维度分类,深入考察企业在不同时期的环境、社会及治理表现。此外,借助Bertopic等主题建模方法,能够从数据中识别出与企业相关的核心ESG议题,并观察这些议题随时间的演进轨迹。该资源以其开放获取特性与连续的时间覆盖,为探究企业可持续性表现的动态变化提供了系统化的数据基础。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值