SP375 QTREE - Query on a tree
题意大意
给定\(n\)个点的树,边按输入顺序编号为\(1,2,...n-1\),要求作以下操作: CHANGE \(i\) \(t_i\) 将第\(i\)条边权值改为\(t_i\),QUERY \(a\) \(b\) 询问从\(a\)点到\(b\)点路径上的最大边权
有多组测试数据,每组数据以DONE结尾
题目描述
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
 
输入输出格式
输入格式:
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.
输出格式:
For each "QUERY" operation, write one integer representing its result.
输入输出样例
输入样例#1:
1
3
1 2 1
2 3 2
QUERY 1 2
CHANGE 1 3
QUERY 1 2
DONE
输出样例#1:
1
3
思路:
其实这个题也不算特别的难,如果你做过有关换边权为点权的题目的话,比如……月下“毛景树”,旅游等。自我感觉难度都比这道题要大,并且都涉及到化边权为点权然后树链剖分。
不多说了,我们来看这个题,这道题与普通的树链剖分题目不同的是,这道题目给出的是边权,但是普通的树链剖分题目都是给出的点权,那,该怎么办呢?不难发现,每个点有且只有一条边连向它的父亲结点,诶??有且只有一条?那!!我们就可以用这个点的点权去代替它与它父亲之间边的边权呀!!那这不就又成了树链剖分板子题了嘛?!\(QwQ\)。还有一个坑就是最后查询的时候,因为LCA的点权不在查询的路径范围内,因此要排除掉。
洛谷上此题的c++评测有些问题,下面给出c的代码(memset必须要全写,不然没法AC,我也不知道为什么):
#include <ctype.h>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#define maxn 10007
#define ls rt<<1
#define rs rt<<1|1
int id[maxn],d[maxn],n,cnt,son[maxn],top[maxn],maxx[maxn<<2],t;
int head[maxn],num,fa[maxn],siz[maxn],w[maxn],a[maxn];
char s[8];
struct node {
  int v,w,nxt;
}e[maxn<<1];
int max(int a, int b) { return a > b ? a : b; }
#define swap(A, B)   \
    {                \
        int __T = A; \
        A = B;       \
        B = __T;     \
    }
