SPOJ QTREE-Query on a tree-树链剖分-边权
用每个点代表父节点到此点的边。建立一一映射后就可以用点权的方法处理了。
注意的是路径两端节点的处理
#include <cstdio>
#include <algorithm>
#include <vector> using namespace std; const int maxn = 1e5+;
int val[maxn],dep[maxn],siz[maxn],top[maxn],id[maxn],son[maxn],fa[maxn];
int topw,M; vector<int> G[maxn];
struct Edge{
int x,y,val;
}e[maxn]; void dfs_1(int u,int f,int d)
{
fa[u] = f;
son[u] = ;
siz[u] = ;
dep[u] = d; for(int i=;i<G[u].size();i++) if(G[u][i] != f )
{
dfs_1(G[u][i],u,d+);
siz[u] += siz[G[u][i]];
if(siz[son[u] ] < siz[G[u][i] ])
{
son[u] = G[u][i];
}
}
} void dfs_2(int u,int tp)
{
top[u] = tp;
id[u] = ++topw;
if(son[u]) dfs_2(son[u],tp);
for(int i=;i<G[u].size();i++) if(G[u][i] != fa[u] && G[u][i] != son[u])
{
dfs_2(G[u][i],G[u][i]);
}
} int N,T; void debug()
{
for(int i=;i<=N;i++)
{
printf("%d siz:%d son:%d dep:%d fa:%d ",i,siz[i],son[i],dep[i],fa[i]);
printf("top:%d id:%d\n",top[i],id[i]);
}
} /*-------------------------------------*/
//segment Tree
#define lson(x) (x<<1)
#define rson(x) (x<<1|1) struct SegmentTree{
int l,r,val;
}sgtree[*maxn]; void pushup(int x)
{
sgtree[x].val = max(sgtree[lson(x)].val,sgtree[rson(x)].val);
} void Build(int l,int r,int x)
{
sgtree[x].l = l;
sgtree[x].r = r;
if(l==r)
{
sgtree[x].val = val[l];
return ;
}
int mid = (l+r)>>;
Build(l,mid,lson(x));
Build(mid+,r,rson(x));
pushup(x);
} void update(int x,int v,int add)
{
if(sgtree[x].l == sgtree[x].r)
{
sgtree[x].val = add;
//printf("change:%d %d\n",v,sgtree[x].l);
return ;
}
int mid = (sgtree[x].l+sgtree[x].r)>>;
if(v <= mid) update(lson(x),v,add);
else update(rson(x),v,add);
pushup(x);
} int query(int x,int l,int r)
{
if(sgtree[x].l >= l && sgtree[x].r <= r)
{
return sgtree[x].val;
}
int mid = (sgtree[x].l+sgtree[x].r)>>;
int ans = ;
if(l <= mid) ans = max(ans,query(lson(x),l,r));
if(r > mid) ans = max(ans,query(rson(x),l,r));
return ans;
} int Find(int u,int v)
{
int ans = ,fu = top[u],fv = top[v];
while(fu != fv)
{
if(dep[fu] < dep[fv])
{
swap(fu,fv);swap(u,v);
}
ans = max(ans,query(,id[fu],id[u]));
u = fa[fu];
fu = top[u];
}
if(u == v) return ans;
if(dep[u]>dep[v]) swap(u,v);
return max(ans,query(,id[son[u] ],id[v]));
} int main()
{
//freopen("input.in","r",stdin);
scanf("%d",&T);
while(T--)
{
scanf("%d",&N);
for(int i=,a,b,c;i<N;i++)
{
scanf("%d%d%d",&a,&b,&c);
e[i].x = a;
e[i].y = b;
e[i].val = c;
G[a].push_back(b);
G[b].push_back(a);
}
topw = ;
dfs_1(,,);
dfs_2(,);
//debug(); for(int i=;i<N;i++)
{
if(dep[e[i].x] < dep[e[i].y]) swap(e[i].x,e[i].y);
val[id[e[i].x]] = e[i].val;
} Build(,topw,);
char op[];
while(scanf("%s",op) && op[] != 'D')
{
int a,b;
scanf("%d%d",&a,&b);
if(op[] == 'Q')
printf("%d\n",Find(a,b));
else if(op[] == 'C')
update(,id[e[a].x],b);
} for(int i=;i<=N;i++) G[i].clear();
}
}
SPOJ QTREE-Query on a tree-树链剖分-边权的更多相关文章
- 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 QTREE - Query on a tree(树链剖分+线段树单点更新,区间查询)
传送门:Problem QTREE https://www.cnblogs.com/violet-acmer/p/9711441.html 题解: 树链剖分的模板题,看代码比看文字解析理解来的快~~~ ...
- SPOJ QTREE Query on a tree ——树链剖分 线段树
[题目分析] 垃圾vjudge又挂了. 树链剖分裸题. 垃圾spoj,交了好几次,基本没改动却过了. [代码](自带常数,是别人的2倍左右) #include <cstdio> #incl ...
- SPOJ QTREE Query on a tree --树链剖分
题意:给一棵树,每次更新某条边或者查询u->v路径上的边权最大值. 解法:做过上一题,这题就没太大问题了,以终点的标号作为边的标号,因为dfs只能给点分配位置,而一棵树每条树边的终点只有一个. ...
- spoj 375 QTREE - Query on a tree 树链剖分
题目链接 给一棵树, 每条边有权值, 两种操作, 一种是将一条边的权值改变, 一种是询问u到v路径上最大的边的权值. 树链剖分模板. #include <iostream> #includ ...
- 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 Query on a tree 树链剖分模板
第一次写树剖~ #include<iostream> #include<cstring> #include<cstdio> #define L(u) u<&l ...
- POJ3237 Tree 树链剖分 边权
POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...
- SPOJ Query on a tree 树链剖分 水题
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
- Query on a tree——树链剖分整理
树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中s ...
随机推荐
- SpringBoot整合篇
目录 SpringBoot整合篇 SpringBoot简介 SpringBoot运行 SpringBoot目录结构 整合JdbcTemplate @RestController 整合JSP 整合JPA ...
- 为什么大公司一定要使用DevOps?
0 DevOps的意图 究竟什么是DevOps? 要想回答这个问题,首先要明确DevOps这个过程参与的人员是谁?即开发团队和IT运维团队!那么,DevOps的意图是什么呢?即在两个团队之间,建立良好 ...
- c++构造函数成员初始化中赋值和初始化列表两种方式的区别
先总结下: 由于类成员初始化总在构造函数执行之前 1)从必要性: a. 成员是类或结构,且构造函数带参数:成员初始化时无法调用缺省(无参)构造函数 b. 成员是常量或引用:成员无法赋值,只能被初始化 ...
- Python-random模块-59
random模块: 随机数模块 >>> import random #随机小数 >>> random.random() # 大于0且小于1之间的小数 0.76643 ...
- H5 16-并集选择器
16-并集选择器 我是标题 我是段落 我是段落 我是段落 <!DOCTYPE html> <html lang="en"> <head> < ...
- Day13 Python基础之time/datetime/random模块一(十一)
time模块 import time print(help(time)) time.time() #return current time in seconds since the Epoch as ...
- siteServer创建网站中Mysql和SqlServer的区别
mysql中使用本地数据库时使用:localhost sqlserver使用本地数据库时使用:(local)
- echarts使用笔记二:柱子堆叠
1.多个柱子堆叠效果,多用于各部分占比 app.title = '坐标轴刻度与标签对齐'; option = { title : { //标题 x : 'center', y : 5, text : ...
- centos yum install oracle java
How to install Java on CentOS 7 | Linuxizehttps://linuxize.com/post/install-java-on-centos-7/ CentOS ...
- laravel中migration 数据迁移
简介 数据库迁移就像是数据库的版本控制,可以让你的团队轻松修改并共享应用程序的数据库结构.迁移通常与 Laravel 的数据库结构生成器配合使用,让你轻松地构建数据库结构.如果你曾经试过让同事手动在数 ...