spoj 375 Query on a tree(树链剖分,线段树)
| Time Limit: 851MS | Memory Limit: 1572864KB | 64bit IO Format: %lld & %llu |
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(树链剖分,线段树)的更多相关文章
- 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, ...
- 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 ...
- 【POJ3237】Tree(树链剖分+线段树)
Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...
- POJ3237 Tree 树链剖分 线段树
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ3237 题意概括 Description 给你由N个结点组成的树.树的节点被编号为1到N,边被编号为1 ...
- 【CF725G】Messages on a Tree 树链剖分+线段树
[CF725G]Messages on a Tree 题意:给你一棵n+1个节点的树,0号节点是树根,在编号为1到n的节点上各有一只跳蚤,0号节点是跳蚤国王.现在一些跳蚤要给跳蚤国王发信息.具体的信息 ...
- Water Tree CodeForces 343D 树链剖分+线段树
Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...
- 【BZOJ-2325】道馆之战 树链剖分 + 线段树
2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 1153 Solved: 421[Submit][Statu ...
- POJ3237 (树链剖分+线段树)
Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...
- BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 )
BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 ) 题意分析 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 ...
- Aragorn's Story 树链剖分+线段树 && 树链剖分+树状数组
Aragorn's Story 来源:http://www.fjutacm.com/Problem.jsp?pid=2710来源:http://acm.hdu.edu.cn/showproblem.p ...
随机推荐
- 【转载】分析商品日均销量(DMS)对促销商品选择的意义
江苏省常州市信特超市有限公司副总经理高晓颖 随着中国零售业的进一步的开放,竞争日趋激烈,促销活动在日常经营中已经成为不可缺少的一部分,频繁的促销活动的开展,零售业经营管理者越来越觉得促销商品的选择难度 ...
- apache windowns 下wamp配置多站点的问题
1.多站点配置找到apache下面的 conf/httpd.conf # Virtual hostsInclude conf/extra/httpd-vhosts.conf //将这句前面的#号注释 ...
- linux中重置服务器的mysql用户密码
本文章前提条件是自己经把mysql登录密码给忘记了,这个时间我们解决方法有很多,重新安装mysql数据库一切重来,另一种是通过下面文章重新设置root密码,下面我们一起来看看方法二吧. 最 近 ...
- ios开发:Core Data概述
Core Data 概述 2005年的四月份,Apple 发布了 OS X 10.4,在这个版本中 Core Data 框架发布了.Core Data本身既不是数据库也不是数据库访问框架.相反,Cor ...
- 用NOPI将图片二进制流导出到Excel
这儿采取的是将图片的二进制流导出到Excel,直接上代码: /// <summary> /// DataTable导出到Excel的MemoryStream /// </summar ...
- javascript笔记——label包含的自定义按钮选中
自定义按钮ui样式就是需要有label包含input以及带另外的标签作为新ui的载体,此时触发label的click的时候也会选中按钮,也就是说存在事件捕获,解决这个问题有如下方式 用到了 mouse ...
- 方便实用的jQuery checkbox复选框全选功能
// 主复选框 <input type="checkbox" id="ck" name="ckAll">// 子复选框项 < ...
- callback调用测试
<html> <head> <script> var context="全局"; var testObj={ context:"初始& ...
- 工厂方法模式与IoC/DI控制反转和依赖注入
IoC——Inversion of Control 控制反转 DI——Dependency Injection 依赖注入 要想理解上面两个概念,就必须搞清楚如下的问题: 参与者都有谁? 依赖:谁 ...
- R简易入门(二)
本文内容来源:https://www.dataquest.io/mission/128/working-with-data-frames 本文摘要:简单介绍一下用R处理数据 原始数据展示(这是一份 ...