void ct(int u, int v, int w) {
  e[++num].v=v;
  e[num].w=w;
  e[num].nxt=head[u];
  head[u]=num;
}
void pushup(int rt) {
  maxx[rt]=max(maxx[ls],maxx[rs]);
}
void build(int rt, int l, int r) {
  if(l==r) {
    maxx[rt]=a[l];
    return;
  }
  int mid=(l+r)>>1;
  build(ls,l,mid);
  build(rs,mid+1,r);
  pushup(rt);
}
void modify(int rt, int l, int r, int L, int val) {
  if(l==r) {
    maxx[rt]=val;
    return;
  }
  int mid=(l+r)>>1;
  if(L<=mid) modify(ls,l,mid,L,val);
  else modify(rs,mid+1,r,L,val);
  pushup(rt);
}
int cmax(int rt, int l, int r, int L, int R) {
  if(L<=l&&r<=R) return maxx[rt];
  int ans=0;
  int mid=(l+r)>>1;
  if(L<=mid) ans=max(ans,cmax(ls,l,mid,L,R));
  if(R>mid) ans=max(ans,cmax(rs,mid+1,r,L,R));
  return ans;
}
void dfs1(int u) {
  siz[u]=1;
  for(int i=head[u];i;i=e[i].nxt) {
    int v=e[i].v;
    if(v!=fa[u]) {
      d[v]=d[u]+1;
      fa[v]=u;
      w[v]=e[i].w;
      dfs1(v);
      siz[u]+=siz[v];
      if(siz[v]>siz[son[u]]) son[u]=v;
    }
  }
}
void dfs2(int u, int t) {
  id[u]=++cnt;
  top[u]=t;
  a[cnt]=w[u];
  if(son[u]) dfs2(son[u],t);
  for(int i=head[u];i;i=e[i].nxt) {
    int v=e[i].v;
    if(v!=fa[u]&&v!=son[u]) dfs2(v,v);
  }
}
int query(int x, int y) {
  int ans=0,fx=top[x],fy=top[y];
  while(fx!=fy) {
    if(d[fx]<d[fy]) {
      swap(x,y);
      swap(fx,fy);
    }
    ans=max(ans,cmax(1,1,n,id[fx],id[x]));
    x=fa[fx],fx=top[x];
  }
  if(id[x]>id[y]) swap(x,y);
  ans=max(ans,cmax(1,1,n,id[x]+1,id[y]));
  return ans;
}
int main() {
  scanf("%d",&t);
  while(t--) {
    memset(head,0,sizeof(head));
    memset(siz,0,sizeof(siz));
    memset(id,0,sizeof(id));
    memset(top,0,sizeof(top));
    memset(son,0,sizeof(son));
    memset(w,0,sizeof(w));
    memset(maxx,0,sizeof(maxx));
    memset(a,0,sizeof(a));
    memset(fa,0,sizeof(fa));
    memset(d,0,sizeof(d));num=0;cnt=0;
    scanf("%d",&n);
    for(int i=1,u,v,w;i<n;++i) {
      scanf("%d%d%d",&u,&v,&w);
      ct(u,v,w),ct(v,u,w);
    }
    dfs1(1);dfs2(1,1);build(1,1,n);
    while(1) {
      scanf("%s",s);
      if(s[0]=='D') break;
      if(s[0]=='C') {
        int x,y;
        scanf("%d%d",&x,&y);
        x=d[e[x*2-1].v]<d[e[x<<1].v]?e[x<<1].v:e[x*2-1].v;
        modify(1,1,cnt,id[x],y);
      }
      if(s[0]=='Q') {
        int x,y;
        scanf("%d%d",&x,&y);
        printf("%d\n",query(x,y));
      }
    }
  }
  return 0;
}
接下来是在SPOJ上能够AC的c++代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#define maxn 10007
#define ls rt<<1
#define rs rt<<1|1
using namespace std;
int id[maxn],d[maxn],n,cnt,son[maxn],top[maxn],maxx[maxn<<2],t;
int head[maxn],num,fa[maxn],siz[maxn],w[maxn],a[maxn];
char s[8];
struct node {
  int v,w,nxt;
}e[maxn<<1];
inline void ct(int u, int v, int w) {
  e[++num].v=v;
  e[num].w=w;
  e[num].nxt=head[u];
  head[u]=num;
}
inline void pushup(int rt) {
  maxx[rt]=max(maxx[ls],maxx[rs]);
}
void build(int rt, int l, int r) {
  if(l==r) {
    maxx[rt]=a[l];
    return;
  }
  int mid=(l+r)>>1;
  build(ls,l,mid);
  build(rs,mid+1,r);
  pushup(rt);
}
void modify(int rt, int l, int r, int L, int val) {
  if(l==r) {
    maxx[rt]=val;
    return;
  }
  int mid=(l+r)>>1;
  if(L<=mid) modify(ls,l,mid,L,val);
  else modify(rs,mid+1,r,L,val);
  pushup(rt);
}
int cmax(int rt, int l, int r, int L, int R) {
  if(L<=l&&r<=R) return maxx[rt];
  int ans=0;
  int mid=(l+r)>>1;
  if(L<=mid) ans=max(ans,cmax(ls,l,mid,L,R));
  if(R>mid) ans=max(ans,cmax(rs,mid+1,r,L,R));
  return ans;
}
void dfs1(int u) {
  siz[u]=1;
  for(int i=head[u];i;i=e[i].nxt) {
    int v=e[i].v;
    if(v!=fa[u]) {
      d[v]=d[u]+1;
      fa[v]=u;
      w[v]=e[i].w;
      dfs1(v);
      siz[u]+=siz[v];
      if(siz[v]>siz[son[u]]) son[u]=v;
    }
  }
}
void dfs2(int u, int t) {
  id[u]=++cnt;
  top[u]=t;
  a[cnt]=w[u];
  if(son[u]) dfs2(son[u],t);
  for(int i=head[u];i;i=e[i].nxt) {
    int v=e[i].v;
    if(v!=fa[u]&&v!=son[u]) dfs2(v,v);
  }
}
int query(int x, int y) {
  int ans=0,fx=top[x],fy=top[y];
  while(fx!=fy) {
    if(d[fx]<d[fy]) {
      swap(x,y);
      swap(fx,fy);
    }
    ans=max(ans,cmax(1,1,n,id[fx],id[x]));
    x=fa[fx],fx=top[x];
  }
  if(id[x]>id[y]) swap(x,y);
  ans=max(ans,cmax(1,1,n,id[x]+1,id[y]));
  return ans;
}
int main() {
  scanf("%d",&t);
  while(t--) {
    memset(head,0,sizeof(head));
    memset(siz,0,sizeof(siz));
    memset(id,0,sizeof(id));
    memset(top,0,sizeof(top));
    memset(son,0,sizeof(son));
    memset(w,0,sizeof(w));
    memset(maxx,0,sizeof(maxx));
    memset(a,0,sizeof(a));
    memset(fa,0,sizeof(fa));
    memset(d,0,sizeof(d));num=0;cnt=0;
    scanf("%d",&n);
    for(int i=1,u,v,w;i<n;++i) {
      scanf("%d%d%d",&u,&v,&w);
      ct(u,v,w),ct(v,u,w);
    }
    dfs1(1);dfs2(1,1);build(1,1,n);
    while(1) {
      scanf("%s",s);
      if(s[0]=='D') break;
      if(s[0]=='C') {
        int x,y;
        scanf("%d%d",&x,&y);
        x=d[e[x*2-1].v]<d[e[x<<1].v]?e[x<<1].v:e[x*2-1].v;
        modify(1,1,cnt,id[x],y);
      }
      if(s[0]=='Q') {
        int x,y;
        scanf("%d%d",&x,&y);
        printf("%d\n",query(x,y));
      }
    }
  }
  return 0;
}
												
											SP375 QTREE - Query on a tree的更多相关文章
