SPOJ375Query on a tree I(树剖+线段树)(询问边)
ι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
题意:
对于一棵树,有如下两种操作;
- 询问u,v两点间的最短路上权值最大的边。
- 更改某条边的权值。
思路:
树剖基础题,大家都会,而我刚刚学,所以就不啰嗦了。
如果是更新点,查询点,可能好写点。而这里是对边进行处理,写半天把自己搅混了。。。
代码里是这样处理的:
- 对于每条边的两个顶点u,v,距离root远的是u,不然交换即可。假设这条边是i,用tid[u]表示这条边在线段树中对应的位置。即tid x表示x和父亲这条边在线段树中的位置。用时间戳tim++保证了root一下的x才有对应的tid;
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstring>
const int maxn=;
using namespace std;
int e[maxn][];
int dpt[maxn],son[maxn],fa[maxn];
int Laxt[maxn],Next[maxn],To[maxn],cnt;
int sz[maxn],tid[maxn],top[maxn],tim;//树剖
int Max[maxn],Rank[maxn],n;//线段树
//tid[v]表示v与其父亲节点的连边在线段树中的位置 即time'id
struct TC
{
int lowbit(int x) { return x&(-x);}
void init()
{
cnt=; tim=;
memset(Laxt,,sizeof(Laxt));
memset(Max,,sizeof(Max));
memset(son,,sizeof(son));
}
void add_edge(int u,int v)
{
Next[++cnt]=Laxt[u];
Laxt[u]=cnt; To[cnt]=v;
}
int dfs1(int u,int pre)//得到dpt,fa,son,sz
{
fa[u]=pre;dpt[u]=dpt[pre]+;sz[u]=;
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i];
if(v!=pre){
dfs1(v,u); sz[u]+=sz[v];
if(!son[u]||sz[v]>sz[son[u]]) son[u]=v;
}
}
}
void dfs2(int u,int Top)//得到边在线段树中的位置。
{
top[u]=Top; tid[u]=tim++; Rank[tid[u]]=u;//是tim++,而不是++tim,从而把root排除,弱保存点是后者
if(!son[u]) return ;
dfs2(son[u],Top);
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i];
if(v!=fa[u]&&v!=son[u]) {
dfs2(v,v);//这里不再是Top了,换链了。
}
}
}
int update(int Now,int L,int R,int pos,int num)
{
if(L==pos&&R==pos) return Max[Now]=num;
int Mid=(L+R)>>;//下面注意不要丢掉return,WA了两发。不然多写个push_up也可以。
if(pos<=Mid) return Max[Now]=max(update(Now<<,L,Mid,pos,num),Max[Now<<|]);
else return Max[Now]=max(update(Now<<|,Mid+,R,pos,num),Max[Now<<]);
}
void Make_tree()
{
int u,v;
for(int i=;i<n;i++){
scanf("%d%d%d",&e[i][],&e[i][],&e[i][]);
add_edge(e[i][],e[i][]);
add_edge(e[i][],e[i][]);
}
dfs1(,);
dfs2(,);
for(int i=;i<n; i++){//单点更新建树
if(dpt[e[i][]]>dpt[e[i][]]) swap(e[i][],e[i][]);
update(,,n-,tid[e[i][]],e[i][]);
}
}
int get_max(int Now,int L,int R,int l,int r)
{
if(L>=l&&R<=r) return Max[Now];
int Mid=(L+R)>>;
if(Mid>=r) return get_max(Now<<,L,Mid,l,r);
else if(Mid+<=l) return get_max(Now<<|,Mid+,R,l,r);
else return max(get_max(Now<<,L,Mid,l,r),get_max(Now<<|,Mid+,R,l,r));
}
void Query()
{
int u,v; scanf("%d%d",&u,&v);
int f1=top[u],f2=top[v];
int ans=;
while(f1 != f2){
if(dpt[f1] < dpt[f2]){
swap(f1,f2); swap(u,v);
}
ans=max(ans,get_max(,,n-,tid[f1],tid[u]));
u=fa[f1]; f1=top[u];
}
if(u!=v){
if(dpt[u]>dpt[v]) swap(u,v);
ans=max(ans,get_max(,,n-,tid[son[u]],tid[v]));
}
printf("%d\n",ans);
}
void Change()
{
int u,w; scanf("%d%d",&u,&w);
update(,,n-,tid[e[u][]],w);
}
}Tc;
int main()
{
int T; char c[];
scanf("%d",&T);
while(T--){
Tc.init();
scanf("%d",&n);
Tc.Make_tree();
while(~scanf("%s",&c)){
if(c[]=='D') break;
else if(c[]=='Q') Tc.Query();
else Tc.Change();
}
} return ;
}
SPOJ375Query on a tree I(树剖+线段树)(询问边)的更多相关文章
- POJ3237 Tree(树剖+线段树+lazy标记)
You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edges are numbe ...
- BZOJ_2238_Mst_树剖+线段树
BZOJ_2238_Mst_树剖+线段树 Description 给出一个N个点M条边的无向带权图,以及Q个询问,每次询问在图中删掉一条边后图的最小生成树.(各询问间独立,每次询问不对之后的询问产生影 ...
- BZOJ_4551_[Tjoi2016&Heoi2016]树_树剖+线段树
BZOJ_4551_[Tjoi2016&Heoi2016]树_树剖+线段树 Description 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为 ...
- BZOJ_2157_旅游_树剖+线段树
BZOJ_2157_旅游_树剖+线段树 Description Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但 ...
- 【BZOJ5210】最大连通子块和 树剖线段树+动态DP
[BZOJ5210]最大连通子块和 Description 给出一棵n个点.以1为根的有根树,点有点权.要求支持如下两种操作: M x y:将点x的点权改为y: Q x:求以x为根的子树的最大连通子块 ...
- [LNOI2014]LCA(树剖+线段树)
\(\%\%\% Fading\) 此题是他第一道黑题(我的第一道黑题是蒲公英) 一直不敢开,后来发现是差分一下,将询问离线,树剖+线段树维护即可 \(Code\ Below:\) #include ...
- [CF1007D]Ants[2-SAT+树剖+线段树优化建图]
题意 我们用路径 \((u, v)\) 表示一棵树上从结点 \(u\) 到结点 \(v\) 的最短路径. 给定一棵由 \(n\) 个结点构成的树.你需要用 \(m\) 种不同的颜色为这棵树的树边染色, ...
- LOJ#3088. 「GXOI / GZOI2019」旧词(树剖+线段树)
题面 传送门 题解 先考虑\(k=1\)的情况,我们可以离线处理,从小到大对于每一个\(i\),令\(1\)到\(i\)的路径上每个节点权值增加\(1\),然后对于所有\(x=i\)的询问查一下\(y ...
- BZOJ3531-[Sdoi2014]旅行(树剖+线段树动态开点)
传送门 完了今天才知道原来线段树的动态开点和主席树是不一样的啊 我们先考虑没有宗教信仰的限制,那么就是一个很明显的树剖+线段树,路径查询最大值以及路径和 然后有了宗教信仰的限制该怎么做呢? 先考虑暴力 ...
- 【bzoj4699】树上的最短路(树剖+线段树优化建图)
题意 给你一棵 $n$ 个点 $n-1$ 条边的树,每条边有一个通过时间.此外有 $m$ 个传送条件 $(x_1,y_1,x_2,y_2,c)$,表示从 $x_1$ 到 $x_2$ 的简单路径上的点可 ...
随机推荐
- PHPstudy如何在本地搭建多站点
参考地址: http://jingyan.baidu.com/article/e52e36154227ef40c70c5147.html
- python 补充:join() , 基本数据类型的增删改查以及深浅拷贝
# join() join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串. li = ["李李嘉诚", "麻花藤", "黄海海峰&q ...
- But what exactly do we mean by "gets closer to"?
https://rdipietro.github.io/friendly-intro-to-cross-entropy-loss/ [将输入转化为输出:概率分布] When we develop a ...
- HTML/CSS/JS初始化
CSS <link type="text/css" href="http://www.mazey.cn/css/mazey-base.css" rel=& ...
- python cookbook第三版学习笔记十八:可由用户修改的装饰器
定义一个属性可由用户修改的装饰器: 在前面的介绍中使用装饰器来包装函数,这一章来介绍下如何让用户调整装饰器的属性. 首先来看下代码: from functools import wraps,parti ...
- perl智能匹配
1.perl中~~为智能匹配,它能够智能地依据符号两側的操作数来确定操作. 如要推断某个元素是否存在于数组中,不使用智能匹配,程序像这样: my $x=2; my @array=(1,2,3); my ...
- activiti基础--2----------------------(流程定义)
Deployment 部署对象 1.一次部署的多个文件信息,对于不需要的流程可以删除和修改 2.对应的表 act_re_deployment #部署对象表 act_re_procdef #流程定义表 ...
- linux -unrar解压缩
解压缩命令unrar的使用: $unrar --help 用法: unrar <command>-<switch 1> -<switchN> <arch ...
- C#调用大漠插件
大漠插件是一个很不错的东西,在按键精灵和易语言里面用得很多,可以后台找图找字,写游戏自动脚本用得特别多.前面写一个微信的自动脚本,查了一些资料,易语言不太熟悉,按键精灵功能上可能不好实现,就找了些资料 ...
- 【leetcode刷题笔记】Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...