poj 3041 Asteroids

Asteroids
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13022   Accepted: 7093

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 4
1 1
1 3
2 2
3 2

Sample Output

2

Hint

INPUT DETAILS: 
The following diagram represents the data, where "X" is an asteroid and "." is empty space: 
X.X 
.X. 
.X.
 

OUTPUT DETAILS: 
Bessie may fire across row 1 to destroy the asteroids at (1,1) and (1,3), and then she may fire down column 2 to destroy the asteroids at (2,2) and (3,2).

Source

 1 //1164K    32MS    C++    793B    2013-11-09 12:08:18
 2 /*
 3     
 4     题意:
 5         给出一个n*n的图,上面有m个障碍物,每次可以清除整行或整列的障碍物,
 6     问最少要多少次
 7     
 8     二分匹配:
 9         很明显的模板题...求二分图的最大匹配 
10     
11 */
12 #include<stdio.h>
13 #include<string.h>
14 int g[505][505];
15 int match[505];
16 int vis[505];
17 int n,m;
18 int dfs(int x)
19 {
20     for(int i=1;i<=n;i++)
21        if(!vis[i] && g[x][i]){
22            vis[i]=1;
23            if(match[i]==-1 || dfs(match[i])){
24                match[i]=x;
25                return 1;
26            }
27        }
28     return 0;
29 }
30 void hungary()
31 {
32     memset(match,-1,sizeof(match));
33     int ret=0;
34     for(int i=1;i<=n;i++){
35         memset(vis,0,sizeof(vis));
36         ret+=dfs(i);
37     }
38     printf("%d\n",ret); 
39 }
40 int main(void)
41 {
42     int a,b;
43     while(scanf("%d%d",&n,&m)!=EOF)
44     {
45         memset(g,0,sizeof(g));
46         for(int i=0;i<m;i++){
47             scanf("%d%d",&a,&b);
48             g[a][b]=1;
49         }
50         hungary();
51     }
52     return 0;
53 }

 

posted @ 2013-11-09 12:13  heaventouch  阅读(195)  评论(0)    收藏  举报