1. 程式人生 > >Poj 3041 Asteroids

Poj 3041 Asteroids

truct r+ dfs hint 有一個 現在 The col gate

Asteroids
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 25270 Accepted: 13635

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

USACO 2005 November Gold
//題意:有一個n*n的矩陣,上面有m個黑格子
//        現在有一把刷子,可以將一行或一列上的黑格子刷成白色
//        問最少需要幾刷子可以讓矩陣變成全白
//        格子不是黑的就是白的 

//用橫坐標和縱坐標建立二分圖,如果有黑格子在(x,y)
//那麽就讓左側x連向右側y
//那麽我們就可以得到同行和同列的格子的個數
//然後求最小點覆蓋就可以了 

//最小點覆蓋=最大匹配數

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

inline int read()
{
    char c=getchar();int num=0;
    for(;!isdigit(c);c=getchar());
    for(;isdigit(c);c=getchar())
        num=num*10+c-0;
    return num;
}

const int M=1e4+5;

int n,m;
int match[M];
int vis[M],tim;
int head[M],num_edge;
struct Edge
{
    int v,nxt;
}edge[M];

void add_edge(int u,int v)
{
    edge[++num_edge].v=v;
    edge[num_edge].nxt=head[u];
    head[u]=num_edge;
}

bool dfs(int u)
{
    for(int i=head[u],v;i;i=edge[i].nxt)
    {
        v=edge[i].v;
        if(vis[v]==tim)
            continue;
        vis[v]=tim;
        if(!match[v]||dfs(match[v]))
        {
            match[v]=u;
            return 1;
        }
    }
    return 0;
}

int main()
{
    n=read(),m=read();
    for(int i=1,x,y;i<=m;++i)
    {
        x=read(),y=read();
        add_edge(x,y);
    }
    int ans=0;
    for(int i=1;i<=n;++i)
    {
        ++tim;
        ans+=dfs(i);
    }
    printf("%d",ans);
    return 0;
}

Poj 3041 Asteroids