DFS order study notes

1. Define and understand:

sequence is a sequence stamp dfs depth-first traversal of the tree generated when the tree structure may be ordered into a linear structure, so that the problem into a linear problem tree

At this time, we can use a lot of good data structures, such as maintenance intervals segment tree, the tree arrays, and so easy to deal with tree problem!

2. important properties

(1) is provided in [x] denotes the first timestamp dfs x node, out [x] represent the time stamp from the exit node x time, num [time] represent the time stamp corresponding to

Which is a tree node. Wherein, the array is dfs NUM our new sequence obtained was a linear sequence node.

Obviously, a node x to the root subtree necessarily occupy a contiguous interval [in [x], out [x]], and all nodes within this range are all child subtrees in Zheke dfs preface node.

Code:

 

the Dfs void (U int, int FA)
{
in [U] = CNT ++; // record the timestamp into the x
dfs [cnt] = u; // update sequence dfs
int V; //
for (int I = head [U]; I; I = E [I] .next)
{
V = E [I] .to;
IF (V = FA)!
the Dfs (V, U); // search subtree
}
OUT [U] = ++ cnt; // record timestamps from the x-
dfs [cnt] = u; // update sequence dfs
}

(2) One problem: a single point of modification, and sub-tree query:

Dfs determined sequence of the entire tree, directly modify the node x corresponds to a single point on dfs sequence value, then the subtree and demand interval and is, consider a tree or array segment tree maintenance can be!

Second problem: Tree chain modification, a single point of inquiry:

Modification value between (u, v) node, such as a plus all

So we consider four chains to operate!

考虑u--->root,v--->root,LCA(u,v)--->root,fa(LCA(uv))--->root!

We will all add a point on the front two paths, the two paths of all minus a point

This tree can be used to maintain the array dfs sequence, corresponding to weights sequentially on each node corresponding to the sequence of Sequence dfs!

Then modify? The Fenwick tree is in fact the maintenance of the weight and the prefix dfs sequence!

get_sum (x) represents a differential and a prefix 1-x

add(l[u],a),add(l[v],a),add(LCA(u,v),-a),add(fa(LCA(u,v)),-a);

val(x)=get_sum(r[x])-get_sum(l[x]-1);

Weight subtree overall modify, query a single point to the root of the weights and, as well as a single point: the question three

For example, we x node to the root of the subtree plus a whole

We all nodes maintain two values

It is a tag, and the other is self-weights of the nodes;

We offer two modes of operation:

1) a single point directly modified

2) will be the subtree rooted at x plus a whole

Operation 1, direct modifications to a single point of operation 2, for all points, tag + = a, then the node's own weight, increasing a - (dep [x] -1) * a; that is not in order to counteract this as the x root

Contribution within the sub-tree nodes! Ask weights and node y to the root, we returned: tag * dep [y] + val [y];

Then the operation can be!

Example: POJ3321  https://www.cnblogs.com/shenben/p/5764932.html

Luo Gu: P3178 tree operation

 

 

 

 

Guess you like

Origin www.cnblogs.com/little-cute-hjr/p/11832278.html