spoj QTREE - Query on a tree(树链刨分)

You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1.

We will ask you to perfrom some instructions of the following form:

  • CHANGE i ti : change the cost of the i-th edge to ti
    or
  • QUERY a b : ask for the maximum edge cost on the path from node a to node b

Input

The first line of input contains an integer t, the number of test cases (t <= 20). t test cases follow.

For each test case:

  • In the first line there is an integer N (N <= 10000),
  • In the next N-1 lines, the i-th line describes the i-th edge: a line with three integers a b c denotes an edge between ab of cost c (c <= 1000000),
  • The next lines contain instructions "CHANGE i ti" or "QUERY a b",
  • The end of each test case is signified by the string "DONE".

There is one blank line between successive tests.

Output

For each "QUERY" operation, write one integer representing its result.

树链刨分入门。题目有两种操作,修改某个边的值,查询两个点之间的最大边,用树链刨分刨分成轻重链重新编号,重链的点的编号是连续的,每次在一个重链中查询,这样就成了线段树的单点更新和区间查询的问题;

#include<bits/stdc++.h>
using namespace std;
const int MAXN=10010;
int head[MAXN],tot;
int son[MAXN],top[MAXN],fa[MAXN],p[MAXN],fp[MAXN],num[MAXN],deep[MAXN];
struct Edge{
    int to,next;
}edge[2*MAXN];
struct Node
{
    int l,r;
    int Max;
}segTree[MAXN*4];
int e[MAXN][3];
int pos;
void init()
{
    memset(head,-1,sizeof(head));
    memset(son,-1,sizeof(son));
    tot=0;pos=0;
}
void addedge(int x,int y)
{
    edge[tot].to=y;
    edge[tot++].next=head[x];
    head[x]=tot-1;
}
void dfs1(int u,int pre,int d)
{
    deep[u]=d;
    fa[u]=pre;
    num[u]=1;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].to;
        if(v!=pre)
        {
            dfs1(v,u,d+1);
            num[u]+=num[v];
            if(son[u]==-1||num[v]>num[son[u]])
                son[u]=v;
        }
    }
}
void getpos(int u,int sp)
{
    top[u]=sp;
    if(son[u] != -1)
    {
        p[u]=pos++;
        fp[p[u]]=u;
        getpos(son[u],sp);
    }
    else
    {
        p[u]=pos++;
        fp[p[u]]=u;
        return;
    }
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].to;
        if(v!=son[u]&&v!=fa[u])
            getpos(v,v);
    }
}
void build(int i,int l,int r)
{
    segTree[i].l=l;
    segTree[i].r=r;
    segTree[i].Max=0;
    if(l == r)return;
    int mid=(l+r)/2;
    build(i<<1,l,mid);
    build((i<<1)|1,mid+1,r);
}
void push_up(int i)
{
    segTree[i].Max=max(segTree[i<<1].Max,segTree[(i<<1)|1].Max);
}
void update(int i,int k,int val)
{
    if(segTree[i].l==k&&segTree[i].r==k)
    {
        segTree[i].Max=val;
        return;
    }
    int mid=(segTree[i].l + segTree[i].r)/2;
    if(k<=mid)update(i<<1,k,val);
    else update((i<<1)|1,k,val);
    push_up(i);
}
int query(int i,int l,int r)
{
    if(segTree[i].l==l&&segTree[i].r==r)
        return segTree[i].Max;
    int mid=(segTree[i].l+segTree[i].r)/2;
    if(r<=mid) 
    return query(i<<1,l,r);
    else if(l>mid) 
    return query((i<<1)|1,l,r);
    else return 
    max(query(i<<1,l,mid),query((i<<1)|1,mid+1,r));
}
int find(int u,int v)
{
    int f1=top[u],f2=top[v];
    int tmp=0;
    while(f1!=f2)
    {
        if(deep[f1]<deep[f2])
        {
            swap(f1,f2);
            swap(u,v);
        }
        tmp=max(tmp,query(1,p[f1],p[u]));
        u=fa[f1];f1=top[u];
    }
    if(u==v)
    return tmp;
    if(deep[u]>deep[v]) 
    swap(u,v);
    return max(tmp,query(1,p[son[u]],p[v]));
}
int main()
{
    int T;
    int n;
    scanf("%d",&T);
    while(T--)
    {
        init();
        scanf("%d",&n);
        for(int i=0;i<n-1;i++)
        {
            scanf("%d%d%d",&e[i][0],&e[i][1],&e[i][2]);
            addedge(e[i][0],e[i][1]);
            addedge(e[i][1],e[i][0]);
        }
        dfs1(1,0,0);
        getpos(1,1);
        build(1,0,pos-1);
        for(int i=0;i<n-1;i++)
        {
            if(deep[e[i][0]]>deep[e[i][1]])
                swap(e[i][0],e[i][1]);
            update(1,p[e[i][1]],e[i][2]);
        }
        char op[10];
        int u,v;
        while(scanf("%s",op)==1)
        {
            if(op[0]=='D')break;
            scanf("%d%d",&u,&v);
            if(op[0]=='Q')
                printf("%d\n",find(u,v));
            else update(1,p[e[u-1][1]],v);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值