BRIDGE/cutting edge in poj3177-tarjan

Source: Internet
Author: User

There are f pastures, 1 <= F <= 5000. Now a herd often needs to migrate from one farm to another. The cows are tired of taking the same path, so it is necessary to build a few more roads so that they can always choose at least two independent roads when migrating from one farm to another. Now there is at least one path between any two pastures in farm F, and the cows need at least two.
Given the existing R bars directly connect the roads of the two pastures, the F-1 <= r <= 10000, calculates the number of new roads that need to be built directly to the two pastures, so that there are at least two independent routes between any two pastures. Two independent paths refer to paths without public edges, but can pass through the same intermediate vertex.

If low [v]> dfn [u], (u, v) is the cut edge.. However, we do not judge this in actual processing, because some graphs may have duplicate edges, which is hard to handle. We record the number of each edge (the number of the two undirected edges is the same), and record the number of the father of each vertex to its edge. If the edge (u, v) is the father side of V. Therefore, you cannot use dfn [u] to update low [v]. In this way, if we find that low [v] = dfn [v] After traversing all the subnodes of V, the father side of U (u, v) is the cut edge.

Void Tarjan (int x)
{
Vis [x] = 1;
Dfn [x] = low [x] = ++ num;
For (INT I = head [X]; I; I = next [I])
If (! Vis [ver [I])
{
P [ver [I] = edge [I]; // record the parent edge
Tarjan (ver [I]);
Low [x] = min (low [X], low [ver [I]);
}
Else if (P [x]! = Edge [I]) // It is updated only when it is not the father side.
Low [x] = min (low [X], dfn [ver [I]);
If (P [x] & low [x] = dfn [x]) f [p [x] = 1; // cut edge
}

2. Calculate the dual-connected component and construct the dual-connected component:

ForPoint-connected branchIn factCut PointBy the way, we can find the dual-connected branches of each vertex. Create a stack to store the current dual-connected branches. When you search for a graph, add this edge to the stack every time you find a branch edge or backward edge (not a cross-cross edge. If the DFS (u) <= low (v) is met at a time, it indicates that u is a cut point, and the edges are extracted from the top of the stack one by one until the edges (u, v. A cut point can belong to multiple vertices in a dual-connected branch. The other vertices and each edge only belong to one vertex in a dual-connected branch.

ForEdge dual-connectivity BranchThe method is simpler. You only needFind all bridgesAfter the bridge edge is deleted and the source image is changed to multiple connected blocks, each connected block is an edge dual-connected branch. A bridge does not belong to any edge dual-connected branch. The other edges and each vertex belong to and only belongs to one edge dual-connected branch.

How to add an edge to a connected graph with a bridge and change it into an edge pair Connected Graph? The method is to first find all bridges and then delete these bridges. Each connected block is a double connected subgraph. Every connected subgraph is reduced to a vertex, and the bridge edge is added back. The final graph is a tree with the edge connectivity of 1.

Count the number of nodes with a moderate value of 1 in the tree, that is, the number of leaf nodes, which is recorded as leaf. Then, at least two (leaf + 1)/two sides are added to the tree to enable the tree to be connected by two sides. Therefore, the minimum number of edge added is (leaf + 1)/2. The specific method is to first connect an edge between the two most recent common ancestor's two leaf nodes, so that the two nodes can contract all the points on the path of the ancestor, because the formation of a ring must be dual-connected. Then we can find two leaf nodes with the farthest recent common ancestor. This one is exactly the same as (leaf + 1)/two times, and all the points are reduced together.

# Include <stdio. h> # include <stdlib. h> # include <string. h> # define Nmax 5005 # define min (a, B) (a <B? A: B) struct edge {int V, next;} edge [2 * Nmax]; int preedge [Nmax]; int N, edgenum, index; bool map [Nmax] [Nmax]; int dfn [Nmax], low [Nmax]; void addedge (int u, int v) {edge [edgenum]. V = V; edge [edgenum]. next = preedge [u]; preedge [u] = edgenum ++;} void Tarjan (int u, int father) {dfn [u] = low [u] = ++ index; For (INT I = preedge [u]; I! =-1; I = edge [I]. Next) {int v = edge [I]. V; If (! Dfn [v]) {Tarjan (v, U); low [u] = min (low [u], low [v]);} else if (V! = Father) {LOW [u] = min (low [u], dfn [v]) ;}} void output () {int CNT [Nmax], num = 0; memset (CNT, 0, sizeof (CNT); For (INT I = 1; I <= N; ++ I) {for (Int J = preedge [I]; j! =-1; j = edge [J]. Next) {int v = edge [J]. V; If (low [v]! = Low [I]) {CNT [low [I] ++ ;}}for (INT I = 0; I <= N; ++ I) {If (CNT [I] = 1) {num ++ ;}} printf ("% d \ n", (Num + 1)/2 );} int main () {int R, U, V; while (scanf ("% d", & N, & R )! = EOF) {Index = edgenum = 0; memset (preedge,-1, sizeof (preedge); memset (MAP, false, sizeof (MAP); memset (dfn, 0, sizeof (dfn); For (INT I = 0; I <r; ++ I) {scanf ("% d", & U, & V ); if (! Map [u] [v]) {addedge (u, v); addedge (v, U ); map [u] [v] = map [v] [u] = true;} Tarjan (1, 1); output ();} return 0 ;}

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.