Question address: Asteroids
Question:
Give you a grid of N * n. In the coordinates of the given stars, the operation is to eliminate a whole row or a column of stars each time and ask you the minimum number of operations required.
Solution:
The key is to think about binary matching. You can draw a picture first, and create the coordinate rows and columns of the star position, (1-> 1, 1-> 3)/(2-> 2)/(3-> 2 ). how can we minimize the elimination of all edges by removing all the stars? Here we need to think of the most connected vertices, because all the edges are aggregated to a point that they can be eliminated at the same time. Therefore, you only need to find the minimum vertex to overwrite all the edges, and the minimum vertex overwrite of the second vertex is equal to the maximum number of matches. (Proof: http://wenku.baidu.com/link? URL =_T06jkRwGZphG1B6EDu8WCRnn_5OoIP41WAeasBO72gVinsurM-SDIY2aKY1AeotG8zzW6LsjD49WmYWXfBBlhn0PDlB4vh_ZuHo9h2c-x _)
Hungary algorithm.
Code:
1 # include <algorithm> 2 # include <iostream> 3 # include <sstream> 4 # include <cstdlib> 5 # include <cstring> 6 # include <cstdio> 7 # include <string> 8 # include <bitset> 9 # include <vector> 10 # include <queue> 11 # include <stack> 12 # include <cmath> 13 # include <list> 14/ /# include <map> 15 # include <set> 16 using namespace STD; 17 /************************************** */18 # define ll long long19 # define in T64 _ int6420 /*********************************** * ***/21 const int INF = 0x7f7f7f7f; 22 const double EPS = 1e-8; 23 const double PIE = ACOs (-1.0); 24 const int d1x [] = {0,-, 1 }; 25 const int d1y [] = {-,}; 26 const int d2x [] = {0,-, 1}; 27 const int d2y [] =, -}; 28 const int FX [] = {-1,-1,-,}; 29 const int FY [] = {-, 1, -,-, 1 }; 30 /************************************** */31 void o Penfile () 32 {33 freopen ("data. in "," rb ", stdin); 34 freopen (" data. out "," WB ", stdout); 35} 36/************************* gorgeous split line, the above is the template section ****************/37 # define maxn 50538 int NX, NY; 39 int G [maxn] [maxn]; 40 int CX [maxn], CY [maxn]; 41 int vis [maxn]; 42 int path (int u) 43 {44 int V; 45 for (V = 0; v <NX; V ++) 46 If (G [u] [v] &! Vis [v]) 47 {48 vis [v] =-1; 49 If (CY [v] =-1 | path (CY [v]) 50 {51 CX [u] = V; 52 CY [v] = u; 53 return 1; 54} 55} 56 return 0; 57} 58 int maxmatch () 59 {60 int CNT = 0; 61 memset (CX,-1, sizeof (CX); 62 memset (CY,-1, sizeof (CY )); 63 for (INT I = 0; I <NX; I ++) 64 {65 memset (VIS, 0, sizeof (VIS); 66 CNT + = path (I ); 67} 68 return CNT; 69} 70 int main () 71 {72 int m; 73 while (scanf ("% d", & NX, & M )! = EOF) 74 {75 int I, j; 76 int X, Y; 77 int min = 0; 78 memset (G, 0, sizeof (g )); 79 for (I = 0; I <m; I ++) 80 {81 scanf ("% d", & X, & Y ); 82g [x-1] [Y-1] = 1; 83} 84 min + = maxmatch (); 85 printf ("% d \ n", min); 86} 87 return 0; 88}
View code