poj3237

http://www.elijahqi.win/archives/853
Description

You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edges are numbered 1 through N − 1. Each edge is associated with a weight. Then you are to execute a series of instructions on the tree. The instructions can be one of the following forms:

CHANGE i v Change the weight of the ith edge to v
NEGATE a b Negate the weight of every edge on the path from a to b
QUERY a b Find the maximum weight of edges on the path from a to b
Input

The input contains multiple test cases. The first line of input contains an integer t (t ≤ 20), the number of test cases. Then follow the test cases.

Each test case is preceded by an empty line. The first nonempty line of its contains N (N ≤ 10,000). The next N − 1 lines each contains three integers a, b and c, describing an edge connecting nodes a and b with weight c. The edges are numbered in the order they appear in the input. Below them are the instructions, each sticking to the specification above. A lines with the word “DONE” ends the test case.

Output

For each “QUERY” instruction, output the result on a separate line.

Sample Input

1

3
1 2 1
2 3 2
QUERY 1 2
CHANGE 1 3
QUERY 1 2
DONE
Sample Output

1
3
Source

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; 
#define N 110000
#define inf 0x7fffffff
inline int read(){
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
struct node{
    int y,z,next;
}data[N<<1];
struct node1{
    int l,r,left,right,max,lazy,min;
}tree[N<<2];
int num,T,a[N],son1[N],size[N],fa[N],dep[N],son[N],tp[N],id[N],h[N],root,w[N],n;
void dfs1(int x){
    size[x]=1;
    for (int i=h[x];i;i=data[i].next){
        int y=data[i].y,z=data[i].z;
        if (fa[x]==y) continue;son1[i>>1]=y;a[y]=z;
        dep[y]=dep[x]+1;fa[y]=x;dfs1(y);size[x]+=size[y];
        if (size[y]>size[son[x]]) son[x]=y;
    }
} 
void dfs2(int x,int top){
    id[x]=++num;tp[x]=top;w[num]=a[x];
    if (son[x]) dfs2(son[x],top);
    for (int i=h[x];i;i=data[i].next){
        int y=data[i].y;
        if (fa[x]==y||son[x]==y) continue;
        dfs2(y,y);
    }
}
inline void update(int x){
    int l=tree[x].left,r=tree[x].right;
    tree[x].max=max(tree[l].max,tree[r].max);
    tree[x].min=min(tree[l].min,tree[r].min);
} 
void build(int &x,int l,int r){
    x=++num;tree[x].l=l;tree[x].r=r;tree[x].lazy=0;
    if (l==r){tree[x].max=tree[x].min=w[l];tree[x].left=tree[x].right=0;return;}
    int mid=l+r>>1;
    build(tree[x].left,l,mid);build(tree[x].right,mid+1,r);
    update(x);
}
void pushdown(int x){
    if (!tree[x].lazy) return;
    int l=tree[x].left,r=tree[x].right;
    tree[l].lazy^=1;tree[r].lazy^=1;
    tree[l].max*=-1;tree[r].max*=-1;
    tree[l].min*=-1;tree[r].min*=-1;
    swap(tree[l].max,tree[l].min);swap(tree[r].max,tree[r].min);
    tree[x].lazy=0;
}
int query(int x,int l,int r){
    if (!x) return -inf;
    if (l<=tree[x].l&&r>=tree[x].r) return tree[x].max;
    int mid=tree[x].l+tree[x].r>>1;int max1=-inf;pushdown(x);
    if (l<=mid) max1=max(max1,query(tree[x].left,l,r));
    if (r>mid) max1=max(max1,query(tree[x].right,l,r));
    return max1;
}
void change(int x,int l,int v){
    if (tree[x].l==tree[x].r){tree[x].max=tree[x].min=v;return;}
    int mid=tree[x].l+tree[x].r>>1;pushdown(x);
    if (l<=mid) change(tree[x].left,l,v);
    if (l>mid) change(tree[x].right,l,v);
    update(x);
}
void negate(int x,int l,int r){
    if(l<=tree[x].l&&r>=tree[x].r){tree[x].max*=-1;tree[x].min*=-1;swap(tree[x].min,tree[x].max);tree[x].lazy^=1;return;}
    int mid=tree[x].l+tree[x].r>>1;pushdown(x);
    if (l<=mid) negate(tree[x].left,l,r);
    if (r>mid) negate(tree[x].right,l,r);
    update(x);
}
int main(){
    //freopen("poj3237.in","r",stdin);
    T=read();
    while (T--){
        n=read();num=1;memset(son,0,sizeof(son));memset(h,0,sizeof(h));
        for (int i=1;i<n;++i){
            int x=read(),y=read(),z=read();
            data[++num].y=y;data[num].next=h[x];data[num].z=z;h[x]=num;
            data[++num].y=x;data[num].next=h[y];data[num].z=z;h[y]=num;
        }num=0;dep[1]=1;
        dfs1(1);dfs2(1,1);num=0;
    //  for (int i=1;i<=n;++i) printf("%d ",w[i]);
        build(root,1,n);
        char str1[10];
        while (1){
            scanf("%s",str1);
            if (str1[0]=='D') break;
            if (str1[0]=='Q'){
                int x=read(),y=read();int max1=-inf;
                while (tp[x]!=tp[y]){
                    if (dep[tp[x]]<dep[tp[y]]) swap(x,y);
                    max1=max(max1,query(root,id[tp[x]],id[x]));
                    x=fa[tp[x]];
                }
                if (id[x]>id[y]) swap(x,y);if (id[x]==id[y]){printf("%d\n",max1);continue;}
                max1=max(max1,query(root,id[x]+1,id[y]));
                printf("%d\n",max1);
            }
            if (str1[0]=='C'){
                int x=read(),y=read();
                change(root,id[son1[x]],y);
            }
            if (str1[0]=='N'){
                int x=read(),y=read();
                while (tp[x]!=tp[y]){
                    if (dep[tp[x]]<dep[tp[y]]) swap(x,y);
                    negate(root,id[tp[x]],id[x]);
                    x=fa[tp[x]];
                }
                if (id[x]>id[y]) swap(x,y);if (id[x]==id[y]) continue;
                negate(root,id[x]+1,id[y]);
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值