hdu 4010

11360 단어 hdu
#include 
#include 
#include 
#include 

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;
}

좋은 웹페이지 즐겨찾기