[Two dual-connected components and O (nlogn )~ O (logn) is used to calculate the Tarjan algorithm of LCA online]

Source: Internet
Author: User

I got two kinds of Dual-connected components from the last two days.

For related concepts, refer to byV's blog. Here are several key points.

The bridge is the edge of dfn [I] <low [J] (I, j)

The cut point is at least one of the edges (I, j) that it sends to satisfy dfn [I] <= low [J] (here the equal sign is used)

What I usually want is the so-called edge dual-connected component. In fact, it is the set of points that are still together after the bridge is removed.

There is also a vertex dual-connected component, which is actually a set of edges that are still together after the cut point is removed.

PS: Click <=> edge. Why is this thing not called in turn?

If the method is used, the Tarjan algorithm is used. However, an edge stack must be opened when the edge is connected to two components, when an edge meeting dfn [I] <= low [J] is met, the stack is rolled back to this edge.

Another thing is the algorithm used to calculate the LCA.

The main idea it uses is to multiply and find the point after each point jumps 2 ^ J, so that a point can jump to its ancestor with the complexity of O (logn ).

But how do I do this?

The first simple idea is the depth of the Two-Point LCA, and then jump and judge again -- but in this way, the complexity of each query reaches O (logn) ^ 2), which is not ideal.

Is there a direct logn method? Obviously, the point in depth (I, j) First jumps to two places with the same depth and the other two jumps together. This is the complexity of O (logn, but the constant is multiplied by 2.

Is there a method to skip only one round? Not yet expected

In addition, why am I not here to mention this very beautiful LCA method?

Although this method is not optimal in both time and space, it has a huge advantage in online and scalable

In the ghy paper, it is difficult to extend the Method of Using Euler's sequence to obtain the LCA, and some additional information such as the weights on the path and so on cannot be obtained simultaneously.

There is an offline O (N + q) that uses the and query set method. Although it is highly scalable, It is offline.

Another point is that this method requires very little information. Only the tree in the father's representation can be used to find everything. Other methods need to store the tree in an adjacent table.

This advantage is actually quite useful-this method has a low programming complexity advantage when some "extended version" Trees require LCA.

For example, in a cactus diagram (a cactus diagram, each vertex in the diagram belongs to a simple ring at most), or in the yt Shortest Path, each edge belongs to a simple ring at most, if you change the father of a vertex in the expanded block to it, the graph becomes a tree.

In fact, it is easier to find out the father's representation of the last tree. If we need to re-create an adjacent table, it will be troublesome. In this case, it is very convenient to use this method to find the LCA.

 

The yt-Shortest-short-circuit code of the training team is attached, which contains most of the algorithms mentioned above.

Program syj; <br/> uses <br/> math; <br/> var <br/> E, I, J, K, L, Ed, TB, T, TP, n, m, task, JJ, MD: longint; <br/> H, A, B, SK, Q, low, D, P, F, S: array [0 .. 10005] of longint; <br/> next, point, W: array [0 .. 40005] of longint; <br/> V: array [0 .. 10005] of Boolean; <br/> U: array [0 .. 40005] of Boolean; <br/> FA, G: array [0 .. 15, 0 .. 10005] of longint; <br/> procedure work; <br/> begin <br/> I: = point [B [1]; K: = 0; <br/> Inc (TB); <br/> for J: = 2 to B [0] Do begin <br/> K: = K + W [B [J-1]; <br/> Jj: = point [B [J]; <br/> F [JJ]: = Tb; fa [0, JJ]: = I; <br/> P [JJ]: = K; <br/> end; <br/> S [TB]: = K + W [B [B [0]; <br/> for J: = 2 to B [0] Do begin <br/> Jj: = point [B [J]; <br/> G [0, JJ]: = min (P [JJ], s [TB]-P [JJ]); <br/> end; <br/> procedure DFS (I: longint); <br/> var J, K: longint; <br/> begin <br/> Inc (t); A [I]: = T; low [I]: = T; <br/> Inc (TP ); SK [TP]: = I; <br/> V [I]: = true; J: = H [I]; <br/> while j <> 0 do begin <br/> K: = point [J]; <br/> if not U [J] Then begin <br/> U [J]: = true; U [j xor 1]: = true; <br/> Inc (ed); Q [ed]: = J; <br/> If a [k] = 0 then begin <br/> DFS (k ); <br/> If low [k] <low [I] Then low [I]: = low [k]; <br/> end else <br/> If V [k] and (A [k] <low [I]) then low [I]: = A [k]; <br/> If low [k]> = A [I] Then begin <br/> B [0]: = 0; <br/> while Q [ed + 1] <> J do begin <br/> Inc (B [0]); B [B [0]: = Q [ed]; <br/> Dec (ed); <br/> end; <br/> If B [0]> 1 then work else begin <br/> Fa [0, K]: = I; G [0, K]: = W [J]; <br/> end; <br/> J: = next [J]; <br/> end; <br/> while SK [TP + 1] <> I do begin <br/> V [SK [TP]: = false; dec (TP); <br/> end; <br/> procedure find (I: longint ); <br/> begin <br/> if d [I]> 0 Then exit; <br/> Find (Fa [0, I]); <br/> d [I]: = d [Fa [0, I] + 1; <br/> end; <br/> begin <br/> assign (input, 'path. in '); reset (input); <br/> assign (output, 'path. out'); rewrite (output); <br/> readln (n, m, task); <br/> E: = 1; <br/> for M: = 1 to M do begin <br/> readln (I, j, k); <br/> Inc (E); next [e]: = H [I]; point [e]: = J; W [e]: = K; H [I]: = E; <br/> Inc (E); next [e]: = H [J]; point [e]: = I; W [e]: = K; H [J]: = E; <br/> end; <br/> DFS (1); <br/> d [1]: = 1; <br/> for I: = 1 to n do begin <br/> Find (I); <br/> if d [I]> MD then MD: = d [I]; <br/> end; <br/> MD: = trunc (Ln (md-1)/ln (2) + 1e-8); <br/> for J: = 1 to md do <br/> for I: = 1 to n do begin <br/> Fa [J, I]: = Fa [J-1, Fa [J-1, i]; <br/> G [J, I]: = G [J-1, I] + G [J-1, Fa [J-1, I]; <br/> end; <br/> for task: = 1 to task do begin <br/> readln (I, j ); <br/> if d [I]> d [J] Then begin <br/> K: = I; I: = J; J: = K; <br/> end; <br/> K: = 0; <br/> for L: = md downto 0 DO <br/> if d [Fa [L, j]> = d [I] Then begin <br/> K: = K + G [L, J]; <br/> J: = Fa [L, j]; <br/> end; <br/> If I = J then begin <br/> writeln (k); continue; <br/> end; <br/> for L: = md downto 0 DO <br/> If Fa [L, I] <> Fa [L, J] Then begin <br/> K: = K + G [L, I] + G [L, J]; <br/> I: = Fa [L, I]; J: = Fa [L, j]; <br/> end; <br/> If (F [I] = f [J]) and (F [I] <> 0) then <br/> K: = K + min (ABS (P [I]-P [J]), s [f [I]-ABS (P [I]-P [J]) <br/> else <br/> K: = K + G [0, i] + G [0, J]; <br/> writeln (k); <br/> end; <br/> close (input); close (output ); <br/> end. 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.