POJ3352-Road Construction

Source: Internet
Author: User

Reprinted please indicate the source: Thank youHttp://blog.csdn.net/lyy289065406/article/details/6762370

 

 

 

General question:

A company wants to turn a tropical paradise island into a tourist attraction. There are n tourist attractions on the island, and any two tourist attractions are connected through a path (note that they are not necessarily directly connected ). In order to provide more convenient services for tourists, the company requires the road department to add some facilities on some roads.

Each time the road department selects only one road for construction, other roads can still be used before the construction of the road is completed. However, when a road department is under construction, tourists are prohibited from passing through the construction. This causes tourists to be unable to reach some scenic spots during construction.

In order to make all tourist sites accessible to tourists during the construction period, the company decided to build some temporary bridges so that no matter which road Department chooses for construction, visitors can reach all tourist attractions. Given the current number of R roads that are allowed to pass, how many more Temporary bridges should the company build so that tourists can ignore the existence of the road department to reach all tourist attractions?

 

Solution:

After I have done poj2942, this question is just a water question =

 

First, create a model:

Given a connected undirected graph G, you must add at least several edges to change it to a dual connected graph.

 

The model is very simple. We can think that the side is deleted as the road under construction. Therefore, a graph G can still be connected after any edge is deleted. if and only when graph G is at least dual-connected.

PS: Don't ask me why I am not 3-connected or 4-connected... people ask the question "add at least a few sides...

 

Obviously, when graph G has a bridge (edge Cutting), it must not be a dual-connection. The two endpoints of the bridge must belong to the two edge dual-connected components of the graph G respectively (note that they are not vertex dual-connected components). Once the bridge is deleted, the two [edge dual-connected components] must be disconnected, and graph G will not be connected. However, if an edge is added between two [edge dual-connected components], the bridge is no longer a bridge, and the two [edge dual-connected components] are also dual-connected.

 

What if graph G has multiple [edge dual-connected components? How many edges should be added at least to ensure that any two edge dual-connected components are dual-connected (that is, graph G is dual-connected )?

 

This is the question of this question. To solve this problem:

 

1. First, we need to find all edge dual-connected components of graph G ].

The Tarjan algorithm is the simplest and most effective method to find all edge double-connected components of graph G, because the Tarjan algorithm generates a low value for all nodes in graph G during the DFS process, because the question already shows that no duplicate edge exists between any two nodes, therefore, the two nodes with the same low value must be in the same edge dual-connectivity component! (If there is a duplicate edge, different low values may belong to the same edge dual-connected component. In this case, we need to use other methods to solve the edge dual-connected component. But this is not the subject to be discussed)

 

2. Think of every edge-connected component as a point (that is, a contraction point ])

Some people also call the [point reduction] as a [block], which is the same. In fact, the scale-down is not a real scale-down. As long as the low value is used to process the graph G's point classification, the scale-down has been completed.

Take example 1 as an example. The graph G obtained in Example 1 is the upper left graph,

Here, low [4] = low [9] = low [10]

Low [3] = low [7] = low [8]

Low [2] = low [5] = low [6]

Low [1] Government alone ....

Divide the dots with the same low value into one class. Each class is a [edge dual-Connected Component], that is, a [point reduction]. It is not difficult to find that the edges between the [point contraction] are connected, all are the bridges of graph G, so we get the tree constructed from the top right graph with the point shrinking as the node and the bridge as the tree edge.

 

3. The problem is transformed into "How many tree edges are added to the scaled tree at least, making the tree a dual-connected graph ".

First, we know an equation:

If you want to change a tree to a dual connected graph after adding several edges

At least the number of edges added = (the total number of knots in the tree is 1 + 1)/2

