Hdoj 4612 Warm up "double-connected components for bridge && indent building new graph to find the diameter of the tree"

Source: Internet
Author: User

Warm up

Time limit:10000/5000 MS (java/others) Memory limit:65535/65535 K (java/others)
Total submission (s): 5093 Accepted Submission (s): 1131


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 INPUT4 41 21 31 42 30 0

Sample Output0 Test instructions: N points m Edge, asked to create a new edge, you can make the number of bridges to a minimum, the output of the bridge is the least: the number of the bridge to find out all the numbers, the bridge connected to the various graphs, according to the diameter of the tree to find the longest path, the number of bridges minus the diameter of the tree
SCC on behalf of the double-unicom component writing habit of the right to write an SCC is also too lazy to change #include <stdio.h> #include <string.h> #include <algorithm> #include <stack> #include <queue> #include <vector> #define MAXM 2000100#define MAX 200100#define INF 0x7fffffusing namespace Std;int n,m,bridge,sum;int low[max],dfn[max];int head[max],ans,age;int sccno[MAX];// Represents the current point belonging to which double-connected component int dfsclock,scccnt;vector<int>newmap[max];//stores the indent after the new figure int instack[max];//marks whether the current point is in the stack int Dis[max] ;//Find the diameter of the tree when the length of the record path int vis[max];//the diameter of the tree when the mark is in the queue stack<int>s;struct node{int Beg,end,next;} Edge[maxm];void init () {Ans=0;bridge=0;memset (head,-1,sizeof (Head));}    void Add (int u,int v) {edge[ans].beg=u;    Edge[ans].end=v; edge[ans].next=head[u];head[u]=ans++;} void Getmap () {int A,b;while (m--) {scanf ("%d%d", &a,&b); add (b); add (b,a);}} void Tarjan (int u,int fa) {int v,i;low[u]=dfn[u]=++dfsclock;instack[u]=1;s.push (u); int Flag=1;for (i=head[u];i!=-1;i=    Edge[i].next) {v=edge[i].end;if (FLAG&AMP;&AMP;V==FA)//for heavy side {flag=0;continue;} if (!dfn[v]) {TARjan (V,u); Low[u]=min (Low[u],low[v]); if (dfn[u]<low[v]) bridge++;}    else if (Instack[v]) low[u]=min (Low[u],dfn[v]);}    if (Dfn[u]==low[u]) {scccnt++;    while (1) {v=s.top ();     S.pop (); instack[v]=0;sccno[v]=scccnt;if (v==u) break; }}}void Find () {int I;memset (low,0,sizeof (Low)), memset (sccno,0,sizeof (SCCNO)); Memset (Dfn,0,sizeof (DFN)); Memset ( Instack,0,sizeof (instack));d fsclock=scccnt=0;for (i=1;i<=n;i++) {if (!dfn[i]) Tarjan (i,-1);}} void Suodian () {int u,v,i,j;for (i=1;i<=scccnt;i++) newmap[i].clear (); for (i=0;i<ans;i=i+2) {u=sccno[edge[i]. Beg];v=sccno[edge[i].end];if (u!=v) {newmap[u].push_back (v); Newmap[v].push_back (U);}}}    void BFs (int beg) {queue<int>q;memset (dis,0,sizeof (dis)); memset (vis,0,sizeof (VIS)); int I,j;while (!q.empty ()) Q.pop (); Sum=0;age=beg;vis[beg]=1;q.push (beg); int u;while (!q.empty ()) {U=q.front (); Q.pop (); for (I=0;i<newmap[u] . Size (); i++) {if (!vis[newmap[u][i]]) {Dis[newmap[u][i]]=dis[u]+1;vis[newmap[u][i]]=1;q.push (newmap[u][i]); if (sum <dis[newMap[u][i]]) {sum=dis[newmap[u][i]];age=newmap[u][i];}}}} void Solve () {int I,j;bfs (1), BFS (age);p rintf ("%d\n", Bridge-sum),//printf ("%d\n%d\n", Bridge,sum);} int main () {while (scanf ("%d%d", &n,&m), n|m) {init (); Getmap (); find ();//printf ("%d#\n", bridge); Suodian (); Solve ();} return 0;}

  

Hdoj 4612 Warm up "double-connected components for bridge && indent building new graph to find the diameter of the tree"

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.