dfs序+线段树,啥?如果在一棵树上,需要你修改一些节点和查询一些节点,如果直接dfs搜的话肯定超时,那用线段树?树结构不是区间啊,怎么用?用dfs序将树结构转化为一个区间,就能用线段树进行维护了。

dfs序是指:每个节点在dfs深度优先遍历中的进出栈的时间序列,记录每个点进栈和出栈的时间点,会发现每个点在栈中出现两次

比如下面这个图的dfs序:

(转载来的图片,太懒不想画)

那么这样转化后我们就可以在上面进行线段树了。对于进栈时间点,我们记录为left[ u ],出栈时间点为right[ u ] 。对于这个区间,我们将每个节点标记为 1~len(dfs序长度)

以这个为区间1~len建线段树,然后那棵树就没用了!,没用了!对于修改一个节点x,就是修改left[x](但你的len是等于n的,或者你如果建的是两个节点都算的,你需要update左右编号),对于查询一个区间,就是查询left[x]~right[x],实际上就是线段树。

但是我在刚理解时候总会受原来那颗树的影响,实际上,可以这样理解,比如在这个dfs序中你要修改树节点1(原本的值都是1)

------------------------------>

附上一个例题

POJ 3321

There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree.

The tree has N forks which are connected by branches. Kaka numbers the forks by 1 to N and the root is always numbered by 1. Apples will grow on the forks and two apple won't grow on the same fork. kaka wants to know how many apples are there in a sub-tree, for his study of the produce ability of the apple tree.

The trouble is that a new apple may grow on an empty fork some time and kaka may pick an apple from the tree for his dessert. Can you help kaka?

Input

The first line contains an integer N (N ≤ 100,000) , which is the number of the forks in the tree.
The following N - 1 lines each contain two integers u and v, which means fork u and fork v are connected by a branch.
The next line contains an integer M (M ≤ 100,000).
The following M lines each contain a message which is either
"x" which means the existence of the apple on fork x has been changed. i.e. if there is an apple on the fork, then Kaka pick it; otherwise a new apple has grown on the empty fork.
or
"x" which means an inquiry for the number of apples in the sub-tree above the fork x, including the apple (if exists) on the fork x
Note the tree is full of apples at the beginning

Output

For every inquiry, output the correspond answer per line.

Sample Input

3
1 2
1 3
3
Q 1
C 2
Q 1

Sample Output

3
2
 var
left,right,next,head,a,value:array[..]of longint;
n,m,k,x,y,e,tot,s:longint;
i:longint;
ch:char;
procedure add(x,y:int64);
begin
inc(e);a[e]:=y;next[e]:=head[x];head[x]:=e;
end;
procedure dfs(u,m:longint);
var i,v:longint;
begin
inc(tot);
left[u]:=tot;
i:=head[u];
while i> do
begin
v:=a[i];
if v<>m then begin dfs(v,u); end;
i:=next[i];
end;
// inc(tot);
right[u]:=tot; end;
procedure build(u,l,r:longint);
var mid:longint;
begin
if l=r then
begin
value[u]:=;
exit;
end;
mid:=(l+r)>>;
build(u*,l,mid);
build(u*+,mid+,r);
value[u]:=value[u*]+value[u*+];
end;
procedure upate(u,id,l,r:longint);//id 为修改点位置
var mid:longint;
begin
if (l>id)or(r<id) then exit;
if (l=id)and(r=id) then
begin
if value[u]= then value[u]:= else value[u]:=;
exit;
end;
mid:=(l+r)>>;
upate(u*,id,l,mid);
upate(u*+,id,mid+,r);
value[u]:=value[u*]+value[u*+];
end;
function que(u,l,r,ql,qr:int64):int64;
var mid:longint;
begin
if (ql>r)or(qr<l) then exit();
if (ql<=l)and(r<=qr) then exit(value[u]);
mid:=(l+r)>>;
exit(que(u*,l,mid,ql,qr)+que(u*+,mid+,r,ql,qr));
end;
begin
readln(n);
for i:= to n- do
begin
readln(x,y);
add(x,y);
add(y,x);
end;
dfs(,);
build(,,tot);
readln(k);
for i:= to k do
begin
readln(ch,s);
if ch='Q' then writeln(que(,,tot,left[s],right[s]))else
begin
upate(,left[s],,tot);
// upate(,right[s],,tot);
end;
end;
end.

