UVA 796-critical Links "Finding Bridges"

Source: Internet
Author: User

link:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&itemid=8&page=show_problem& problem=737

Test instructions
To find the number and edge of the bridge, the order of the points of the output edge is from small to large

Code:

#include <stdio.h>#include <ctime>#include <math.h>#include <limits.h>#include <complex>#include <string>#include <functional>#include <iterator>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <set>#include <map>#include <list>#include <bitset>#include <sstream>#include <iomanip>#include <fstream>#include <iostream>#include <ctime>#include <cmath>#include <cstring>#include <cstdio>#include <time.h>#include <ctype.h>#include <string.h>#include <assert.h>#pragma COMMENT (linker, "/stack:102400000,102400000")using namespace STD;intN, m, x, y;Const intMAXN =1001000;Const intMAXM =1000100;intNumChartmp vector<pair<int, int>> A;structedge{intTo, next;BOOLCut//Is the mark of the bridge}EDGE[MAXM];intHEAD[MAXN], tot;intLOW[MAXN], DFN[MAXN], STACK[MAXN];intIndex, top;BOOLINSTACK[MAXN];BOOLCUT[MAXN];intADD_BLOCK[MAXN];//Delete a point after adding the connected blockintBridgevoidAddedge (intUintV) {edge[tot].to = v; edge[tot].next = head[u]; edge[tot].cut =false; Head[u] = tot++;}voidTarjan (intUintPre) {intV    Low[u] = dfn[u] = ++index;    stack[top++] = u; Instack[u] =true;intson =0; for(inti = Head[u]; I! =-1; i = edge[i].next) {v = edge[i].to;if(v = = Pre)Continue;if(!            Dfn[v]) {son++; Tarjan (V, u);if(Low[u] > Low[v]) Low[u] = Low[v];//Bridge            ( u,v) is a bridge when and only if (U,V) is a branch edge and satisfies the DFS (u) <low (v).             if(Low[v] > Dfn[u])                {bridge++; Edge[i].cut =true; Edge[i ^1].cut =true;            A.push_back (Make_pair (min (U, v), max (U, v))); }//Cutting point            //a vertex u is a cut point, when and only if satisfies (1) or (2) (1) U is a root, and U has more than one subtree.             //(2) U is not a root, and satisfies the presence (U,V) as a branch edge (or parent-child side,            //That is, U is the father of V in the search tree, so dfs (u) <=low (v)            if(U! = Pre && low[v] >= Dfn[u])//Not root{Cut[u] =true;            add_block[u]++; }        }Else if(Low[u] > Dfn[v])    Low[u] = Dfn[v]; }//root, number of branches greater than 1    if(U = = Pre && son >1) Cut[u] =true;if(U = = pre) Add_block[u] = son-1; Instack[u] =false; top--;}voidSolveintN) {memset(DFN,0,sizeof(DFN));memset(Instack,false,sizeof(Instack));memset(Add_block,0,sizeof(Add_block));memset(Cut,false,sizeof(cut)); Index = top =0; Bridge =0; for(inti =1; I <= N; i++)if(!    Dfn[i]) Tarjan (i, I); Sort (A.begin (), A.end ());printf("%d critical links\n", bridge); for(inti =0; I < a.size (); i++)printf("%d-%d\n", A[i].first-1, A[i].second-1);printf("\ n");}voidInit () {tot =0; num =0; A.clear ();memset(Head,-1,sizeof(head));}intMain () { while(~scanf("%d", &n)) {init (); for(inti =1; I <= N; i++) {scanf("%d", &m); M + =1; GetChar ();scanf("%c", &tmp);scanf("%d", &x);scanf("%c", &tmp); while(x--) {scanf("%d", &y); Y + =1;                Addedge (M, y);            Addedge (Y, M);    }} solve (n); }return 0;}

Copyright NOTICE: Reprint please indicate the source.

UVA 796-critical Links "Finding 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.