Hoj 1520 the bottom of a graph // poj 2553 the bottom of a graph

Source: Internet
Author: User
/*  After the strongly connected component is found to be reduced to a point, the following Tarjan is used to calculate all points with a given degree of 0.AlgorithmThe first is to use the adjacent linked list of the struct, and the second is to use the vector as the adjacent linked list.  */  # Include <Cstdio> # Include <Cstring> Const   Int X = 15002  ;  Int  Dfn [X], low [X], stack [X], Father [X], depth, top, bcnt;  Int  Counter [X], n, m; Bool  Instack [x];  Struct  Node {  Int  V; Node * Next;  Void  Fun () {v = 0  ; Next = Null ;}} edge [X], * Head [X], * TMP;  Void Tarjan ( Int U ){  Int  V; dfn [u] = Low [u] = ++ Depth; stack [ ++ Top] = U; instack [u] = True  ;  For (Node * P = head [u]; P = p-> Next) {v = P-> V;  If (! Low [v]) {Tarjan (v );  If (Low [v] <Low [u]) low [u] = Low [v];}  Else   If (Instack [v] & low [u]> Dfn [v]) low [u] = Dfn [v];}  If (Low [u] = Dfn [u]) {bcnt ++ ;  Do  {V = Stack [top -- ]; Instack [v] =False  ; Father [v] = Bcnt ;}  While (U! = V );}}  Void  Solve () {top = Depth = bcnt = 0  ; Memset (instack,  False , Sizeof  (Instack); memset (low,  0 , Sizeof (Low); memset (counter,  0 , Sizeof  (Counter ));  For ( Int I = 1 ; I <= N; I ++ )  If (! Low [I]) Tarjan (I );  For ( Int I = 1 ; I <= N; I ++ ) For (Node * P = head [I]; P = p-> Next)  If (Father [I]! = Father [p-> V]) Counter [Father [I] ++ ;  Bool Flag = False  ;  For ( Int I = 1 ; I <= N; I ++ )  If (!Counter [Father [I]) {  If  (FLAG) printf (  "   "  );  Else  Flag = True  ; Printf (  "  % D  "  , I);} printf (  " \ N  "  );}  Int  Main () {freopen (  "  Sum. In  " , "  R  "  , Stdin); freopen (  "  Sum. Out  " , "  W "  , Stdout );  Int  U, V;  While (Scanf ( "  % D  " ,& N), n) {scanf (  "  % D  " ,& M); memset (Head, null,  Sizeof  (Head ));  For ( Int I = 0 ; I <m; I ++ ) Edge [I]. Fun (); TMP = Edge;  For ( Int I = 0 ; I <m; I ++ ) {Scanf (  "  % D  " , & U ,& V); TMP -> Next = Head [u]; TMP -> V = V; head [u] = TMP ++ ;} Solve ();}  Return   0  ;}  /*  Vector as an adjacent linked list  */  # Include <Vector> # Include <Cstdio> # Include <Cstring> Using   Namespace  STD; Const   Int X = 15005  ;  Int  Dfn [X], low [X], Father [X], stack [X], bcnt, top, depth;  Int  Counter [X], n, m;  Bool  Instack [X]; vector < Int > Adj [x];  Void Tarjan ( Int  U) {LOW [u] = Dfn [u] = ++ Depth; instack [u] = True  ; Stack [ ++ Top] = U;  Int V, Len = Adj [u]. Size ();  For ( Int I = 0 ; I <Len; I ++ ) {V = Adj [u] [I];  If (!Low [v]) {Tarjan (V); low [u] = Min (low [u], low [v]);}  Else   If  (Instack [v]) low [u] = Min (low [u], dfn [v]);}  If (Low [u] = Dfn [u]) { ++ Bcnt;  Do  {V = Stack [top -- ]; Instack [v] =False  ; Father [v] = Bcnt ;}  While (U! = V );}}  Void  Solve () {depth = Top = bcnt = 0  ; Memset (low,  0 , Sizeof  (Low); memset (instack,  False , Sizeof (Instack); memset (counter,  0 , Sizeof  (Counter ));  For ( Int I = 1 ; I <= N; I ++ )  If (! Low [I]) Tarjan (I );  Int  Len;  For ( Int I =1 ; I <= N; I ++ ) {Len = Adj [I]. Size ();  For ( Int J = 0 ; J <Len; j ++ )  If (Father [I]! = Father [adj [I] [J]) Counter [Father [I] ++ ;}  Bool Flag = False  ; For ( Int I = 1 ; I <= N; I ++ )  If (! Counter [Father [I]) {  If  (FLAG) printf (  "   "  );  Else  Flag = True ; Printf (  "  % D  "  , I);} printf (  "  \ N  "  );}  Int  Main () {freopen (  "  Sum. In  " , "  R  " , Stdin); freopen (  "  Sum. Out  " , "  W  "  , Stdout );  Int  U, V;  While (Scanf ( "  % D  " ,& N), n) {scanf (  " % D  " ,& M );  For ( Int I = 1 ; I <= N; I ++ ) Adj [I]. Clear ();  For ( Int I = 0 ; I <m; I ++ ) {Scanf (  "  % D  " , & U ,&V); adj [u]. push_back (V);} solve ();}  Return   0  ;} 

 

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.