- SP375 QTREE - Query on a tree (树剖)
		
题目 SP375 QTREE - Query on a tree 解析 也就是个蓝题,因为比较长 树剖裸题(基本上),单点修改,链上查询. 顺便来说一下链上操作时如何将边上的操作转化为点上的操作: 可 ...
 - QTREE - Query on a tree
		
QTREE - Query on a tree 题目链接:http://www.spoj.com/problems/QTREE/ 参考博客:http://blog.sina.com.cn/s/blog ...
 - SPOJ QTREE Query on a tree  树链剖分+线段树
		
题目链接:http://www.spoj.com/problems/QTREE/en/ QTREE - Query on a tree #tree You are given a tree (an a ...
 - SPOJ  VJudge QTREE - Query on a tree
		
Query on a tree Time Limit: 851MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Submi ...
 - 题解 SP375 【QTREE - Query on a tree】
		
\[ \texttt{Preface} \] 这题在 \(\text{Luogu}\) 上竟然不能交 \(C++\) ,会一直 \(Waiting\) ,只能交非 \(C++\) 的语言. 所以打完了 ...
 - spoj QTREE - Query on a tree(树链剖分+线段树单点更新,区间查询)
		
传送门:Problem QTREE https://www.cnblogs.com/violet-acmer/p/9711441.html 题解: 树链剖分的模板题,看代码比看文字解析理解来的快~~~ ...
 - SPOJ QTREE Query on a tree --树链剖分
		
题意:给一棵树,每次更新某条边或者查询u->v路径上的边权最大值. 解法:做过上一题,这题就没太大问题了,以终点的标号作为边的标号,因为dfs只能给点分配位置,而一棵树每条树边的终点只有一个. ...
 - SPOJ375 QTREE - Query on a tree
		
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
 - SPOJ QTREE Query on a tree VI
		
You are given a tree (an acyclic undirected connected graph) with n nodes. The tree nodes are number ...
 
随机推荐
- codeforces 86D D. Powerful array(莫队算法)
			
题目链接: D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input stan ...
 - Xposed模块开发学习记录
			
Xposed模块相关API可以参考在线文档: https://api.xposed.info/reference/packages.html 入门教程可以参考: https://github. ...
 - properties使用
			
properties可以load store 注释可以采用 "#" 或者"!" 分隔采用"="或者":" 分行采用&qu ...
 - 服务端返回可执行js格式要求
			
服务端返回的数据,如果有直接执行的代码,那返回的头部格式中的"Content-Type",不能为"text/plain",不然,浏览器是不会执行返回数据的. 返 ...
 - Boilerplate
			
HTML5 Boilerplate 是一个由 Paul Irish(Google Chrome 开发人员.jQuery 项目成员.Modernizr 作者.yayQuery 播客主持人)主导的“前端开 ...
 - MYSQL主从复制配置遇到的问题
			
在进行配置从服务器时遇到的错误. mysql> change master to master_host='192.168.136.129',master_user='repl',master_ ...
 - TreeView控件实践
			
TreeView控件可以通过HierarchicalDataTemplate 和 DataTemplate来自定义. 1)HierarchicalDataTemplate用来支持HeaderedIte ...
 - Ubuntu中Could not get lock /var/lib/dpkg/lock
			
找出所有的 apt 以及 apt-get 进程: ps -A | grep apt-get 杀死进程: processnumbe 删除锁定文件: rm /var/lib/dpkg/loc 之后像下面这 ...
 - Java常见设计模式之适配器模式
			
在阎宏博士的<JAVA与模式>一书中开头是这样描述适配器(Adapter)模式的: 适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能 ...
 - Centos环境docker的正确安装及疑难杂症
			
根据官方文档:https://docs.docker.com/install/linux/docker-ce/centos/搭建docker 1.卸载docker旧版本: sudo yum remov ...