HDU 1054 strategic game (minimum vertex overwrite in a bipartite graph)

Source: Internet
Author: User
Strategic game

Time Limit: 20000/10000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 2916 accepted submission (s): 1217

Problem descriptionbob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. now he has the following problem. he must defend a medieval city, the roads of which form a tree. he has to put the minimum number of soldiers on the nodes so that they can observe all the edges. can you help him?

Your program shocould find the minimum number of soldiers that Bob has to put for a given tree.

The input file contains several data sets in text format. Each data set represents a tree with the following description:

The number of nodes
The description of each node in the following format
Node_identifier :( number_of_roads) node_identifier1 node_identifier2... node_identifier
Or
Node_identifier :( 0)

The node identifiers are integer numbers between 0 and n-1, for N nodes (0 <n <= 1500). Every edge appears only once in the input data.

For example for the tree:

The solution is one soldier (at the node 1 ).

The output shoshould be printed on the standard output. for each given input data set, print one integer number in a single line that gives the result (the minimum number of soldiers ). an example is given in the following table:

 

Sample input4 0 :( 1) 1 1 :( 2) 2 3 2 :( 0) 3 :( 0) 5 3 :( 3) 1 4 2 1 :( 1) 0 2 :( 0) 0 :( 0) 4 :( 0)

 

Sample output1 2

 

Sourcesoutheastern Europe 2000

 

Recommendjgshining is the minimum vertex overwrite of a bipartite graph. There are more than 1500 points. Normal Hungary Algorithm Will time out. You can use an adjacent table to implement the Hungary algorithm. However, I enabled the hopcroft-carp algorithm in the binary match. Special processing points. It seems that the tree-like DP can also be used. Wait and learn.
 /*  HDU 1054 strategic game is to find the minimum point to overwrite. The maximum number of matching points in a bipartite graph is equal to the minimum number of vertices covered in this graph. However, we need to extend the matching node to n-n, an undirected graph. Therefore, the division of the binary matching number in 2 is the answer. There are 1500 nodes. The normal Hungary algorithm should time out. Enable hopcroft-carp algorithms. G ++ 6156 Ms 9428 K  */  # Include <Stdio. h> # Include <Algorithm> # Include < String . H> # Include <Iostream> # Include <Queue> Using   Namespace  STD;  /*  **************************************** * *** Bipartite Graph Matching (hopcroft-carp algorithm ). Initialization: G [] [] calling of the adjacent matrix: res = maxmatch (); NX, NY needs to be initialized !!! Time Complexity: O (V ^ 0.5 E) this method is applicable to the header file ********************************* required for binary matching with large data ****************************** ****************  */  Const  Int Maxn = 1505  ;  Const   Int INF = 1 < 28  ;  Int  G [maxn] [maxn], MX [maxn], my [maxn], NX, NY;  Int  DX [maxn], Dy [maxn], DIS;  Bool  VST [maxn];  Bool Searchp () {queue < Int > Q; DIS = INF; memset (dx, - 1 , Sizeof  (Dx); memset (dy, - 1 , Sizeof  (Dy ));  For ( Int I = 0 ; I <NX; I ++)  If (MX [I] =- 1  ) {Q. Push (I); DX [I] = 0  ;}  While (! Q. Empty ()){  Int U = Q. Front (); q. Pop ();  If (Dx [u]> dis) Break  ;  For (Int V = 0 ; V <NY; V ++ )  If (G [u] [v] & dy [v] =- 1  ) {Dy [v] = DX [u] + 1  ;  If (My [v] =- 1 ) Dis = Dy [v];  Else  {DX [my [v] = Dy [v] +1  ; Q. Push (my [v]) ;}}  Return Dis! = INF ;}  Bool DFS ( Int  U ){  For ( Int V = 0 ; V <NY; V ++ )  If (! VST [v] & G [u] [v] & dy [v] = DX [u] + 1 ) {VST [v] = 1  ;  If (My [v]! =- 1 & Dy [v] = dis) Continue  ;  If (My [v] =- 1 | DFS (my [v]) {my [v] = U; MX [u] = V;  Return  1  ;}}  Return   0  ;}  Int  Maxmatch (){  Int Res = 0  ; Memset (MX, - 1 , Sizeof  (MX); memset (my, - 1 , Sizeof (My ));  While  (Searchp () {memset (VST,  0 , Sizeof  (VST ));  For ( Int I = 0 ; I <NX; I ++ )  If (MX [I] =- 1 & DFS (I) RES ++ ;}  Return Res ;}  //  **************************************** **********************************/  Int  Main (){  //  Freopen ("in.txt", "r", stdin );  //  Freopen ("out.txt", "W", stdout );      Int  N;  Int  U, K, V;  While (Scanf ( " % D  " , & N )! = EOF) {memset (G,  0 , Sizeof  (G ));  For ( Int I = 0 ; I <n; I ++ ) {Scanf (  "  % D :( % d)  " , & U ,& K ); While (K -- ) {Scanf (  "  % D  " ,& V); G [u] [v] = 1  ; G [v] [u] = 1  ;} NX = Ny = N;  Int Ans = Maxmatch (); printf (  " % D \ n  " , ANS/ 2  );}  Return   0  ;} 

 

 

 

The following is a Hungarian algorithm that uses vector to establish an adjacent table. Higher efficiency than above

 /*  HDU 1054 uses vector in STL to establish an adjacent table to achieve a relatively high efficiency of the Hungarian algorithm. g ++ 578 Ms 580 K  */  # Include <Stdio. h> # Include <Iostream> # Include <Algorithm> # Include < String . H> # Include <Vector> Using   Namespace  STD;  //  **************************************** ********  Const   Int Maxn = 1505  ;  Int  Linker [maxn]; Bool  Used [maxn]; vector < Int > Map [maxn];  Int  UN;  Bool DFS ( Int  U ){  For ( Int I = 0 ; I <map [u]. Size (); I ++ ){  If (!Used [map [u] [I]) {used [map [u] [I] = True  ;  If (Linker [map [u] [I] =- 1 | DFS (linker [map [u] [I]) {linker [map [u] [I] = U;  Return   True  ;}}}  Return   False  ;} Int  Hungary (){  Int  U;  Int Res = 0  ; Memset (linker, - 1 , Sizeof  (Linker ));  For (U = 0 ; U <UN; U ++ ) {Memset (used,  False , Sizeof (Used ));  If (DFS (u) RES ++ ;}  Return  Res ;}  //  **************************************** *************  Int  Main (){  Int  U, K, V;  Int  N;  While (Scanf ( " % D  " , & N )! = EOF ){  For ( Int I = 0 ; I <maxn; I ++ ) Map [I]. Clear ();  For ( Int I = 0 ; I <n; I ++ ) {Scanf (  "  % D :( % d)  " , & U ,&K );  While (K -- ) {Scanf (  "  % D  " ,& V); map [u]. push_back (V); map [v]. push_back (U);} Un = N; printf (  "  % D \ n  " , Hungary ()/ 2  );} 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.