本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

Description

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 正解:树链剖分
解题报告:
  链剖裸题,注意清空数组。
//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <complex>
using namespace std;
#define lc root<<1
#define rc root<<1|1
typedef long long LL;
const int MAXN = 20011;
const int MAXM = 40011;
const int inf = (1<<30);
int n,ecnt,first[MAXN],to[MAXM],next[MAXM],w[MAXM],father[MAXN],quan[MAXN],quanv[MAXN];
int deep[MAXN],id[MAXN],pre[MAXN],son[MAXN],size[MAXN],top[MAXN],match[MAXN],ans,ql,qr,CC;
char ch[12];
struct node{ int maxl; }a[MAXN*3];
inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
} inline void dfs(int x,int fa){
size[x]=1;
for(int i=first[x];i;i=next[i]) {
int v=to[i]; if(v==fa) continue;
father[v]=x; deep[v]=deep[x]+1; quanv[v]=(i+1)>>1;
quan[v]=w[i]; match[(i+1)>>1]=v;
dfs(v,x); size[x]+=size[v];
if(size[v]>=size[son[x]]) son[x]=v;
}
} inline void dfs2(int x,int fa){
id[x]=++ecnt; pre[ecnt]=x;
if(son[x]) top[son[x]]=top[x],dfs2(son[x],x);
for(int i=first[x];i;i=next[i]) {
int v=to[i]; if(v==fa || v==son[x]) continue;
top[v]=v; dfs2(v,x);
}
} inline void build(int root,int l,int r){
if(l==r) { a[root].maxl=quan[pre[l]]; return ; }
int mid=(l+r)>>1; build(lc,l,mid); build(rc,mid+1,r);
a[root].maxl=max(a[lc].maxl,a[rc].maxl);
} inline void query(int root,int l,int r){
if(ql<=l && r<=qr) { ans=max(ans,a[root].maxl); return ; }
int mid=(l+r)>>1; if(ql<=mid) query(lc,l,mid); if(qr>mid) query(rc,mid+1,r);
} inline void lca(int x,int y){
ans=-inf; int f1=top[x],f2=top[y];
while(f1!=f2) {
if(deep[f1]<deep[f2]) swap(f1,f2),swap(x,y);
ql=id[f1]; qr=id[x]; query(1,1,n);
x=father[f1]; f1=top[x];
}
if(deep[x]<deep[y]) swap(x,y);
ql=id[son[y]]; qr=id[x];
if(ql<=qr) query(1,1,n);
printf("%d\n",ans);
} inline void update(int root,int l,int r){
if(l==r) { a[root].maxl=CC; return ; }
int mid=(l+r)>>1;
if(ql<=mid) update(lc,l,mid); else update(rc,mid+1,r);
a[root].maxl=max(a[lc].maxl,a[rc].maxl);
} inline void work(){
int T=getint(); int x,y,z;
while(T--) {
n=getint(); ecnt=0; memset(first,0,sizeof(first));
memset(son,0,sizeof(son));
for(int i=1;i<n;i++) {
x=getint(); y=getint(); z=getint();
next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y; w[ecnt]=z;
next[++ecnt]=first[y]; first[y]=ecnt; to[ecnt]=x; w[ecnt]=z;
}
deep[1]=1; dfs(1,0);
ecnt=0; top[1]=1; dfs2(1,0);
build(1,1,n);
while(1) {
scanf("%s",ch); if(ch[0]=='D') break;
if(ch[0]=='Q') {
x=getint(); y=getint();
lca(x,y);
}
else {
x=getint(); y=getint(); CC=y;
z=match[x];//边对应的连接的儿子节点
quan[z]=y; ql=id[z]; update(1,1,n);
}
}
}
} int main()
{
work();
return 0;
}

  

