HDU 2878 great world of goo

Source: Internet
Author: User

Hdu_2878

To tell the truth about this question, I feel that this question has a good idea of solving the problem, but the description of the question is correct ,&@! # $ &*@! *&! @#&*! @#...... &*! @ # * (¥ @!)!!!!!!!

Looking at this figure, we can see why I want to explain the problem ......

First, there are two core elements in the question: ball and triangle. It is not difficult to find that the number of balls-2 is the number of triangles. If we encode the positions where the ball can be placed in a certain order, we will find that a continuous triangle will get a continuous position, so the problem becomes that a ball with a continuous number can be placed in a continuous position with a certain number, this is like finding the maximum number of matches, but it seems that the number of matches is not directlyAlgorithmSo use search.

For the time being, explain K A1 B1 A2 B2... Ak bk p C1 D1 C2 D2... What does cp dp mean, such as 2 1 2 4 5 1 4 6, this means that the balls labeled 1, 2, 4, and 5 can be placed in triangles labeled 4, 5, and 6, if it is converted to a position, the balls can be placed in the positions 4, 5, 6, 7, and 8. There is also the and all wills are satisfied Statement, which is purely nonsense. If all will be satisfied, it will be wa. In fact, only one will be satisfied, it is equivalent to taking the "will" in the question as the Union, rather than the intersection.

The next step is a good part of my thinking. I am only able to solve the problem. (the only thing that hurts is that there is no way to solve the problem, so it was not until this morning that the question was enumerated ......). Create a sparse matrix of N * n rows and 2 * n columns. Each row indicates a specific solution where a ball is placed in a certain position. If this ball can be placed in this position, set the two columns in this row to 1. One column indicates the ball and the other column indicates the position of the ball, after the matrix is created, it is converted into a model that selects certain rows so that each column has only one value. You can use the dancing links method.

# Include <stdio. h> # Include < String . H> # Include <Algorithm> # Define Maxm 1010 # Define Maxn 250010 # Define Maxd 501010 # Define INF 0x3f3f3f Int N, m, K, P, [ 510 ], B [ 510 ], G [ 510 ] [ 510  ];  Int  Size, U [maxd], d [maxd], L [maxd], R [maxd], C [maxd];  Int  S [maxm], H [maxn], ans; Void Read ( Int X, Int *, Int  C ){  Int  I, j, X, Y, N; memset (,  0 , Sizeof ( Int ) * (N + 1  ));  For (I = 0 ; I <X; I ++) {Scanf (  "  % D  " , & X ,& Y );  If (X> Y) STD: swap (x, y );  If (C) Y + = 2  ;  For (J = x; j <= y; j ++) A [J] = 1  ;}}  Void Prep (Int N, Int  M ){  Int  I;  For (I = 0 ; I <= m; I ++ ) {R [I] = I + 1 , L [I + 1 ] = I; U [I] = D [I] = I, S [I] = 0  ;} R [m] = 0 , Size = M;  For (I = 0 ; I <= N; I ++) H [I] =- 1  ;}  Void Insert ( Int R, Int  C ){ ++ Size; C [size] = C, ++ S [c]; d [size] = D [c], U [size] = C, U [d [c] = size, d [c] = Size;  If (H [R] =-1 ) L [size] = R [size] = size, H [R] = Size;  Else L [size] = H [R], R [size] = R [H [R], L [R [H [R] = size, R [H [R] = Size ;}  Void  Init (){  Int  I, J, K; scanf (  "  % D  " , & N ,& M); memset (G,  0 , Sizeof (G ));  For (I = 0 ; I <m; I ++ ) {Scanf (  "  % D  " , & K), read (K,, 0  ); Scanf (  "  % D  " , & P), read (p, B, 1  );  For (J =1 ; J <= N; j ++ )  If  (A [J])  For (K = 1 ; K <= N; k ++ )  If (B [k]) g [J] [k] = 1  ;} Prep (n * N, n + N );  For (I = 1 ; I <= N; I ++)  For (J = 1 ; J <= N; j ++ )  If (G [I] [J]) insert (I- 1 ) * N + J, I), insert (I- 1 ) * N + J, N + J );}  Void Remove ( Int  C ){  Int  I, J; R [L [c] = R [c], L [R [c] =L [c];  For (I = d [c]; I! = C; I = D [I])  For (J = R [I]; J! = I; j = R [J]) U [d [J] = U [J], d [U [J] = d [J], -- S [C [J];}  Void Resume ( Int  C ){  Int  I, J;  For (I = U [c]; I! = C; I = U [I]) For (J = L [I]; J! = I; j = L [J]) U [d [J] = J, d [U [J] = J, ++ S [C [J]; R [L [c] = C, L [R [c] = C ;}  Void  Dance (){  If (R [ 0 ] = 0  ){ ++ Ans;  Return  ;}  Int I, j, t = INF, ID;  For (I = R [ 0 ]; I! = 0 ; I = R [I])  If (S [I] <t) t = s [I], id = I; remove (ID );  For (I = d [ID]; I! = ID; I = D [I]) {  For (J = R [I]; J! = I; j = R [J]) Remove (C [J]); Dance ();  If (ANS>65535 ) Return  ;  For (J = L [I]; J! = I; j = L [J]) Resume (C [J]);} resume (ID );}  Void  Solve () {ans = 0  ; Dance ();  If (ANS> 65535 ) Printf ( "  Overflow! \ N  " );  Else Printf ( "  % D \ n  "  , ANS );}  Int  Main (){  Int  T; scanf (  "  % D  " ,& T );  While (T -- ) {Init (); 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.