HDU 4612--warm up —————— "edge double connected component, tree diameter"

Source: Internet
Author: User

Warm up

Time limit:5000MS Memory Limit:65535KB 64bit IO Format:%i64d &%i64 U SubmitStatusPracticeHDU 4612

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

4 41 21 31 42 30 0

Sample Output

0 The main topic: There are n planets, M-two-way channels connected to the M-to-planets. Ask you to create a new bidirectional channel, the least number of bridges will be left in the graph. There are heavy edges. Problem-Solving ideas: undirected graph for the side of the two connected components, shrinking point, re-composition, forming a tree. The diameter of the tree is then calculated by subtracting the diameter of the tree from the total Bridge of the original. In order to find the diameter of the tree, we use two searches, the first time from any point, the furthest node found is the one end of the diameter, and then from the end of the search again, searching to the other end of the diameter.
#include <stdio.h> #include <string.h> #include <algorithm> #include <vector> #include <    stack>using namespace Std;const int maxn = 200100;struct edge{int from,to,dist,next; Edge () {} edge (int _to,int _next): to (_to), Next (_next) {}}edges[maxn*10];int HEAD[MAXN], Tot;int Dfs_clock, DFN[MAXN], BR Inum;int STACK[MAXN], INSTACK[MAXN], top, EBCCNO[MAXN], ebcc_cnt;int deg[maxn];vector<int>g[maxn];void init () {T    ot = 0;    Brinum = Dfs_clock = 0;    top = 0;    ebcc_cnt = 0;    memset (deg,0,sizeof (deg)); memset (head,-1,sizeof (Head));}    void Addedge (int _u,int _v) {Edges[tot] = Edge (_v,head[_u]); Head[_u] = tot++;}    int dfs (int u,int fa) {int Lowu = dfn[u] = ++dfs_clock;   Stack[++top] = u;    Instack[u] = 1;        for (int i = head[u]; i =-1; i = edges[i].next) {int v = edges[i].to;            if (!dfn[v]) {int LOWV = DFS (v,i);            Lowu = min (LOWU,LOWV);            if (Lowv > Dfn[u]) {brinum++;     }   }else if (Dfn[v] < Dfn[u] && (fa^1)! = i) {//Here Use the number of the edge to mark whether it is the back edge of the same edge Lowu = min (lowu,dfn[v]);        }} if (dfn[u] = = Lowu) {//Find an edge dual-connected component ebcc_cnt++; for (;;)         {int v = stack[top--];            INSTACK[V] = 0;   EBCCNO[V] = ebcc_cnt;            Divide each point by a component designator if (U = = v) {break;    }}}//low[u] = Lowu; return LOWU;}    void find_ebcc (int n) {memset (dfn,0,sizeof (DFN));    memset (instack,0,sizeof (instack));        for (int i = 1; I <= n; i++) {if (!dfn[i]) {DFS (i,-1);        }}}int Pos, maxd;void dfs1 (int u,int dep,int FA) {//Seek tree diameter if (dep > Maxd) {maxd = dep;    pos = u;        } for (int i = 0; i < g[u].size (); i++) {int v = g[u][i];        if (FA = = v) {continue;}    DFS1 (V,dep+1,u);    }}int Main () {int n,m;        while (scanf ("%d%d", &n,&m)!=eof&& (n+m)) {init ();          for (int i = 0; I <= N; i++) {  G[i].clear ();        } int A, b;            for (int i = 0; i < m; i++) {scanf ("%d%d", &a,&b);            Addedge (A, b);        Addedge (B,a);        } FIND_EBCC (n); for (int i = 1; I <= N, i++) {for (int j = head[i]; J! =-1; j = edges[j].next) {int v = edges[                j].to;                if (ebccno[i]! = Ebccno[v]) {//re-framing, forming a tree g[ebccno[i]].push_back (Ebccno[v]);        }}} pos = 1, maxd = 0;        DFS1 (1,0,-1); int st = POS;        Maxd = 0;        DFS1 (pos,0,-1);    printf ("%d\n", Brinum-maxd); } return 0;}

  

HDU 4612--warm up —————— "edge double connected component, tree diameter"

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.