(It doesn't prove it. Just draw several trees by yourself)

 

Then we only need to shrink the number of knots (that is, the number of leaves) with a total degree of 1 in the Vertex Tree. In other words, we only need to find the degrees of all the contraction points, and then judge the number of contraction points with the degree of 1, the problem is solved.

 

4. Method for Finding the degrees of all shrinkage points

If the two vertices of graph G are not in the same [contraction point], the degrees of their respective [contraction points] are + 1. Note that the graph G is undirected, so that the degrees of all [point shrinking] are two times of the true degrees, and the leaves must be judged after division of 2.

 

 

 

========================================================== ======================================

Refer to the relevant portal of the above knowledge points. If you do not know it, you can go to school immediately!

Definition of knowledge points about Graph Theory: http://www.byvoid.com/blog/biconnect/

Basic Introduction to The Tarjan algorithm:

Http://hi.baidu.com/lydrainbowcat/blog/item/42a6862489c98820c89559f3.html

Tarjan algorithm application extension:

Http://hi.baidu.com/lydrainbowcat/blog/item/2194090a96bbed2db1351de8.html

 

 

// Memory time // 340 K 32 Ms # include <iostream> using namespace STD; Class node {public: int ID; Class node * Next; node (): ID (0), next (0) {}}; class solve {public: Solve (int n, int R): n (n), R (r) {initial (); input_creat (); Tarjan (1,-1); // The graph G given in this question is connected, therefore, you can search for printf ("% d \ n", bcc_sp_d_e ();} ~ from any node ());}~ Solve () {Delete [] dfn; Delete [] low; Delete [] degree; emptylist () ;}int min (int A, int B) const {return a <B? A: B;} void initial (void); // apply for a bucket and initialize void input_creat (void); // enter and create the figure gvoid Tarjan (INT S, int father ); // calculate the low [] array for finding all sides of the dual-Connected Component int bcc_sp_d_e (void); // construct each side of the dual-connected component (BCC) as the contraction point (SP ), calculate the degree (D) of each contraction point. // the return value is the number of void dellink (node * P) that makes the graph G the minimum number of edges (e) to be added for the dual-connection ); // release the entire chain void emptylist (void) with P as the header; // release the adjacent linked list protected: int N; // The number of islandsint R; // The number of roadsnode ** linkhead; // int timestamp of the List header; // (external) timestamp int * dfn; // DF N [u]: The Search Order (timestamp) of node u int * low; // low [u]: the subtree of the node u or u can trace the number of the node in the earliest stack int * degree; // record the total number of each contraction point}; void solve :: initial (void) {linkhead = new node * [n + 1]; for (INT I = 1; I <= N; I ++) linkhead [I] = 0; timestamp = 0; dfn = new int [n + 1]; Low = new int [n + 1]; memset (dfn, 0, sizeof (INT) * (n + 1); memset (low, 0, sizeof (INT) * (n + 1); Degree = new int [n + 1]; memset (degree, 0, sizeof (INT) * (n + 1); return;} void solve: input_creat (void) {int A, B; node * TMP; for (I NT j = 1; j <= r; j ++) {scanf ("% d", & A, & B); If (! Linkhead [a]) linkhead [a] = new node; If (! Linkhead [B]) linkhead [B] = new node; TMP = linkhead [a]-> next; linkhead [a]-> next = new node; linkhead [a]-> next-> id = B; linkhead [a]-> next = TMP; TMP = linkhead [B]-> next; linkhead [B]-> next = new node; linkhead [B]-> next-> id = A; linkhead [B]-> next = TMP ;} return;} void solve: Tarjan (int s, int father) {dfn [s] = low [s] = ++ timestamp; for (node * P = linkhead [s]-> next; P = p-> next) {int T = p-> ID; If (T! = Father & dfn [T] <dfn [s]) {If (dfn [T] = 0) // s-> T is the branch edge {Tarjan (t, s); low [s] = min (low [s], low [T]);} else // s-> T is the backward side {LOW [s] = min (low [s], dfn [T]) ;}}return ;} int solve :: bcc_sp_d_e (void) {for (INT I = 1; I <= N; I ++) if (linkhead [I]) {for (node * P = linkhead [I]-> next; P = p-> next) // enumerate the connected vertex I in graph G <-> J {// Since graph G is an undirected graph, the connected vertex is a bidirectional Int J = p-> ID; if (low [I]! = Low [J]) // The two points with the same low value in graph G must be in the same edge dual-connected component (that is, the same contraction point) {// check if I and j are not in the same shrink point degree [low [I] ++; // The degree of contraction of node I + 1 degree [low [J] ++; // degree of contraction of node J + 1 }}int leave = 0; // The total number of records = 1 (leaf) shrinkage point for (int K = 1; k <= N; k ++) // enumerate the degree DIF (degree [k]/2 = 1) of each contraction point. // as it is an undirected graph, therefore, the degree of each contraction point is calculated twice. After the sum of 2, the true degree leave ++; Return (leave + 1)/2 is obtained; // Number of edges to be added for connecting a tree into an edge pair Connected Component = (number of leaf nodes + 1)/2} void solve: dellink (node * P) {If (p-> next) P = p-> next; Delete [] P; return;} void solve: emptylist (voi D) {for (INT I = 1; I <= N; I ++) if (linkhead [I]) dellink (linkhead [I]); return ;} int main (void) {int N, R; while (scanf ("% d", & N, & R )! = EOF) Solve poj3352 (n, R); 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.