luogu4114 spoj qtree1

本文介绍了一种解决树形结构中路径最大值查询及边权更新问题的方法。通过两次深度优先搜索预处理树的重链剖分,并利用线段树进行区间查询与单点更新操作,实现高效的路径查询与边权更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.elijahqi.win/2018/02/11/spoj-qtree1-luogu4114/
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 a, b 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.29启程长沙的时候早晨起来敲的复习板子 然而在noiwc 2018上却成为了tj选手中的倒数第一 qwq 太菜太菜了
洛谷到是很快A了 spoj死活过不去 菜死 直到今天才调过

洛谷:洛谷数据比较水..建议写过之后再去spoj挑战下

#include<cstdio>
#include<algorithm>
#define N 110000
using namespace std;
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;
}
int h[N],size[N],fa[N],n,num,top[N],w[N],a[N],id[N],dep[N],son[N],root;
char s[10];
struct node{
    int y,z,next;
}data[N<<1];
struct node1{
    int left,right,max;
}tree[N<<1];
inline 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 (y==fa[x]) continue;fa[y]=x;a[y]=z;
        dep[y]=dep[x]+1;dfs1(y);size[x]+=size[y];if (size[son[x]]<size[y]) son[x]=y;
    }
}
inline void dfs2(int x,int tp){
    id[x]=++num;top[x]=tp;w[num]=a[x];
    if (son[x]) dfs2(son[x],tp);
    for (int i=h[x];i;i=data[i].next){
        int y=data[i].y;if (fa[x]==y||y==son[x]) continue;
        dfs2(y,y);
    }
}
inline void update(int x){
    tree[x].max=max(tree[tree[x].left].max,tree[tree[x].right].max);
}
inline void build(int &x,int l,int r){
    x=++num;if(l==r) {tree[x].max=w[l];return;} 
    int mid=l+r>>1;build(tree[x].left,l,mid);
    build(tree[x].right,mid+1,r);update(x);
}
inline int query(int x,int l,int r,int l1,int r1){
    if (l1<=l&&r1>=r) return tree[x].max;int mid=l+r>>1,tmp=0;
    if(l1<=mid) tmp=max(tmp,query(tree[x].left,l,mid,l1,r1));
    if (r1>mid) tmp=max(tmp,query(tree[x].right,mid+1,r,l1,r1));return tmp; 
}
inline void change(int x,int l,int r,int p,int v){
    if (l==r) {tree[x].max=v;return;}int mid=l+r>>1;
    if (p<=mid) change(tree[x].left,l,mid,p,v);else change(tree[x].right,mid+1,r,p,v);update(x);
}
int main(){
    //freopen("4114.in","r",stdin);
    n=read();
    for (int i=1;i<n;++i){
        int x=read(),y=read(),z=read();
        data[++num].y=y;data[num].z=z;data[num].next=h[x];h[x]=num;
        data[++num].y=x;data[num].z=z;data[num].next=h[y];h[y]=num;
    }dfs1(1);num=0;dfs2(1,1);num=0;build(root,1,n);
    while(1){
        scanf("%s",s);if (s[0]=='D') return 0;int x=read(),y=read();
        if (s[0]=='Q'){
            int ans=0;if(x==y) {puts("0");continue;}
            while(top[x]!=top[y]){
                if (dep[top[x]]<dep[top[y]]) swap(x,y);
                ans=max(ans,query(root,1,n,id[top[x]],id[x]));x=fa[top[x]];
            }if (id[x]>id[y]) swap(x,y);if(id[x]==id[y]){printf("%d\n",ans);continue;}
            ans=max(ans,query(root,1,n,id[x]+1,id[y]));printf("%d\n",ans);
        }
        if (s[0]=='C'){
            x=data[(x<<1)-1].y;x=id[x];change(root,1,n,x,y);
        }
    }
    return 0;
}

spoj版: 注意多组数据需要针对重儿子和h数组清0 并且题目中给边的顺序不一定是按照深度小到深度大这样逐次给的而是乱序给的所以输出答案的时候注意判断一下

#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 110000
using namespace std;
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;
}
int h[N],size[N],fa[N],n,num,top[N],w[N],a[N],id[N],dep[N],son[N],root;
char s[10];
struct node{
    int y,z,next;
}data[N<<1];
struct node1{
    int left,right,max;
}tree[N<<1];
inline 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 (y==fa[x]) continue;fa[y]=x;a[y]=z;
        dep[y]=dep[x]+1;dfs1(y);size[x]+=size[y];if (size[son[x]]<size[y]) son[x]=y;
    }
}
inline void dfs2(int x,int tp){
    id[x]=++num;top[x]=tp;w[num]=a[x];
    if (son[x]) dfs2(son[x],tp);
    for (int i=h[x];i;i=data[i].next){
        int y=data[i].y;if (fa[x]==y||y==son[x]) continue;
        dfs2(y,y);
    }
}
inline void update(int x){
    tree[x].max=max(tree[tree[x].left].max,tree[tree[x].right].max);
}
inline void build(int &x,int l,int r){
    x=++num;if(l==r) {tree[x].max=w[l];return;} 
    int mid=l+r>>1;build(tree[x].left,l,mid);
    build(tree[x].right,mid+1,r);update(x);
}
inline int query(int x,int l,int r,int l1,int r1){
    if (l1<=l&&r1>=r) return tree[x].max;int mid=l+r>>1,tmp=-0x3f3f3f3f;
    if(l1<=mid) tmp=max(tmp,query(tree[x].left,l,mid,l1,r1));
    if (r1>mid) tmp=max(tmp,query(tree[x].right,mid+1,r,l1,r1));return tmp; 
}
inline void change(int x,int l,int r,int p,int v){
    if (l==r) {tree[x].max=v;return;}int mid=l+r>>1;
    if (p<=mid) change(tree[x].left,l,mid,p,v);else change(tree[x].right,mid+1,r,p,v);update(x);
}
int main(){
    freopen("1.in","r",stdin);
    int T=read();
    while(T--){
        n=read();memset(h,0,sizeof(h));num=0;memset(son,0,sizeof(son));
        for (int i=1;i<n;++i){
            int x=read(),y=read(),z=read();
            data[++num].y=y;data[num].z=z;data[num].next=h[x];h[x]=num;
            data[++num].y=x;data[num].z=z;data[num].next=h[y];h[y]=num;
        }dfs1(1);num=0;dfs2(1,1);num=0;build(root,1,n);
        while(1){
            scanf("%s",s);if (s[0]=='D') break;int x=read(),y=read();
            if (s[0]=='Q'){
                int ans=-0x3f3f3f3f;if(x==y) {puts("0");continue;}
                while(top[x]!=top[y]){
                    if (dep[top[x]]<dep[top[y]]) swap(x,y);
                    ans=max(ans,query(root,1,n,id[top[x]],id[x]));x=fa[top[x]];
                }if (id[x]>id[y]) swap(x,y);if(id[x]==id[y]){printf("%d\n",ans);continue;}
                ans=max(ans,query(root,1,n,id[x]+1,id[y]));printf("%d\n",ans);
            }
            if (s[0]=='C'){
                int xx=data[(x<<1)-1].y,yy=data[(x<<1)].y;if (dep[xx]<dep[yy])swap(xx,yy);
                x=xx;x=id[x];change(root,1,n,x,y);
            }
        }   
    } 
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值