【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, ...
随机推荐
- 【BZOJ】1833 [ZJOI2010]count 数字计数
[算法]数位DP [题解] 记忆化搜索 #include<cstdio> #include<algorithm> #include<cstring> #define ...
- UIControl事件---iOS-Apple苹果官方文档翻译
本系列所有开发文档翻译链接地址: iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 UIControl事件1.UIControlEventTouchDown单点触摸按下 ...
- UIImageView与UIScrollView的关系图
UIImageView与UIScrollView的关系图 https://www.evernote.com/shard/s227/sh/0af9f23c-08e6-4be6 ...
- Katu Puzzle(POJ3678+2-SAT问题+tarjan缩点)
题目链接:http://poj.org/problem?id=3678 题目: 题意:给你a,b,c,op,op为逻辑运算符或.与.异或,使得a op b = c,让你判断这些运算符是否存在矛盾,不存 ...
- pythonTensorFlow实现yolov3训练自己的目标检测探测自定义数据集
1.数据集准备,使用label标注好自己的数据集. https://github.com/tzutalin/labelImg 打开连接直接下载数据标注工具, 2.具体的大师代码见下链接 https:/ ...
- 双内网渗透代理之reGeorg+Proxifier
由于这个工具第一次体验感觉还不错,很稳定.因此在这记录一下reGeorg+Proxifier的配置及其使用. 下载地址 :https://github.com/sensepost/reGeorg.gi ...
- 可以高度定制的代理服务器anyproxy
简介 anyproxy是一款可以高度定制的代理服务器,基于nodejs. 特征 支持https明文代理 支持低网速模拟 支持二次开发,可以用javascript控制代理的全部流程,搭建前端个性化调试环 ...
- bzoj 1179 Atm
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1179 题解: 一道比较综合的图论题 直接讲正解: 如果这个图G中存在某个强连通分量,那么这 ...
- tab切换 jQuery
$('p.guidan-load1').click(function(){ $("p.guidan-load1").removeClass("guidan-load12& ...
- golang fmt格式占位符
golang 的fmt 包实现了格式化I/O函数,类似于C的 printf 和 scanf. # 定义示例类型和变量 type Human struct { Name string } var peo ...