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. The fourteenth day

    A man is not old as long as he is seeking something. A man is not old until regrets take the place o ...

  2. 一步步理解typedef

    1.如何用C语言实现一个函数,传递两个整形数,返回两个数的和? #include<stdio.h> int add(int a,int b) { return a+b; } void ma ...

  3. Spark Streaming与Kafka集成

    Spark Streaming与Kafka集成 1.介绍 kafka是一个发布订阅消息系统,具有分布式.分区化.多副本提交日志特点.kafka项目在0.8和0.10之间引入了一种新型消费者API,注意 ...

  4. 在windows bat脚本(batch)中延时

    编写bat脚本时,有事我们希望在指令和指令之间,加入延时.例如当一条指令执行后,windows需要一定时间来响应的情况. 以下是一种实现方法,通过ping 指令来实现,5表示ping5次,就是延时5秒 ...

  5. [转载]弹出一个不带地址栏、工具栏的IE非模态窗口

    标签:ie /非模态窗口 window.open(url,'_blank','menubar=no,fullscreen=1,toolbar=no,resizable=no,location=no,s ...

  6. MySQL入门很简单: 8查询数据

    1. 查询语句语法 SELECT 属性列表 FROM 表名和视图列表 [WHERE 条件表达式1] [GROUP BY 属性名1 [HAVING t条件表达式2]] [ORDER BY 属性名2 [A ...

  7. PHP获取系统时间不对的解决办法(转载)

    原地址:https://blog.csdn.net/u012124764/article/details/51450958 使用PHP获取系统时间,发现时间不对,是因为PHP默认的时区是UTC,应该将 ...

  8. hdu-3015 Disharmony Trees---离散化+两个树状数组

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3015 题目大意: 有一些树,这些树的高度和位置给出.现在高度和位置都按从小到大排序,对应一个新的ra ...

  9. 2017.10.27 C语言精品集

    第一章 程序设计和C语言 1.1 什么是计算机程序? @ ······ 所谓程序,就是一组计算机能识别和执行的指令.每一条指令使计算机执行特定的操作. 计算机的一切操作都是由程序控制的.所以计算机的本 ...

  10. an exception occurred while initializing the database.

    对于手动删除本地的LocalDB数据库之后出现标题所示异常的,推荐下面的命令: sqllocaldb.exe stop v11.0 sqllocaldb.exe delete v11.0 在程序包管理 ...