dfs序线段树的更多相关文章

  1. Educational Codeforces Round 6 E dfs序+线段树

    题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...

  2. 【BZOJ-3252】攻略 DFS序 + 线段树 + 贪心

    3252: 攻略 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 339  Solved: 130[Submit][Status][Discuss] D ...

  3. Codeforces 343D Water Tree(DFS序 + 线段树)

    题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...

  4. BZOJ2434 [Noi2011]阿狸的打字机(AC自动机 + fail树 + DFS序 + 线段树)

    题目这么说的: 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现,这个打字机是这样工作的: 输入小 ...

  5. POJ 3321 DFS序+线段树

    单点修改树中某个节点,查询子树的性质.DFS序 子树序列一定在父节点的DFS序列之内,所以可以用线段树维护. 1: /* 2: DFS序 +线段树 3: */ 4:   5: #include < ...

  6. 【XSY2667】摧毁图状树 贪心 堆 DFS序 线段树

    题目大意 给你一棵有根树,有\(n\)个点.还有一个参数\(k\).你每次要删除一条长度为\(k\)(\(k\)个点)的祖先-后代链,问你最少几次删完.现在有\(q\)个询问,每次给你一个\(k\), ...

  7. F - Change FZU - 2277 (DFS序+线段树)

    题目链接: F - Change FZU - 2277 题目大意: 题意: 给定一棵根为1, n个结点的树. 有q个操作,有两种不同的操作 (1) 1 v k x : a[v] += x, a[v ' ...

  8. BZOJ4551[Tjoi2016&Heoi2016]树——dfs序+线段树/树链剖分+线段树

    题目描述 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为1),有以下 两种操作:1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记,其他结点均 ...

  9. BZOJ1103 [POI2007]大都市meg dfs序 线段树

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1103 题意概括 一棵树上,一开始所有的边权值为1,我们要支持两种操作: 1. 修改某一条边的权值为 ...

  10. Codeforces Round #442 (Div. 2)A,B,C,D,E(STL,dp,贪心,bfs,dfs序+线段树)

    A. Alex and broken contest time limit per test 2 seconds memory limit per test 256 megabytes input s ...

随机推荐

  1. iptable防范ddos攻击

    Basic DoS Protection https://github.com/MPOS/php-mpos/wiki/Basic-DoS-Protection # Rule 1: Limit New ...

  2. C#对bat脚本文件的操作示例

    实现C#操作bat脚本文件 using System;using System.Collections.Generic;using System.ComponentModel;using System ...

  3. HCNA配置console线路密码password认证

    1.华为设备配置主机名<Huawei>system    <Huawei>system-view Enter system view, return user view wit ...

  4. linux基础命令-文件查看类命令cat/tac/more/less/tail/head

    cat 查看文本文件内容 -b :给每一行编号不包括空白行 -n:给所有行编号 [root@host01 tmp]# cat -An /root/anaconda-ks.cfg # Kickstart ...

  5. Gameplay Classes

    每个虚幻游戏类都是一个.h和一个.cpp组成. 类在虚幻中有便准的命名模式. 前缀: A继承于可量产的游戏性类.他们都是Actor,可以直接在游戏中生成. U继承于所有游戏性对象.不能在游戏中直接生成 ...

  6. 那些年我用过的SAP IDE

    在Google上根据关键字"程序员鄙视链"搜索,会得到68多万条结果. 玲琅满目的搜索结果里是众多不同维度划分的鄙视链. 其中有一个维度,就是编程工具的鄙视链,比如: 而我在SAP ...

  7. 【CCPC-Wannafly Winter Camp Day3 (Div1) D】精简改良(状压DP)

    点此看题面 大致题意: 给你一张图,定义\(dis(i,j)\)为\(i\)与\(j\)的最短距离,现要求删去若干条边,使得图仍然联通,且\(\sum_{i=1}^n\sum_{j=i+1}^ndis ...

  8. 安装gcc,g++

    安装gcc.g++ sudo apt-get install make gcc g++ 查看g++版本 g++ --version

  9. C# for语句

    一.C# for语句 for语句是C#语言中使用频率最高的循环语句. 1. for语句 语法格式如下: for(initializer; condition; iterator){    embedd ...

  10. html5 canvas中CanvasGradient对象用法

    html5 中canvas提供了强大的渲染样式,可以实现一些比较复杂的样式设置,今天学习了CanvasGradient对象可以实现一个颜色的渐变 CanvasGradient对象可以实现两种不同形式的 ...