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 ...
随机推荐
- 关于sql中constraint 前缀的用意(PK、UK、DF、CK、FK)
--主键constraint PK_字段 primary key(字段), --唯一约束constraint UK_字段 unique key(字段), --默认约束constrint DF_字段 d ...
- Linq连接数据库时,sql中的列为自增长,如何添加数据?
1`首先在数据库中写入命令 SET IDENTITY_INSERT 表名 on 2·重新把表拖入 .dbml 中
- Jsp万能密码漏洞修复例子
更多详细内容请查看:http://www.111cn.net/jsp/Java/58610.htm 如果网站出现这种“万能密码”漏洞该怎么办呢 'or'='or' 漏洞修复 方法有很多在这里介绍两种, ...
- 配置Rip的认证
实验拓扑图 PS:我做实验使用的是DynamipsDUI模拟器,并且加载的是真实的ios镜像(c3745-advipservicesk9-mz.124-3c.bin) 1. 配置路由器R1 2. ...
- Webservice发布出现 测试窗体只能用于来自本地计算机的请求
今天发布了一个接口,一开始以为是.netframework版本的问题,从3.5降到2.0到服务器发布,发布后还是会出现 测试窗体只能用于来自本地计算机的请求 上网查找资料发现原来是 webconfig ...
- mysql之触发器before和after的区别(2)
我们先做个测试: 接上篇日志建的商品表g和订单表o和触发器 假设:假设商品表有商品1,数量是10: 我们往订单表插入一条记录: insert into o(gid,much) values(1,20) ...
- ArcGIS 坐标系统文件
坐标是GIS数据的骨骼框架,能够将我们的数据定位到相应的位置,为地图中的每一点提供准确的坐标. ArcGIS自带了多种坐标系统,在${ArcGISHome}Coordinate Systems目录下可 ...
- Windows Phone 8.1开发:如何让ListView滚动到顶部,回到第一条?
Windows Phone 8.1开发中,ListView向下滑动了半天,用户如果突然想回头看看第一条数据怎么办? 如何让listView滚动到顶部,回到第一条? 很简单,一行代码.调用ListVie ...
- 随便写了一个DAO
package com.java; public class ExamStudent { /** * 流水号 */ private int flowId; /** * 四级.六级 */ private ...
- 阿里云服务器 ECS Ubuntu系统安装配置
1. 登陆服务器 系统开通成功后手机会收到阿里云发来的短信,包含公网IP及root登录密码. WEB管理后台方式 可通过阿里云管理后台选择“连接管理终端…”进行登录 提示输入VNC密码 登录成功后显示 ...