HDU 4612--warm up "undirected graph Edge Double Connectivity Bridge number && reconstruction graph to find the diameter of the tree after the contraction point"

Source: Internet
Author: User

Warm up Time limit:10000/5000 MS (java/others) Memory limit:65535/65535 K (java/others)
Total submission (s): 4830 Accepted Submission (s): 1086


Problem Description N Planets is connected by M bidirectional channels the Allow instant transportation. It's always possible to travel between any and planets through these channels.
If We can isolate some planets from others to breaking only one channel, the channel is called a Bridge of the Transporta tion System.
People don ' t like is isolated. So they ask what ' s the minimal number of bridges they can has if they decide to build a new channel.
Note that there could is more than one channel between and one planets.

Input the input contains multiple cases.
Each case starts with 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-positive integers a and B, indicating a channel between planet A and B in the system. Planets is numbered by 1..N.
A line with the 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

Sample Output
0
Test instructions: There are n points, M-Edge (there is a heavy edge) of the graph, so that there may be a bridge in the picture, asked to add an edge, so that the bridge to the minimum, to seek the bridge tree.

Idea: Figure out the number of bridges in the diagram, and then the reconstruction map must be a tree, the longest diameter of the tree, in the diameter of the two ends of the point connected, the figure of the bridge will be the least.

#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <stack > #define MAXN 200000+1000#define MAXM 2000000+1000#pragma COMMENT (linker, "/stack:1024000000,1024000000") using Namespace Std;int N, m;struct node {int u, V, next;}; Node Edge[maxm];int HEAD[MAXN], Cnt;int DFN[MAXN], Low[maxn];int STACK[MAXN], Top;bool instack[maxn];int BELONG[MAXN]; int dfs_clock;int ebc_clock;int bridge;vector<int>map[maxn];int dist[maxn], vis[maxn];int ans, nod;void init () {C    NT = 0; Memset (Head,-1, sizeof (head));}    void Addedge (int u, int v) {edge[cnt] = {u, V, Head[u]}; Head[u] = cnt++;}        void Getmap () {while (m--) {int A, B;        scanf ("%d%d", &a, &b);        Addedge (A, b);    Addedge (b, a);    }}void Tarjan (int u, int per) {int V;    Low[u] = dfn[u] = ++dfs_clock;    stack[top++] = u;    Instack[u] = true;    int has = 1;        for (int i = head[u]; I! =-1; i = edge[i].next) {v = edge[i].v; if (V= = per && have) {has = 0;        Continue            } if (!dfn[v]) {Tarjan (V, u);            Low[u] = min (Low[u], low[v]);        if (Low[v] > Dfn[u]) bridge++;    } else if (Instack[v]) low[u] = min (Low[u], dfn[v]);        } if (dfn[u] = = Low[u]) {ebc_clock++;            do{v = stack[--top];            INSTACK[V] = false;        BELONG[V] = Ebc_clock;    }while (v! = u);    }}void Suodian () {//ëõµã½¨ðâí¼for (int i = 1; I <= ebc_clock; ++i) map[i].clear ();        for (int i = 0; i < cnt; i = i + 2) {int u = belong[edge[i].u];        int v = belong[edge[i].v];            if (U = v) {map[u].push_back (v);        Map[v].push_back (U);    }}}void Find () {memset (low, 0, sizeof (low));    memset (DFN, 0, sizeof (DFN));    Memset (Instack, False, sizeof (Instack));    memset (Belong, 0, sizeof (Belong));    Dfs_clock = Ebc_clock = top = Bridge = 0; for (int i =1; I <= N;    ++i) {if (!dfn[i]) Tarjan (i, I);    }}void BFS (int x) {queue<int>q;    memset (Vis, 0, sizeof (VIS));    memset (Dist, 0, sizeof (Dist));    VIS[X] = 1;    Ans = 0;    nod = x;    Q.push (x);        while (!q.empty ()) {int u = q.front ();        Q.pop ();            for (int i = 0; i < map[u].size (); ++i) {int v = map[u][i];                if (!vis[v]) {vis[v] = 1;                DIST[V] = Dist[u] + 1;                    if (ans < dist[v]) {nod = V;                ans = dist[v];            } q.push (v);        }}}}int Main () {while (scanf ("%d%d", &n, &m), n | | m) {init ();        Getmap ();        Find ();        Suodian ();        BFS (1);        BFS (NOD);    printf ("%d\n", Bridge-ans); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 4612--warm up "undirected graph Edge Double Connectivity Bridge number && reconstruction graph to find the diameter of the tree after the contraction point"

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.