Time Limit: 851MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu

Submit Status

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

【思路】

树链剖分。

划分轻重链,线段树维护。

这里有个知识入门:http://blog.sina.com.cn/s/blog_6974c8b20100zc61.html

【代码】


 #include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std; const int N = +; struct Edge { int u,v,w; };
vector<int> G[N];
vector<Edge> es;
int n,z,root,d[N][];
int fa[N],siz[N],dep[N],son[N],w[N],top[N];
//fa为父节点 siz为子树大小 dep为节点深度
//son代表重儿子 w为u与fa[u]在线段树中的位置 top代表所属重链的顶端
//z为线段树大小 void adde(int u,int v,int w) {
es.push_back((Edge){u,v,w});
int m=es.size();
G[u].push_back(m-);
} void dfs(int u) { //->siz[] son[] fa[]
siz[u]=; son[u]=;
for(int i=;i<G[u].size();i++) {
int v=es[G[u][i]].v;
if(v!=fa[u]) {
fa[v]=u;
dep[v]=dep[u]+;
dfs(v);
if(siz[v]>siz[son[u]]) son[u]=v;
siz[u]+=siz[v];
}
}
}
void build_tree(int u,int tp) { //son[] -> top[] w[]
w[u]=++z; top[u]=tp;
if(son[u]) build_tree(son[u],top[u]);
for(int i=;i<G[u].size();i++) {
int v=es[G[u][i]].v;
if(v!=son[u] && v!=fa[u]) build_tree(v,v);
}
} int tree[N];
void update(int u,int L,int R,int loc,int x) {
if(loc<L || R<loc) return ;
if(L==R) { tree[u]=x; return ; }
int M=L+(R-L)/ , lc=u*,rc=lc+;
update(lc,L,M,loc,x);
update(rc,M+,R,loc,x);
tree[u]=max(tree[lc],tree[rc]);
}
int query(int u,int L,int R,int l,int r) {
if(R<l || L>r) return ;
if(l<=L && R<=r) return tree[u];
int M=L+(R-L)/;
return max(query(u*,L,M,l,r),query(u*+,M+,R,l,r));
}
int find(int u,int v) {
int f1=top[u] , f2=top[v] , ans=;
while(f1!=f2) { //直到移动到同一重链
if(dep[f1]<dep[f2])
swap(f1,f2) , swap(u,v);
ans=max(ans,query(,,z,w[f1],w[u])); //在重链上移动同时统计
u=fa[f1] , f1=top[u];
}
if(u==v) return ans;
if(dep[u]>dep[v]) swap(u,v);
return max(ans,query(,,z,w[son[u]],w[v])); //uv之间统计
} void init() {
scanf("%d",&n);
es.clear();
for(int i=;i<=n;i++) G[i].clear();
root=(n+)/;
fa[root]=dep[root]=z=;
memset(siz,,sizeof(siz));
memset(tree,,sizeof(tree));
int u,v,c;
for(int i=;i<n;i++) {
scanf("%d%d%d",&u,&v,&c);
d[i][]=u , d[i][]=v , d[i][]=c;
adde(u,v,c) , adde(v,u,c);
}
dfs(root);
build_tree(root,root);
for(int i=;i<n;i++) {
if(dep[d[i][]]>dep[d[i][]]) swap(d[i][],d[i][]);
update(,,z,w[d[i][]],d[i][]);
}
}
void solve() {
char s[];
int u,v;
while(scanf("%s",s)== && s[]!='D') {
scanf("%d%d",&u,&v);
if(s[]=='Q') printf("%d\n",find(u,v));
else update(,,z,w[d[u][]],v);
}
} int main() {
int T;
scanf("%d",&T);
while(T--) {
init();
solve();
}
return ;
}


spoj 375 Query on a tree(树链剖分,线段树)的更多相关文章

  1. Spoj Query on a tree SPOJ - QTREE(树链剖分+线段树)

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

  2. Aizu 2450 Do use segment tree 树链剖分+线段树

    Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...

  3. 【POJ3237】Tree(树链剖分+线段树)

    Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...

  4. POJ3237 Tree 树链剖分 线段树

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ3237 题意概括 Description 给你由N个结点组成的树.树的节点被编号为1到N,边被编号为1 ...

  5. 【CF725G】Messages on a Tree 树链剖分+线段树

    [CF725G]Messages on a Tree 题意:给你一棵n+1个节点的树,0号节点是树根,在编号为1到n的节点上各有一只跳蚤,0号节点是跳蚤国王.现在一些跳蚤要给跳蚤国王发信息.具体的信息 ...

  6. Water Tree CodeForces 343D 树链剖分+线段树

    Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...

  7. 【BZOJ-2325】道馆之战 树链剖分 + 线段树

    2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1153  Solved: 421[Submit][Statu ...

  8. POJ3237 (树链剖分+线段树)

    Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...

  9. BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 )

    BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 ) 题意分析 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 ...

  10. Aragorn's Story 树链剖分+线段树 && 树链剖分+树状数组

    Aragorn's Story 来源:http://www.fjutacm.com/Problem.jsp?pid=2710来源:http://acm.hdu.edu.cn/showproblem.p ...

随机推荐

  1. Windows Kernel Way 1:Windows内核调试技术

    掌握Windows内核调试技术是学习与研究Windows内核的基础,调试Windows内核的方式大致分为两种: (1)通过Windbg工具在Windows系统运行之初连接到Windows内核,连接成功 ...

  2. linux 在xenserver上安装如何显示图形界面

    centos5.8 64-bit和 centos 6.5 64-bit xenserver安装linux的时候默认使用的VHM,选择对应的虚拟机模板安装linux是Linux Text界面. VHM ...

  3. WCF学习笔记(2)——使用IIS承载WCF服务

    通过前面的笔记我们知道WCF服务是不能独立存在,必须“寄宿”于其他的应用程序中,承载WCF服务的应用程序我们称之为“宿主”.WCF的多种可选宿主,其中比较常见的就是承载于IIS服务中,在这里我们来学习 ...

  4. 【转】C#异步编程及其同步机制

    C#异步编程及其同步机制 本篇文章涵盖一下几部分内容: 1. 什么是异步编程,为什么会需要异步编程 2. .NET下的异步编程及其发展 3. .NET线程同步机制及线程间数据封送 4. 异步模式 5. ...

  5. hdu 1002大数(Java)

    A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. 【ASP.NET】获取网站目录的方法

         获取网站物理路径: HttpRuntime.AppDomainAppPath 获取网站虚拟路径: HttpRuntime.AppDomainAppVirtualPath

  7. Jquery 学习二

    一.事件编程 1.基本事件(以方法形式存在的) 基本语法: 原生Javascript代码中的事件绑定方式: DOM对象.事件 = 事件的处理程序   jQuery代码中的事件绑定方式: jQuery对 ...

  8. Template_16_模板与继承

    1,名称模板参数template <typename PolicySetter1 = DefaultPolicy1,    typename PolicySetter2 = DefaultPol ...

  9. ZigBee安全相关

    ZigBee安全由AES加密算法和CCM操作方式作为安全方案,广泛使用在ZigBee联盟的通信协议中.ZDO层负责安全策略和安全配置的管理. Technorati 标签: ZigBee 安全 2. 配 ...

  10. ARM寄存器的8种寻址方式01

    一.立即数寻址 操作数由指令本身给出 MOV r0,#0x0F //是所有寻址方式里面速度最快的,但是受到合法立即数的限制 立即数要求以#和$开头 十六进制,#后跟0x:十进制,#后直接加:八进制,# ...