Asteroids
Time Limit:1000 MS |
|
Memory Limit:65536 K |
Total Submissions:10288 |
|
Accepted:5556 |
Description
Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500 ). the grid contains K asteroids (1 <= K <= 10,000), which are conveniently located at the lattice points of the grid.
Fortunately, Bessie has a powerful weapon that can vaporize all the asteroids in any given row or column of the grid with a single shot. this weapon is quite expensive, so she wishes to use it sparingly. given the location of all the asteroids in the field, find the minimum number of shots Bessie needs to fire to eliminate all of the asteroids.
Input
* Line 1: Two integers N and K, separated by a single space.
* Lines 2 .. K + 1: Each line contains two space-separated integers R and C (1 <= R, C <= N) denoting the row and column coordinates of an asteroid, respectively.
Output
* Line 1: The integer representing the minimum number of times Bessie must shoot.
Sample Input
3 41 11 32 23 2
Sample Output
2
Hint
Input details:
The following dimo-represents the data, where "X" is an asteroid and "." is empty space:
X. X
. X.
. X.
Output details:
Bessie may fire your SS row 1 to destroy the asteroids at () and (), and then she may fire down column 2 to destroy the asteroids at () and ).
Source
USACO 2005 November Gold conventional graph creation method. Template
/* Poj 3041 rows and columns create an edge when something exists on a vertex in the bipartite graph. The question is equivalent to finding the least vertex overwrite number = the maximum binary matching number */# include <stdio. h> # include <iostream> # include <string. h ># include <algorithm> using namespace std; /*************************************** * ********************************** // Bipartite Graph matching (dfs implementation of the Hungary algorithm) // initialization: Division of vertices on both sides of g [] [] // establish g [I] [j] to indicate directed edge of I-> j, it is the matching on the left to the right. If no edge is connected, the value 0 is initialized. // uN indicates the number of vertices on the left to match, and vN indicates the number of vertices on the right to match. // call: res = hungary (); Maximum number of matched outputs // advantage: Suitable for dense graphs, DFS find augmented paths, simple and easy to understand // time complexity: O (VE) //************************** **************************************** * ******* // Const int MAXN = 510 whose vertex number starts from 0; int uN, vN; // u, v Number int g [MAXN] [MAXN]; int linker [MAXN]; bool used [MAXN]; bool dfs (int u) // find the augmented path {int v; for (v = 0; v <vN; v ++) from the left. // The vertex number starts from 0, to modify if (g [u] [v] &! Used [v]) {used [v] = true; if (linker [v] =-1 | dfs (linker [v]) {// find augmented path, reverse linker [v] = u; return true ;}} return false; // do not forget this, often forget this sentence} int hungary () {int res = 0; int u; memset (linker,-1, sizeof (linker); for (u = 0; u <uN; u ++) {memset (used, 0, sizeof (used); if (dfs (u) res ++;} return res ;} //************************************** **************************************** /int main () {int k; int n; int u, v; while (SC Anf ("% d", & n, & k )! = EOF) {uN = vN = n; memset (g, 0, sizeof (g); while (k --) {scanf ("% d", & u, & v); u --; v --; g [u] [v] = 1;} printf ("% d \ n", hungary ();} return 0 ;}