spoj Query on a tree(树链剖分模板题)

375. Query on a tree

Problem code: QTREE

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.

Example

Input:
1

3
1 2 1
2 3 2
QUERY 1 2
CHANGE 1 3
QUERY 1 2
DONE

Output:
1
3

题意:

给你一颗树。然后你可以对这个树就行两种操作。

1.修改某条边的边权。

2.查询u->v路径上。最大的边权。

题目链接点击打开链接

思路:

没学树链剖分前,觉得这个算法比较难,学了下发现没有想象的那么难。

讲解见点击打开链接还是蛮浅显易懂的。

#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=10010;
typedef long long ll;
#define lson L,mid,ls
#define rson mid+1,R,rs
int fa[maxn],dep[maxn],sz[maxn],son[maxn], id[maxn],top[maxn],cnt,ptr;
//记sz[u]表示以u为根的子树的节点数,dep[u]表示u的深度(根深度为1),top[u]表示u所在的重链的顶端节点,
//fa[u]表示u的父亲,son[u]表示与u在同一重链上的u的儿子节点(姑且称为重儿子),id[u]表示u与其父亲节点的连边(姑且称为u的父边)在线段树中的位置。
struct node
{
    int v;
    node *next;
} ed[maxn<<1],*head[maxn];
int mav[maxn<<2],uu[maxn],vv[maxn],wei[maxn];
void build(int L,int R,int rt)
{
    mav[rt]=-INF;
    if(L==R)
        return;
    int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;
    build(lson);
    build(rson);
}
void update(int L,int R,int rt,int p,int d)
{
    if(L==R)
    {
        mav[rt]=d;
        return;
    }
    int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;
    if(p<=mid)
        update(lson,p,d);
    else
        update(rson,p,d);
    mav[rt]=max(mav[ls],mav[rs]);
}
int qu(int L,int R,int rt,int l,int r)
{
    if(l<=L&&R<=r)
        return mav[rt];
    int ls=rt<<1,rs=ls|1,mid=(L+R)>>1,tp=-INF;
    if(l<=mid)
        tp=max(tp,qu(lson,l,r));
    if(r>mid)
        tp=max(tp,qu(rson,l,r));
    return tp;
}
void adde(int u,int v)
{
    ed[cnt].v=v;
    ed[cnt].next=head[u];
    head[u]=&ed[cnt++];
}
void dfs(int u)
{
    int v,ms=-INF;
    sz[u]=1,son[u]=-1;
    for(node *p=head[u];p!=NULL;p=p->next)
    {
        v=p->v;
        if(v==fa[u])
            continue;
        fa[v]=u;
        dep[v]=dep[u]+1;
        dfs(v);
        if(sz[v]>ms)
            ms=sz[v],son[u]=v;
        sz[u]+=sz[v];
    }
}
void getid(int u,int ft)
{
    id[u]=++ptr,top[u]=ft;
    if(son[u]!=-1)
        getid(son[u],top[u]);
    for(node *p=head[u];p!=NULL;p=p->next)
    {
        if(p->v==fa[u]||p->v==son[u])
            continue;
        getid(p->v,p->v);
    }
}
void init(int rt)
{
    fa[rt]=-1;
    dep[rt]=ptr=0;
    dfs(rt);
    getid(rt,rt);//根没有父边。但是在线段树中占了1的位置。但是不会用到。只是写起来方便
    build(1,ptr,1);//1-ptr。ptr条边。是算上根的虚拟边的。
}
inline int calc(int u,int v)
{
    int f1=top[u],f2=top[v],tp=-INF;
    while(f1!=f2)
    {
        if(dep[f1]<dep[f2])
            swap(f1,f2),swap(u,v);
        tp=max(tp,qu(1,ptr,1,id[f1],id[u]));
        u=fa[f1],f1=top[u];//开始把fa[f1]写成了fa[u]。T了一早上。。。哎。。。
    }
    if(u==v)
        return tp;
    if(dep[u]>dep[v])
        swap(u,v);
    tp=max(tp,qu(1,ptr,1,id[son[u]],id[v]));
    return tp;
}
int main()
{
    int n,u,v,w,t,rt,i;
    char cmd[100];

    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        rt=(n+1)/2;
        cnt=0;
        memset(head,0,sizeof head);
        for(i=1;i<n;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            uu[i]=u,vv[i]=v,wei[i]=w;
            adde(u,v);
            adde(v,u);
        }
        init(rt);
        for(i=1;i<n;i++)
        {
            if(dep[uu[i]]>dep[vv[i]])
                swap(uu[i],vv[i]);
            update(1,ptr,1,id[vv[i]],wei[i]);
        }
        while(1)
        {
            scanf("%s",cmd);
            if(cmd[0]=='D')
                break;
            scanf("%d%d",&u,&v);
            if(cmd[0]=='C')
                update(1,ptr,1,id[vv[u]],v);
            else
                printf("%d\n",calc(u,v));
        }
    }
    return 0;
}
时间: 2024-08-24 00:51:21

spoj Query on a tree(树链剖分模板题)的相关文章

SPOJ 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 tior QUERY a b : ask fo

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 f

SPOJ - QTREE 375 Query on a tree 树链剖分+线段树

操作1:修改第k条边权. 操作2:询问两点间最大边权. 树链剖分,然后线段树维护最大值 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<set> #include<map> #include<queue> #include<vector> #inclu

SPOJ375 Query on a tree 树链剖分

SPOJ375  Query on a tree   树链剖分 no tags 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

HDU 3966 Aragorn&#39;s Story(树链剖分 模板题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who want to invade his kingdom. As Aragorn knows, th

BZOJ 2243 染色 | 树链剖分模板题进阶版

BZOJ 2243 染色 | 树链剖分模板题进阶版 这道题呢~就是个带区间修改的树链剖分~ 如何区间修改?跟树链剖分的区间询问一个道理,再加上线段树的区间修改就好了. 这道题要注意的是,无论是线段树上还是原树上,把两个区间的信息合并的时候,要注意中间相邻两个颜色是否相同. 这代码好长啊啊啊啊 幸好一次过了不然我估计永远也De不出来 #include <cstdio> #include <cstring> #include <algorithm> using namesp

SPOJ QTREE Query on a tree ——树链剖分 线段树

[题目分析] 垃圾vjudge又挂了. 树链剖分裸题. 垃圾spoj,交了好几次,基本没改动却过了. [代码](自带常数,是别人的2倍左右) #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 20005 int T,n,fr[maxn],h[maxn],to[maxn],ne[maxn]

spoj 375 QTREE - Query on a tree 树链剖分

题目链接 给一棵树, 每条边有权值, 两种操作, 一种是将一条边的权值改变, 一种是询问u到v路径上最大的边的权值. 树链剖分模板. #include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <map> #include <set&g

spoj 375 Query on a tree (树链剖分)

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 Q