poj3041

                                                                                               Asteroids
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12270   Accepted: 6679

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

 

 

 

#include <iostream>  
    using namespace std;  
    const int N = 510; //定义 顶点最大个数  
    bool map[N][N],used[N]; //保存 顶点 边关系  
    int link[N]; //记录 与Xi关联的Yi元素 line[Yj] = Xi,-1表示未关联   
    int n,k; //定义 顶点数 与边数  
    bool DFS(int t) //检验顶点t 是否可以增广  
    {  
        for(int i=1; i<=n; i++) //Y中所有顶点 i  
        {  
            if(!used[i] && map[t][i]) //如果顶点i 未被使用,并且t与i有边相连  
            {  
                used[i] = true; //将顶点i标记  
                if(link[i]==-1 || DFS(link[i]))//如果顶点i 没有 被标记的边连接到 t,  
                                               //或者 i与X中的顶点link[i]构成的边被标记 但X的顶点link[i]可以增广  
                {  
                    link[i] = t;//修改边的标记方式,使顶点t与i构成的边关系被标记;  
                    return true;//可以增广  
                }  
            }  
        }  
        return false;  
    }  
    int main()  
    {  
        int i,x,y,ans=0;  
        memset(link, -1, sizeof(link));//初始化  
        memset(map, false, sizeof(map));  
        scanf("%d %d", &n, &k);  
        for(i=0; i<k; i++)  
        {  
            scanf("%d %d", &x, &y);  
            map[x][y] = true;  
        }  
        for(i=1; i<=n; i++)//选择一个顶点Xi  
        {  
            memset(used, false, sizeof(used));//将 所有X顶点设为未标志  
            if(DFS(i)) ans++;//如果Xi可以增广,匹配数加1  
        }  
        printf("%d\n", ans);  
        return 0;  
    }  

 

posted @ 2013-08-05 15:05  哥的笑百度不到  阅读(182)  评论(0编辑  收藏  举报