Asteroids
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:14399 |
|
Accepted:7836 |
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 ).
Okay .. For this type of simple questions, click here .. Deepening investment in reform ..
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespace std;const int M = 1000 + 5;int n, m;int link[M];bool MAP[M][M];bool cover[M];int ans;void init(){ int num; int x; int y; memset(MAP, false, sizeof(MAP)); for(int i=1; i<=m; i++) { scanf("%d%d", &x, &y); MAP[x][y]=true; }}bool dfs(int x){ for(int y=1; y<=n; y++) { if(MAP[x][y] && !cover[y]) { cover[y]=true; if(link[y]==-1 || dfs(link[y])) { link[y]=x; return true; } } } return false;}int main(){ while(scanf("%d%d", &n, &m)!=EOF) { ans=0; init(); memset(link, -1, sizeof(link)); for(int i=1; i<=n; i++) { memset(cover, false, sizeof(cover)); if( dfs(i) ) ans++; } printf("%d\n", ans); } return 0;}