Warm up
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission (s): 2012 Accepted Submission (s): 474
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.
Idea: The question requires that an edge be added to minimize the number of remaining bridges. We can first calculate the dual-connected component of the source image and contract the point to get a tree, each side of the tree is a bridge in the source image, so we can find the longest chain of the tree, and add an edge to the two ends of the longest chain to reduce the most Bridges, therefore, the remaining bridges are obviously the least. Therefore, the original question is to reduce the point of the connected component and record the number of bridges. After the point is reduced, the longest chain of the tree is obtained, the answer is the number of bridges minus the length of the longest chain. After reading most of the questions, I have handed in C ++. It seems that G ++ cannot manually expand the stack. As a result, most of the Cross-G ++ stacks have exploded. The following is code implementation for your reference only...
# Pragma comment (linker, "/STACK: 102400000,102400000") # include <string. h> # include <stdio. h >#include <algorithm> # define maxn 200010 struct Edge {int to; int num; int next;} e [2] [2000100]; int box [2] [maxn], cnt [2]; void init () {memset (box,-1, sizeof (box )); cnt [0] = cnt [1] = 0;} void add (int from, int to, int num, int tt) {e [tt] [cnt [tt]. to = to; e [tt] [cnt [tt]. num = num; e [tt] [cnt [tt]. next = box [tt] [from]; Box [tt] [from] = cnt [tt] ++;} int pre [maxn]; int low [maxn]; int bridge [1000010]; int bcnt = 0; int cnt0; void bridge_search (int now, int edge) {int t; int v, w; low [now] = pre [now] = ++ cnt0; for (t = box [0] [now]; t + 1; t = e [0] [t]. next) {v = e [0] [t]. to; if (edge! =-1 & e [0] [t]. num = e [0] [edge]. num) continue; if (! Pre [v]) {bridge_search (v, t); if (low [v] <low [now]) low [now] = low [v]; if (low [v]> pre [now]) {if (bridge [e [0] [t]. num] = 0) bcnt ++; bridge [e [0] [t]. num] = 1 ;}} else {if (low [now]> pre [v]) low [now] = pre [v] ;}} int Bridge (int n) {int I; cnt0 = 0; memset (pre, 0, sizeof (pre); memset (low, 0, sizeof (low); memset (bridge, 0, sizeof (bridge); bcnt = 0; for (I = 1; I <= n; I ++) {if (! Pre [I]) {bridge_search (I,-1) ;}} return bcnt;} int vis [maxn]; int dist [maxn]; void Dfs (int now, int num) {low [now] = num; vis [now] = 1; int t, v, nn; for (t = box [0] [now]; t + 1; t = e [0] [t]. next) {v = e [0] [t]. to, nn = e [0] [t]. num; if (bridge [nn]) continue; if (! Vis [v]) {Dfs (v, num) ;}} void lensolve (int now) {int t, v; for (t = box [1] [now]; t + 1; t = e [1] [t]. next) {v = e [1] [t]. to; if (dist [v] =-1) {dist [v] = dist [now] + 1; lensolve (v) ;}} void solve (int n, int num) {int I, sum = 0; memset (low, 0, sizeof (low); memset (vis, 0, sizeof (vis )); for (I = 1; I <= n; I ++) {if (! Low [I]) Dfs (I, ++ sum) ;}for (I = 1; I <= n; I ++) {int t, v; for (t = box [0] [I]; t + 1; t = e [0] [t]. next) {v = e [0] [t]. to; if (low [I]! = Low [v]) {add (low [I], low [v],); add (low [v], low [I );}}} if (sum = 1) {printf ("0 \ n"); return;} memset (dist,-1, sizeof (dist); dist [1] = 0; lensolve (1); int ma = 0, root; for (I = 1; I <= sum; I ++) {if (ma <dist [I]) {ma = dist [I]; root = I ;}} memset (dist,-1, sizeof (dist); dist [root] = 0; lensolve (root ); ma = 0; for (I = 1; I <= sum; I ++) {if (ma <dist [I]) {ma = dist [I];} printf ("% d \ n", num-ma);} int main () {// freo Pen ("dd.txt", "r", stdin); int n, m; while (scanf ("% d", & n, & m) & (n + m) {init (); int x, y, I; for (I = 1; I <= m; I ++) {scanf ("% d", & x, & y); if (x! = Y) {add (x, y, I, 0); add (y, x, I, 0) ;}} int sum = Bridge (n); solve (n, sum);} return 0 ;}# pragma comment (linker, "/STACK: 102400000,102400000") # include <string. h> # include <stdio. h >#include <algorithm> # define maxn 200010 struct Edge {int to; int num; int next;} e [2] [2000100]; int box [2] [maxn], cnt [2]; void init () {memset (box,-1, sizeof (box )); cnt [0] = cnt [1] = 0;} void add (int from, int to, int num, int tt) {e [tt] [cnt [Tt]. to = to; e [tt] [cnt [tt]. num = num; e [tt] [cnt [tt]. next = box [tt] [from]; box [tt] [from] = cnt [tt] ++;} int pre [maxn]; int low [maxn]; int bridge [1000010]; int bcnt = 0; int cnt0; void bridge_search (int now, int edge) {int t; int v, w; low [now] = pre [now] = ++ cnt0; for (t = box [0] [now]; t + 1; t = e [0] [t]. next) {v = e [0] [t]. to; if (edge! =-1 & e [0] [t]. num = e [0] [edge]. num) continue; if (! Pre [v]) {bridge_search (v, t); if (low [v] <low [now]) low [now] = low [v]; if (low [v]> pre [now]) {if (bridge [e [0] [t]. num] = 0) bcnt ++; bridge [e [0] [t]. num] = 1 ;}} else {if (low [now]> pre [v]) low [now] = pre [v] ;}} int Bridge (int n) {int I; cnt0 = 0; memset (pre, 0, sizeof (pre); memset (low, 0, sizeof (low); memset (bridge, 0, sizeof (bridge); bcnt = 0; for (I = 1; I <= n; I ++) {if (! Pre [I]) {bridge_search (I,-1) ;}} return bcnt;} int vis [maxn]; int dist [maxn]; void Dfs (int now, int num) {low [now] = num; vis [now] = 1; int t, v, nn; for (t = box [0] [now]; t + 1; t = e [0] [t]. next) {v = e [0] [t]. to, nn = e [0] [t]. num; if (bridge [nn]) continue; if (! Vis [v]) {Dfs (v, num) ;}} void lensolve (int now) {int t, v; for (t = box [1] [now]; t + 1; t = e [1] [t]. next) {v = e [1] [t]. to; if (dist [v] =-1) {dist [v] = dist [now] + 1; lensolve (v) ;}} void solve (int n, int num) {int I, sum = 0; memset (low, 0, sizeof (low); memset (vis, 0, sizeof (vis )); for (I = 1; I <= n; I ++) {if (! Low [I]) Dfs (I, ++ sum) ;}for (I = 1; I <= n; I ++) {int t, v; for (t = box [0] [I]; t + 1; t = e [0] [t]. next) {v = e [0] [t]. to; if (low [I]! = Low [v]) {add (low [I], low [v],); add (low [v], low [I );}}} if (sum = 1) {printf ("0 \ n"); return;} memset (dist,-1, sizeof (dist); dist [1] = 0; lensolve (1); int ma = 0, root; for (I = 1; I <= sum; I ++) {if (ma <dist [I]) {ma = dist [I]; root = I ;}} memset (dist,-1, sizeof (dist); dist [root] = 0; lensolve (root ); ma = 0; for (I = 1; I <= sum; I ++) {if (ma <dist [I]) {ma = dist [I];} printf ("% d \ n", num-ma);} int main () {// freopen ("Dd.txt", "r", stdin); int n, m; while (scanf ("% d", & n, & m) & (n + m) {init (); int x, y, I; for (I = 1; I <= m; I ++) {scanf ("% d", & x, & y); if (x! = Y) {add (x, y, I, 0); add (y, x, I, 0) ;}} int sum = Bridge (n); solve (n, sum);} return 0;} share: