hdu 4010



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

template<class Num>void write(Num x)
{
    if(x == 0) {putchar('0');return;}
    static char s[20]; int sl = 0;
    if(x < 0) x = -x, putchar('-');
    while(x) s[sl++] = x % 10 + '0', x /= 10;
    while(sl) putchar(s[--sl]);
}

template<class Num>void read(Num &x)
{
    char c; int flag = 1;
    while((c = getchar()) < '0' || c > '9')
        if(c == '-') flag *= -1;
    x = c - '0';
    while((c = getchar()) >= '0' && c <= '9')
        x *= 10, x += c - '0';
    x *= flag;
}

const int maxn = 3e5 + 20;

int n, q;

int fa[maxn], c[maxn][2];
int add[maxn], rev[maxn], w[maxn], max[maxn];

bool isroot(int x)
{
    return c[fa[x]][0] != x && c[fa[x]][1] != x;
}
void pushdown(int x)
{
    if(rev[x])
    {
        std::swap(c[x][0], c[x][1]);
        if(c[x][0]) rev[c[x][0]] ^= 1;
        if(c[x][1]) rev[c[x][1]] ^= 1;
        rev[x] = 0;
    }
    if(add[x])
    {
        if(c[x][0])
        {
            w[c[x][0]] += add[x];
            max[c[x][0]] += add[x];
            add[c[x][0]] += add[x];
        }
        if(c[x][1])
        {
            w[c[x][1]] += add[x];
            max[c[x][1]] += add[x];
            add[c[x][1]] += add[x];
        }
        add[x] = 0;
    }
}
void update(int x)
{
    max[x] = w[x];
    if(c[x][0]) max[x] = std::max(max[c[x][0]], max[x]);
    if(c[x][1]) max[x] = std::max(max[c[x][1]], max[x]);
}
void setc(int x,int i,int y)
{
    c[x][i] = x ? y : 0, fa[y] = y ? x : 0;
}
void rotate(int x)
{
    int y = fa[x];
    pushdown(y), pushdown(x);

    int t = c[y][1] == x;

    fa[x] = fa[y];
    if(!isroot(y)) setc(fa[y], c[fa[y]][1] == y, x);

    setc(y, t, c[x][t ^ 1]), setc(x, t ^ 1, y);
    update(y), update(x);
}
void spaly(int u)
{
    pushdown(u);
    while(!isroot(u)) rotate(u);
}
void access(int u)
{
    int o = u, v = 0;

    while(u)
    {
        spaly(u), c[u][1] = v, update(u);
        v = u, u = fa[u];
    }

    spaly(o);
}
int getroot(int u)
{
    access(u);

    while(c[u][0])
    {
        u = c[u][0];
        pushdown(u);
    }
    return u;
}
void makeroot(int u)
{
    access(u), rev[u] ^= 1;
}
void link(int u,int v)
{
    makeroot(u), fa[u] = v;
}
void cut(int u,int v)
{
    makeroot(u), access(v);
    fa[c[v][0]] = fa[v];
    fa[v] = c[v][0] = 0;
    update(v);
}
/*
void cut(int u,int v)
{
    makeroot(u), fa[u] = v;
}
*/
void solve()
{
    int v, x, y;

    read(q);
    while(q--)
    {
        read(v);
        if(v == 1)
        {
            read(x), read(y);
            if(getroot(x) != getroot(y))
                link(x, y);
            else
                puts("-1");
        }
        if(v == 2)
        {
            read(x), read(y);
            if(x != y && getroot(x) == getroot(y))
                cut(x, y);
            else
                puts("-1");
        }
        if(v == 4)
        {
            read(x), read(y);
            if(getroot(x) == getroot(y))
            {
                makeroot(y), access(x);
                write(max[x]), puts("");
            }
            else
                puts("-1");
        }
        if(v == 3)
        {
            read(v), read(x), read(y);

            if(getroot(x) == getroot(y))
            {
                makeroot(y), access(x);
                max[x] += v, w[x] += v, add[x] += v;
            }
            else
                puts("-1");
        }
    }

    puts("");
}
bool init()
{
    static int ex[maxn], ey[maxn];

    if(scanf("%d", &n) == EOF) return false;
    for(int i = 1; i <= n; i++)
    {
        fa[i] = c[i][0] = c[i][1] = 0;
        rev[i] = add[i] = 0;
    }
    for(int i = 1, x, y; i < n; i++) read(ex[i]), read(ey[i]);
    for(int i = 1; i <= n; i++) read(w[i]), max[i] = w[i];
    for(int i = 1; i < n; i++) link(ex[i], ey[i]);
    return true;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
#endif

    while(init()) solve();

#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值