【SPOJ Query on a tree 】 (树链剖分)
http://acm.hust.edu.cn/vjudge/problem/13013
题意:
有一棵N个节点的树(1<=N<=10000),N-1条边,边的编号为1~N-1,每条边有一个权值,要求模拟两种操作:
1:QUERY x y 求节点x和节点y之间的路径中权值最大的边。
2:CHANGE p k修改第p条边的权值为k。
【分析】
树链剖分裸题。。
【表示我一开始怎么TLE,后来怎么AC的并不知道。。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
#define Maxn 10010
#define INF 0x7fffffff int mymax(int x,int y) {return x>y?x:y;} struct node
{
int x,y,c,next;
}t[*Maxn];int len; int first[Maxn];
void ins(int x,int y,int c)
{
t[++len].x=x;t[len].y=y;t[len].c=c;
t[len].next=first[x];first[x]=len;
} char s[]; int son[Maxn],dfn[Maxn],top[Maxn],cnt;
int sm[Maxn],fa[Maxn],id[Maxn],dep[Maxn];
void dfs1(int x,int f)
{
dep[x]=dep[f]+;
sm[x]=;son[x]=;fa[x]=f;
for(int i=first[x];i;i=t[i].next) if(t[i].y!=f)
{
int y=t[i].y;
dfs1(y,x);
sm[x]+=sm[y];
if(son[x]==||sm[son[x]]<sm[y]) son[x]=y;
}
} void dfs2(int x,int tp)
{
dfn[x]=++cnt;top[x]=tp;
if(son[x]) dfs2(son[x],tp);
for(int i=first[x];i;i=t[i].next) if(t[i].y!=fa[x]&&t[i].y!=son[x])
dfs2(t[i].y,t[i].y);
} struct nnode
{
int l,r,lc,rc,mx;
}tr[*Maxn];int tot; int build(int l,int r)
{
int x=++tot;
tr[x].l=l;tr[x].r=r;
int mid=(l+r)>>;
if(l!=r)
{
tr[x].lc=build(l,mid);
tr[x].rc=build(mid+,r);
}
tr[x].mx=-INF;
return x;
} void change(int x,int y,int z)
{
if(tr[x].l==tr[x].r)
{
tr[x].mx=z;
return;
}
int mid=(tr[x].l+tr[x].r)>>;
if(y<=mid) change(tr[x].lc,y,z);
else change(tr[x].rc,y,z);
tr[x].mx=mymax(tr[tr[x].lc].mx,tr[tr[x].rc].mx);
} int query(int x,int l,int r)
{
if(tr[x].l==l&&tr[x].r==r) return tr[x].mx;
int mid=(tr[x].l+tr[x].r)>>;
if(r<=mid) return query(tr[x].lc,l,r);
else if(l>mid) return query(tr[x].rc,l,r);
return mymax(query(tr[x].lc,l,mid),query(tr[x].rc,mid+,r));
} int get_ans(int x,int y)
{
int mx=;
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]]) swap(x,y);
mx=mymax(mx,query(,dfn[top[x]],dfn[x]));
x=fa[top[x]];
}
if(x==y) return mx;
if(dep[x]>dep[y]) swap(x,y);
mx=mymax(mx,query(,dfn[x]+,dfn[y]));
return mx;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
len=;
memset(first,,sizeof(first));
for(int i=;i<n;i++)
{
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
ins(x,y,c);ins(y,x,c);
}
dep[]=;dfs1(,);
cnt=;dfs2(,);
tot=;build(,n);
for(int i=;i<=len;i+=)
{
if(fa[t[i].x]==t[i].y) id[(i+)/]=t[i].x;
else id[(i+)/]=t[i].y;
change(,dfn[id[(i+)/]],t[i].c);
}
while()
{
scanf("%s",s);
if(s[]=='D') break;
int x,y;
scanf("%d%d",&x,&y);
if(s[]=='C')
{
change(,dfn[id[x]],y);
}
else
{
printf("%d\n",get_ans(x,y));
}
}
}
return ;
}
2017-01-21 11:45:00
【SPOJ Query on a tree 】 (树链剖分)的更多相关文章
- 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 树链剖分+线段树
题目链接: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 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 QTREE Query on a tree ——树链剖分 线段树
[题目分析] 垃圾vjudge又挂了. 树链剖分裸题. 垃圾spoj,交了好几次,基本没改动却过了. [代码](自带常数,是别人的2倍左右) #include <cstdio> #incl ...
- spoj 375 QTREE - Query on a tree 树链剖分
题目链接 给一棵树, 每条边有权值, 两种操作, 一种是将一条边的权值改变, 一种是询问u到v路径上最大的边的权值. 树链剖分模板. #include <iostream> #includ ...
- Query on a tree——树链剖分整理
树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中s ...
- 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只能给点分配位置,而一棵树每条树边的终点只有一个. ...
- Query on a tree 树链剖分 [模板]
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
随机推荐
- [Unity]用PropertyDrawer自定义struct/class的外观
一般来说,当我们要扩展编辑器时,我们会从Editor类继承,为自己的MonoBehaviour实现不同的外观. 但是如果有一个struct/class,在许多地方被使用,Unity默认的外观又不够好看 ...
- 【洛谷 P1419】 寻找段落(二分答案,单调队列)
题目链接 开始还以为是尺取.发现行不通. 一看标签二分答案,恍然大悟. 二分一个\(mid\)(实数),把数列里每个数减去\(mid\),然后求前缀和,在用单调队列维护\(sum[i-t\text{~ ...
- 关于win7局域网共享的相关设置
模式1> 被访问方相关设置步骤: (1)被共享方的电脑开通来宾用户 (2)被共享方的电脑的本地安全策略需要设置成 "仅来宾" (3)被共享方的电脑高级共享设置中 " ...
- 将已编写的静态的网页发布到github上
最近在学习前端框架的过程中,一直想把自己学习中做的demo 发布到github 上去.但是在查看了很多相关资料也没能找到一个比较满意的结果. 无奈之下,只能尝试做用了一种自认为最low 的方式来达到部 ...
- Intel MKL(Math Kernel Library)
1.Intel MKL简介 Intel数学核心函数库(MKL)是一套高度优化.线程安全的数学例程.函数,面向高性能的工程.科学与财务应用.英特尔 MKL 的集群版本包括 ScaLAPACK 与分布式内 ...
- SpringBoot工程目录配置
Spring Boot建议的目录结果如下: root package结构:com.example.myproject com +- example +- myproject +- Applicat ...
- Python标准库笔记(4) — collections模块
这个模块提供几个非常有用的Python容器类型 1.容器 名称 功能描述 OrderedDict 保持了key插入顺序的dict namedtuple 生成可以使用名字来访问元素内容的tuple子类 ...
- 【EverydaySport】健身笔记——背部训练
背部训练大致可以分为两种. 1 下拉式动作 躯干纵向上下位移的动作 典型代表 这样的下拉类动作 针对的是背阔肌 也就是两边像翅膀一样的部分 2 垂直于躯干的方向作用 向内拉 主要针对的是,背部的中部 ...
- Qualcomm platform, the commonly used parameters of charger and battery in device tree file
Platform MSM8917 PM8937 PMI8940 Parameters 1 battery charging voltage : qcom,float-voltage-mv = < ...
- 看jquery3.3.1学js类型判断的技巧
需要预习:call , typeof, js数据类型 1. isFunction中typeof的不靠谱 源码: var isFunction = function isFunction( obj ) ...