(POJ 3694) Network to find the number of bridges

Source: Internet
Author: User

Title Link: http://poj.org/problem?id=3694

Descriptiona network administrator manages a large network. The network consists of N computers and M links between pairs of computers. Any pair of computers is connected directly or indirectly by successive links, so data can is transformed between any of Computers. The administrator finds that some links is vital to the network, because failure of any one of the them can cause that data C an't be transformed between some computers. He call such a link a bridge. He is planning to add some new links one by one to eliminate all bridges.Administrator by reporting the number of bridgesinchThe network after eachNewLink isadded. Inputthe input consists of multiple test cases. Each test CaseStarts with a line containing and integers N (1≤n≤ -, the) and M (N-1≤m≤ $, the). Each of the following M lines contains, integers A and B (1≤A≠B≤N), which indicates a link between computer A and B. Computers is numbered from 1To N. It isGuaranteed that any, computers is connectedinchThe initial network. The next line contains a single integer Q (1≤q≤1, the), which isThe number ofNewLinks the administrator plans to add to the network one by one . The I-th line of the following Q lines contains the integer A and B (1≤a≠b≤n), which isThe i-th addedNewlink connecting computer A and b.the last Test Case isfollowed by a line containing and zeros. Outputfor each test Case, print a line containing the test CaseNumber (beginning with1) and Q lines, the i-th of which contains a integer indicating the number of bridgesinchThe network after the first INewLinks are added. Print a blank line after the output forEach test Case. Sample Input3 21 22 321 21 34 41 22 12 31 421 23 40 0Sample outputcase1:10 Case2:20

The main topic: There are n points, M-edge, add Q-side, ask each fill an edge, there are several bridges?

#include <stdio.h>#include<cstdlib>#include<cstring>#include<algorithm>#include<queue>#include<math.h>#include<stack>using namespacestd;#definell Long Long#defineINF 0x3f3f3f3f#defineMet (b) memset (A,b,sizeof (a))#defineMoD 2147493647#defineN 100100intLow[n],dfn[n],vis[n];intFa[n];intN,t,ans;intBin[n];vector<vector<int> >Q;voidInit () {Met (Low,0); Met (DFN,0); T=0;    Q.clear (); Q.resize (n+Ten); Met (Bin,0); Met (FA,0);}voidTarjin (intUintf) {Low[u]=dfn[u]=++T; Fa[u]=F; intL=q[u].size ();  for(intI=0; i<l; i++)    {        intv=Q[u][i]; if(!Dfn[v])            {Tarjin (v,u); Low[u]=min (low[u],low[v]); if(Dfn[u]<low[v])///It's not looking, it could be a bridge.{Bin[v]++; Ans++; }        }        Else if(f!=v) {Low[u]=min (low[u],dfn[v]); if(Dfn[u]<low[v])///have been looked up, there are two ways to pass this point, not the bridge{Bin[v]--; Ans--; }        }    }}voidLCR (intAintb) {    if(a==b)return ; if(dfn[a]<Dfn[b]) {        if(bin[b]==1) {Bin[b]=0; Ans--;    } LCR (A,fa[b]); }    Else    {        if(bin[a]==1) {Bin[a]=0; Ans--;    } LCR (FA[A],B); }}intMain () {intm,e,f; intcot=1, q;  while(SCANF ("%d%d", &n,&m), n+m) {init (); Ans=0;  for(intI=0; i<m; i++) {scanf ("%d%d",&e,&f);            Q[e].push_back (f);        Q[f].push_back (e); } Tarjin (1,-1); scanf ("%d",&q); printf ("Case %d:\n", cot++);  while(q--) {scanf ("%d%d",&e,&f);            LCR (E,F); printf ("%d\n", ans); }    }    return 0;}

(POJ 3694) Network to find the number of bridges

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.