SPOJ375 QTREE - Query on a tree的更多相关文章

  1. [SPOJ375]QTREE - Query on a tree【树链剖分】

    题目描述 给你一棵树,两种操作. 修改边权,查找边权的最大值. 分析 我们都知道,树链剖分能够维护点权. 而且每一条边只有一个,且唯一对应一个儿子节点,那么就把信息放到这个儿子节点上. 注意,lca的 ...

  2. QTREE - Query on a tree

    QTREE - Query on a tree 题目链接:http://www.spoj.com/problems/QTREE/ 参考博客:http://blog.sina.com.cn/s/blog ...

  3. 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 ...

  4. SP375 QTREE - Query on a tree (树剖)

    题目 SP375 QTREE - Query on a tree 解析 也就是个蓝题,因为比较长 树剖裸题(基本上),单点修改,链上查询. 顺便来说一下链上操作时如何将边上的操作转化为点上的操作: 可 ...

  5. SPOJ VJudge QTREE - Query on a tree

    Query on a tree Time Limit: 851MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Submi ...

  6. spoj QTREE - Query on a tree(树链剖分+线段树单点更新,区间查询)

    传送门:Problem QTREE https://www.cnblogs.com/violet-acmer/p/9711441.html 题解: 树链剖分的模板题,看代码比看文字解析理解来的快~~~ ...

  7. SPOJ QTREE Query on a tree --树链剖分

    题意:给一棵树,每次更新某条边或者查询u->v路径上的边权最大值. 解法:做过上一题,这题就没太大问题了,以终点的标号作为边的标号,因为dfs只能给点分配位置,而一棵树每条树边的终点只有一个. ...

  8. SP375 QTREE - Query on a tree

    题意大意 给定\(n\)个点的树,边按输入顺序编号为\(1,2,...n-1\),要求作以下操作: CHANGE \(i\) \(t_i\) 将第\(i\)条边权值改为\(t_i\),QUERY \( ...

  9. 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 ...

随机推荐

  1. Uva12663

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110383#problem/C 题目大意:有一些不同高度的桥,会涨几次水,水流初 ...

  2. 【BZOJ4724】[POI2017]Podzielno 数学+二分

    [BZOJ4724][POI2017]Podzielno Description B进制数,每个数字i(i=0,1,...,B-1)有a[i]个.你要用这些数字组成一个最大的B进制数X(不能有前导零, ...

  3. Java实现单例模式的两种方式

    单例模式在实际开发中有很多的用途,比如我们在项目中常用的工具类,数据库等资源的连接类.这样做的好处是避免创建多个对象,占用内存资源,自始自终在内存中只有一个对象为我们服务. 单例对象一般有两种实现方式 ...

  4. [NOIP2018TG]旅行

    [NOIP2018TG]旅行 树很简单,对每个点sort儿子,贪心走就行了 基环树呢? 如果是1e5可能不太好做 但是5000的话枚举断边就可以\(n^2\)了 #include<bits/st ...

  5. PAT 1068. 万绿丛中一点红(20)

    对于计算机而言,颜色不过是像素点对应的一个24位的数值.现给定一幅分辨率为MxN的画,要求你找出万绿丛中的一点红,即有独一无二颜色的那个像素点,并且该点的颜色与其周围8个相邻像素的颜色差充分大. 输入 ...

  6. Hexo+yilia博客添加背景音乐

    个人主页:https://www.yuehan.online 现在博客:www.wangyurui.top 第一步: 打开网易云音乐的官网:https://music.163.com/ 第二步: 搜索 ...

  7. 0402-服务注册与发现-Eureka Server使用、将服务注册到Eureka server上

    一.Eureka Server使用 官方文档地址:http://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud. ...

  8. .NET Framework 3.5-8 下载地址

    https://dotnet.microsoft.com/download/dotnet-framework Version Released End of life .NET Framework 4 ...

  9. Oracle学习笔记—数据库,实例,表空间,用户、表之间的关系

    之前一直使用的关系型数据库是Mysql,而新公司使用Oracle,所以最近从网上搜集了一些资料,整理到这里,如果有不对的地方,欢迎大家讨论. 基本概念: 数据库:Oracle 数据库是数据的物理存储. ...

  10. easyPieChart 使用小记

    在使用的时候本来想在获取数据的时候,再放入percent值,但死活不出来进度条条了,只能无奈设置默认100.求教有木正确方式? $("#demo-pie-1").attr(&quo ...