POJ 3041 —— 最小顶点覆盖

Asteroids
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12929   Accepted: 7035

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的网格,其中有K个小行星,告诉你他们的坐标,你有两种操作:干掉整行或整列的小行星。问你把所有小行星都干掉的最小步数

思路是我们把操作当做顶点,小行星的坐标当做边。这样,相当于求一个顶点集S,每条边至少有一个属于S的顶点。

典型的最小顶点覆盖,由于是二分图,最小顶点覆盖 = 最大匹配。

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 250000 + 50;
const int MAXS = 10000 + 50;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
const int inf = 1 << 30;
#define eps 1e-8
const long long MOD = 1000000000 + 7;
const int mod = 100000;
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pii;
#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
int n , k;
int R[MAXS] , C[MAXS];
int V;
vector <int> G[MAXN];
int match[MAXN];
bool used[MAXN];
void add_edge(int u , int v)
{
    G[u].push_back(v);
    G[v].push_back(u);
}
bool dfs(int v)
{
    used[v] = true;
    for(int i = 0 ; i < G[v].size() ; i++)
    {
        int u = G[v][i] , w = match[u];
        if(w < 0 || !used[w] && dfs(w))
        {
            match[v] = u;
            match[u] = v;
            return true;
        }
    }
    return false;
}
int bipartite_matching()
{
    int res = 0;
    memset(match , - 1  , sizeof(match));
    for(int v = 0 ; v < V ; v++)
    {
        if(match[v] < 0)
        {
            memset(used , 0 , sizeof(used));
            if(dfs(v))res++;
        }
    }
    return res;
}
void solve()
{
    V = n * 2;
    //    for(int i = 0 ; i < V ;  i++)G[i].clear();
    for(int i = 0 ; i < k ; i++)
    {
        add_edge(R[i] - 1 , n + C[i] - 1);
    }
    printf("%d\n" ,bipartite_matching());
}
int main()
{
    while(~scanf("%d%d" , &n , &k))
    {
        for(int i = 0 ; i < k ; i++)
        {
            scanf("%d%d",&R[i] , &C[i]);
        }
        solve();
    }
    return 0;
}


你可能感兴趣的:(ACM,二分图,最小顶点覆盖)