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. openquery链表删除时报错 “数据提供程序或其他服务返回 E_FAIL 状态”

    DELETE OPENQUERY (VERYEAST_COMPANY_MYSQL_CONN, 'SELECT * FROM company ') WHERE c_userid in(select c_ ...

  2. ebay的api的开发技术笔记

    使用eBay API基本步骤介绍 要开始使用eBay API,需要如下基本步骤: 1.    注册开发帐号: https://developer.ebay.com/join/Default.aspx ...

  3. 表情键盘及文字表情识别简单demo

    1.前言 了解了简单图文混排 (属性字符串的使用)及 正则表达式的部分知识,为了加深印象,写了个简单表情键盘demo 展示文字用的是 UITextView 由于时间匆忙,存在一些bug,以及不完善的地 ...

  4. BeanDefinition的载入和解析

    1.在完成对代表BeanDefinition的Resource定位的分析后,下面来了解整个BeanDefinition信息的载入过程. 2.对IoC容器来说,这个载入过程,相当于把定义的BeanDef ...

  5. Word 录制宏解决粘贴网络上文字格式错乱

        本文将利用Word中的录制宏来解决 复制粘贴网络上文字格式错乱的问题.     本文宏代码取自 : 知乎 李文超,感谢他的提供. Technorati 标签: Word宏 格式修正     1 ...

  6. UILabel常用属性

    [super viewDidLoad]; // 实例化UILabel并指定其边框 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0 ...

  7. windows phone 8 开发系列(一)环境搭建

    一:前奏说明 本人一名普通的neter,对新玩意有点小兴趣,之前wp7出来的时候,折腾学习过点wp7开发,后来也没怎么用到(主要对微软抛弃wp7的行为比较不爽),现在wp8已经出来一段时间了,市场上也 ...

  8. linux命令之grep用法介绍

    Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...

  9. 【风马一族_Android】 图能

    ---------------------------------- 第一次 名称:相片查看器 ---------------------------------- 通过3D.自动播放幻灯片.旋转.跳 ...

  10. VIM中的寄存器简介

    原来在vim粘贴从其他地方复制过来的文本的时候,一直用的shift + inert,那时就想,能不能够直接就像p那样粘贴,不必非得进入插入模式再来粘贴.后来看了<vim实用技巧>上关于vi ...