HDU 1827 summer holiday // Tarjan contraction point

Source: Internet
Author: User
/*  Question: I heard that lcy helped you book a 7-day tour of the new Mattel. wiskey was so happy that he could not stay up late. He wanted to tell you the news quickly, even though he had contact information from everyone, however, one contact once took too much time and telephone fees. He knows that others also have contact information, so that he can notify others and ask others to help notify others. Can you help wiskey calculate the minimum number of people to be notified, and the minimum number of phone bills can be notified to everyone? Analysis: because other people can notify people they know, this graph can be converted into a point reduction graph using a strongly connected component, and all the strongly connected branches become a point reduction graph, the minimum number of people who need to be notified is the minimum number of people who need to be notified. Then, you can enumerate whether the scale-in of all people is 0. If yes, update the minimum cost required for the scale-in.  */  # Include <Iostream> # Include <Cstdio> # Include <Cstring> # Include <Vector> Using   Namespace STD;  Const   Int V = 1005  ;  Const   Int INF = 1e9; vector < Int > Adj [v];  Int  Cost [v], n, m;  Int  Dfn [v], low [v], Father [v], stack [v], bcnt, depth, top;  Bool Instack [v];  Int  Id [v], CNT [v];  Void DFS ( Int U) //  Tarjan contraction point  {Instack [u] = True  ; Low [u] = Dfn [u] = ++ Depth; stack [ ++ Top] = U;  Int  V;  Int Len = Adj [u]. Size ();  For ( Int I = 0 ; I <Len; I ++ ) {V = Adj [u] [I];  If (! Low [v]) {DFS (V); low [u] = Min (low [u], low [v]);}  Else   If  (Instack [v]) low [u] =Min (low [u], dfn [v]);}  If (Dfn [u] = Low [u]) { ++ Bcnt;  Do  {V = Stack [top -- ]; Instack [v] = False  ; Father [v] = Bcnt ;}  While (U! = V );}}  Void Solve () {depth = Top = bcnt = 0  ; Memset (instack,  False , Sizeof  (Instack); memset (low,  0 , Sizeof  (Low ));  For ( Int I = 1 ; I <= N; I ++ )  If (!Low [I]) DFS (I); memset (ID,  0 , Sizeof  (ID ));  Int  V, Len;  For ( Int I = 1 ; I <= N; I ++) //  Count the inbound of all shrinkage points  {Len = Adj [I]. Size ();  For (Int J = 0 ; J <Len; j ++ ) {V = Adj [I] [J];  If (Father [I]! = Father [v]) ID [Father [v] ++ ;}}  Int Ci = 0  ;  Int Ans = 0  ;  For ( Int I = 1 ; I <= bcnt; I ++) //  The number of Shrinkage points is bcnt.  {  If (ID [I] = 0 ) //  Count the number of input 0 CI ++ ; CNT [I] = INF ;}  For ( Int I = 1 ; I <= N; I ++ ) {V = Father [I];  If (ID [v] = 0  ) CNT [v] = Min (CNT [v], cost [I]); //  Update fee  }  For ( Int I = 1 ; I <= bcnt; I ++ )  If (CNT [I]! =INF) ans + = CNT [I]; printf (  "  % D \ n  "  , CI, ANS );}  Int  Main () {freopen (  "  Sum. In  " , "  R  "  , Stdin); freopen (  " Sum. Out  " , "  W  "  , Stdout );  Int  X, Y;  While (Scanf ( "  % D  " , & N, & M )! = EOF ){  For ( Int I = 1 ; I <= N; I ++ ) {Scanf (  "  % D  " ,& Cost [I]); adj [I]. Clear ();}  For ( Int I = 1 ; I <= m; I ++ ) {Scanf (  "  % D  " , & X ,& Y); adj [X]. push_back (y);} 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.