SPOJ375 Query on a tree(树链剖分)
题意
给出一棵树,每条边都有权值,有两种操作:
- 把第p条边的权值改为x
- 询问x,y路径上的权值最大的边
code
#include<cstdio>
#include<algorithm>
#include<cstring>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 using namespace std; const int N = ;
struct Edge {
int to,nxt,w;
}e[];
int head[N],tot,tn,n;
int deth[N],son[N],fa[N],siz[N],bel[N],pos[N];
int mx[N<<],a[N],b[N],c[N],data[N]; void init() {
tot = tn = ;
memset(head,,sizeof(head));
memset(son,,sizeof(son));
}
inline void add_edge(int u,int v,int w) {
e[++tot].to = v,e[tot].w = w,e[tot].nxt = head[u],head[u] = tot;
}
void dfs1(int u,int pa) {
siz[u] = ;
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to;
if (v==pa) continue;
fa[v] = u;
deth[v] = deth[u] + ;
dfs1(v,u);
siz[u] += siz[v];
if (son[u]== || siz[v] > siz[son[u]]) son[u] = v;
}
}
void dfs2(int u,int top) {
pos[u] = ++tn;
bel[u] = top;
if (!son[u]) return ;
dfs2(son[u],top);
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to;
if (v != fa[u] && v != son[u]) dfs2(v,v);
}
}
void pushup(int rt) {
mx[rt] = max(mx[rt<<],mx[rt<<|]);
}
void build(int l,int r,int rt) {
if (l==r) {
mx[rt] = data[l];return ;
}
int m = (l + r) >> ;
build(lson);
build(rson);
pushup(rt);
}
void update(int l,int r,int rt,int p,int x) {
if (l==r) {
mx[rt] = x;return ;
}
int m = (l + r) >> ;
if (p <= m) update(lson,p,x);
else update(rson,p,x);
pushup(rt);
}
int query(int l,int r,int rt,int L,int R) {
if (L <= l && r <= R) {
return mx[rt];
}
int m = (l + r) >> ;
int ret = -1e9;
if (L <= m) ret = max(ret,query(lson,L,R));
if (R > m) ret = max(ret,query(rson,L,R));
return ret;
}
void Change(int p,int x) {
if (deth[a[p]] > deth[b[p]]) update(,n,,pos[a[p]],x);
else update(,n,,pos[b[p]],x);
}
int Ask(int x,int y) {
int ans = -1e9;
while (bel[x] != bel[y]) {
if (deth[bel[x]] < deth[bel[y]]) swap(x,y);
ans = max(ans,query(,n,,pos[bel[x]],pos[x]));
x = fa[bel[x]];
}
if (deth[x] > deth[y]) swap(x,y);
if (x != y) ans = max(ans,query(,n,,pos[x]+,pos[y]));
return ans;
}
int main() {
char opt[];
int T;scanf("%d",&T);
while (T--) {
init();
scanf("%d",&n);
for (int i=; i<n; ++i) {
scanf("%d%d%d",&a[i],&b[i],&c[i]);
add_edge(a[i],b[i],c[i]);
add_edge(b[i],a[i],c[i]);
}
deth[] = ;
dfs1(,);
dfs2(,);
for (int i=; i<n; ++i) {
if (deth[a[i]] > deth[b[i]]) data[pos[a[i]]] = c[i]; // 一定是pos
else data[pos[b[i]]] = c[i];
}
build(,n,);
scanf("%s",opt);
while (opt[]!='D') {
int x,y;
scanf("%d%d",&x,&y);
if (opt[]=='Q') printf("%d\n",Ask(x,y));
else Change(x,y);
scanf("%s",opt);
}
}
return ;
}
SPOJ375 Query on a tree(树链剖分)的更多相关文章
- Query on a tree——树链剖分整理
树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中s ...
- SPOJ Query on a tree 树链剖分 水题
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
- spoj QTREE - Query on a tree(树链剖分+线段树单点更新,区间查询)
传送门:Problem QTREE https://www.cnblogs.com/violet-acmer/p/9711441.html 题解: 树链剖分的模板题,看代码比看文字解析理解来的快~~~ ...
- 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 ...
- spoj 375 Query on a tree (树链剖分)
Query on a tree You are given a tree (an acyclic undirected connected graph) with N nodes, and edges ...
- spoj 375 QTREE - Query on a tree 树链剖分
题目链接 给一棵树, 每条边有权值, 两种操作, 一种是将一条边的权值改变, 一种是询问u到v路径上最大的边的权值. 树链剖分模板. #include <iostream> #includ ...
- SPOJ QTREE Query on a tree ——树链剖分 线段树
[题目分析] 垃圾vjudge又挂了. 树链剖分裸题. 垃圾spoj,交了好几次,基本没改动却过了. [代码](自带常数,是别人的2倍左右) #include <cstdio> #incl ...
- SPOJ 375 Query on a tree 树链剖分模板
第一次写树剖~ #include<iostream> #include<cstring> #include<cstdio> #define L(u) u<&l ...
- SPOJ QTREE Query on a tree --树链剖分
题意:给一棵树,每次更新某条边或者查询u->v路径上的边权最大值. 解法:做过上一题,这题就没太大问题了,以终点的标号作为边的标号,因为dfs只能给点分配位置,而一棵树每条树边的终点只有一个. ...
随机推荐
- Spring的配置及jar包下载
一.相关说明 IOC: Inversion of Control,控制反转,是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度.其中最常见的方式叫做依赖注入(Dependency In ...
- 【简记】HTML + CSS 的一些要点(不定时更新)
1.td占据多行 / 列时,其挤开的 td 不写(但是包裹 td 的 tr 要写) 2. display:td 的元素中的文本默认垂直不居中(table中的td中的文本是垂直居中的) 3.th虽然定义 ...
- SQL基本语法总结
#创建数据库 DROP DATABASE IF EXISTS 数据库名; CREATE DATABASE 数据库名; #展示所有的数据库: SHOW DATABASES; #查看某个数据库的定义信息: ...
- cpu 满载测试软件
for i in `seq 1 $(cat /proc/cpuinfo |grep "physical id" |wc -l)`; do dd if=/dev/zero of=/d ...
- {Linux} boot仅剩余XX字节
1. 查看已安装的linux-image各版本 dpkg --get-selections |grep linux-image 2. 查看我们当前使用的是哪一个版本: uname -a 3. ...
- (原创)linux下Microsoft/cpprestsdk支持https(server)
原创,转载请标明源地址 之前看网上一堆的资料说Microsoft/cpprestsdk不支持https或者说只支持window下的https,差点就被误导了,没办法,只好自己去翻了下源代码 先说明下l ...
- Distributed Transaction Coordinator(DTC)一些问题的解决方法
有时运行某个程序或者安装SQL Server时报错. 错误信息: 事务管理器不可用.(从 HRESULT 异常: 0x8004D01B) 启动服务Distributed Transaction Coo ...
- Installing Apache, PHP, and MySQL on Mac OS X
I have installed Apache, PHP, and MySQL on Mac OS X since Leopard. Each time doing so by hand. Each ...
- 删除表中一个字段的SQL语句
1.删除没有默认值的列:alter table Test drop COLUMN BazaarType 2.删除有默认值的列:先删除约束(默认值)alter table Test DROP CONST ...
- UVA 12905 Volume of Revolution (几何,微积分)
题意:分段用椎台面积近似计算体积,然后计算出近似值和真实值的相对误差 微积分加数学. 平头椎台的体积计算公式: V = 1/3*h*(S1+S2*sqrt(S1*S2) 一个更快的计算多项式值的方法: ...