Hdu4612 connectivity, calculate the tree diameter, and add one side to obtain the least Bridge

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4612



Problem description n planets are connected by M bidirectional channels that allow instant transportation. It's always possible to travel between any two planets through these channels.
If we can isolate some planets from others by breaking only one channel, the channel is called a bridge of the transportation system.
People don't like to be isolated. So they ask what's the minimal number of bridges they can have if they decide to build a new channel.
Note that there cocould be more than one channel between two planets.
 
Input the input contains multiple cases.
Each case starts with two positive integers n and M, indicating the number of planets and the number of channels.
(2 <= n <= 200000, 1 <= m <= 1000000)
Next m lines each contains two positive integers A and B, indicating a channel between planet A and B in the system. planets are numbered by 1 .. n.
A line with two integers '0' terminates the input.
Output for each case, output the minimal number of bridges after building a new channel in a line.
Sample Input
4 41 21 31 42 30 0 
 
Sample output
0
 
Source2013 multi-university training Contest 2

/** Hdu4612 connectivity: Find the diameter of the tree, and add one side to obtain the least bridge. The following is a solution to the minimum number of bridges contained in a graph given by adding an edge to an undirected connected graph: when Tarjan shrinks the vertex edge tree and finds the diameter M of the tree, the original bridge number-M is required. This question requires attention to the method of calculating the diameter of the tree (see the code comment) * diameter:; the longest distance between any two nodes */# pragma comment (linker, "/Stack: 1024000000,1024000000") // apply for space # include <stdio. h> # include <string. h >#include <algorithm> # include <iostream> # include <vector> using namespace STD; const int maxn = 200010; const int maxm = 2000014; struct note {int V, next; bool cut, chong_bian;} edge [maxm]; int head [maxn], IP; int n, m; void Init () {memset (Head,-1, sizeof (head); IP = 0;} Void addedge (int u, int V, bool chong_bian) {edge [IP]. V = V, edge [IP]. cut = false, edge [IP]. chong_bian = chong_bian, edge [IP]. next = head [u], head [u] = IP ++;} int dfn [maxn], low [maxn], Dex, inst [maxn], St [maxn], top, CNT, belong [maxn]; void Tarjan (int u, int pre, bool ff) /// FF indicates the duplicate edge {dfn [u] = low [u] = ++ DEX; ST [top ++] = u; inst [u] = 1; for (INT I = head [u]; I! =-1; I = edge [I]. Next) {int v = edge [I]. V; If (V = pre &&(! Ff) continue; // the difference between it and that there is no duplicate edge. If (! Dfn [v]) {Tarjan (v, U, edge [I]. chong_bian); If (low [u]> low [v]) low [u] = low [v]; If (low [v]> dfn [u]) {// bridge ++; edge [I]. cut = true; edge [I ^ 1]. cut = true ;}} else if (Inst [v] & dfn [v] <low [u]) {LOW [u] = dfn [v] ;}} if (dfn [u] = low [u]) {CNT ++; int V; do {v = sT [-- top]; inst [v] = 0; belong [v] = CNT;} while (V! = U) ;}} vector <int> VEC [maxn]; int Dep [maxn]; void DFS (int u) /// DFS evaluate the depth of each node {for (INT I = 0; I <VEC [u]. size (); I ++) {int v = VEC [u] [I]; If (DEP [v]! =-1) continue; Dep [v] = Dep [u] + 1; DFS (v) ;}} void solve () {memset (dfn, 0, sizeof (dfn); memset (Inst, 0, sizeof (insT); CNT = DEX = Top = 0; Tarjan (1, 0, false); For (INT I = 1; I <= CNT; I ++) VEC [I]. clear (); For (INT u = 1; U <= N; U ++) {for (INT I = head [u]; I! =-1; I = edge [I]. next) {If (edge [I]. cut) {int v = edge [I]. v; VEC [belong [u]. push_back (belong [v]) ;}}/// ======== diameter ======== memset (DEP,-1, sizeof (DEP); Dep [1] = 0; DFS (1); int K = 1; for (INT I = 1; I <= CNT; I ++) {If (DEP [I]> Dep [k]) k = I;} memset (DEP,-1, sizeof (DEP); Dep [k] = 0; DFS (k); int ans = 0; For (INT I = 1; I <= CNT; I ++) {ans = max (ANS, DEP [I]);} /// ==================================== printf ("% d \ n", cnt-1-ans );} struct n Ote _ {int X, Y;} node [maxm]; bool CMP (note _ A, note _ B) {if (. X = B. x) return. Y <B. y; return. x <B. x;} int main () {While (~ Scanf ("% d", & N, & M) {If (n = 0 & M = 0) break; For (INT I = 0; I <m; I ++) {int U, V; scanf ("% d", & U, & V); If (u = V) continue; if (u> V) Swap (u, v); node [I]. X = u; node [I]. y = V ;}sort (node, node + M, CMP); Init (); For (INT I = 0; I <m; I ++) {if (I = 0 | (node [I]. x! = Node [I-1]. x | node [I]. y! = Node [I-1]. y) {if (I <M-1 & node [I]. X = node [I + 1]. X & node [I]. y = node [I + 1]. y) {addedge (node [I]. x, node [I]. y, true); addedge (node [I]. y, node [I]. x, true);} else {addedge (node [I]. x, node [I]. y, false); addedge (node [I]. y, node [I]. x, false) ;}} solve () ;}return 0 ;}

Hdu4612 connectivity, calculate the tree diameter, and add one side to obtain the least Bridge

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.