tarjan shrink Point (Luo Gu P387)

This problem solution draws in part on nine wild blog

Topic analysis

  • Given a \ (n-\) points \ (m \) edges directed graph, each point has a weight, a path seeking the path through the weighted sum of the point and the maximum. You just out of this requirement and weight.
  • Allowing multiple passes an edge or a point, however, after the repeated point, the weight is calculated only once.

  • Without this restriction, then later, it is a must FIG acyclic graph . Because of the ring, then I can always run on the ring, so the answer is no upper bound

  • No ring, then I Meng can naturally think of a \ (O (n) \) topology \ (dp \) practice, do first-degree is \ (0 \) points, update penetration is not \ (0 \ ) the point, to update the degrees (0 \) \ points to join the queue, before continuing to do things

  • Now consider how do ring

  • The idea is to have a greedy, to the ring on the first point on the ring are completed, and then from any point on the ring of departure

  • In fact, this ring can be seen as a big point , that is, we have to introduce the protagonist \ (\ to \) reduction point


condensing point tarjan

The picture below is from nine wild blog that copy over

  • The distance of each other set of points is called a connected component
  • The largest can reach the point that each set of points is the strongly connected component
  • In particular, the point may be a single strongly connected component

For example: \ (\ {4, 5 \} \) is a link component , and {4,5,6} $ $ is a strongly connected components (a large dot)

tarjan process is by dfs find strongly connected components (a large dot) process

FIG dfs look through all of the points are not traversed, you will get a directed tree , the tree is clearly not ring. (Note that the search will not search through the point

The ring can produce only (point to point has been traversed) side

We found \ (. 7 \. 3 to \) ( red side / lateral side prongs) this will not produce side communication component

And \ (. 6 \. 4 to \) ( green edge / edge reversion) of this edge will produce Unicom component

Specifically: a parent-child relationship side will produce Unicom component

We dfs when the need to use a stack to store the current point on the path (all points in the stack must be a parent-child relationship)

We use some of the array to represent dfs process

int tim, dfn[MAX], low[MAX]

\ (dfn [i] \) represents node traversed \ (I \) timestamp (first traversal times)

\ (low [i] \) represents the earliest point can reach up

Initialization \ (dfn [i] = low [i] = ++ tim \)

We can write the code based on the above process steps

    for(int i = head[u]; i; i = nex[i]) {
        if(dfn[to[i]]) {
            if(instack[to[i]]) {
                if(low[to[i]] < low[u]) {
                    low[u] = low[to[i]];
                }
            }
        } else {
            dfs(to[i]);
            if(low[to[i]] < low[u]) {}
                low[u] = low[to[i]];
            }
        }
    }

If the current node \ (U \) , \ (DFN [U] == Low [U] \) it shows the top element until the node \ (U \) belong to a strong link component ,Emotional understanding

The elements in the stack pop and gave them the same color as the mark (the same strongly connected components)

    if(dfn[u] == low[u]) {
        ++totcol;
        do {
            int v = stk[top];
            col[v] = totcol;
            instack[v] = false;
        } while(stk[top--] != u);
    }

Realization look at the code

\(\color {Deepskyblue} {Code}\)

There is also a tarjan practice good hand problem

Guess you like

Origin www.cnblogs.com/Lskkkno1/p/11521301.html