题目链接:
POJ 3041 Asteroids
题意:
给出m个点的所在行列,每次可以选择清除一行或者一列,问清除这些点的最小操作次数是多少?
分析:
将所有点的行看成一组,所有的点列看成一组,对每个点的行和列建一条边,每条边就代表一个点,清除一行或者一列可以看成选择二分图中的一个点,清除所有的点,就相当于选择二分图中的若干点连接所有的边。
这样一来就符合最小点覆盖的定义了!再根据最小点覆盖数=最大匹配数,利用匈牙利算法即可。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <cassert>
#include <map>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
typedef long long ll;
const int MAX_N = 510;
int n, m, total, cntx, cnty;
int head[MAX_N], vis[MAX_N], match[MAX_N];
struct Edge{
int to, next;
Edge() {}
Edge(int _to,int _next) : to(_to), next(_next) {
}
}edge[MAX_N * MAX_N];
inline void AddEdge(int from, int to)
{
edge[total].to = to;
edge[total].next = head[from];
head[from] = total++;
}
inline bool dfs(int u)
{
for(int i = head[u]; i != -1; i = edge[i].next){
int to = edge[i].to;
if(!vis[to]){
vis[to] = 1;
if(match[to] == -1 || dfs(match[to])){
match[to] = u;
return true;
}
}
}
return false;
}
inline void hungary()
{
int ans = 0;
for(int i = 0; i < cntx; ++ i){
memset(vis,0,sizeof(vis));
if(dfs(i)) ans++;
}
cout << ans << endl;
}
int main()
{
freopen("E.in", "r", stdin);
IOS;
while(cin >> n >> m){
memset(head, -1, sizeof(head));
memset(match, -1, sizeof(match));
cntx = cnty = total = 0;
map<int, int> mpx;
map<int, int> mpy;
for(int i = 0; i < m; ++ i){
int x, y;
cin >> x >> y;
if(mpx.find(x) == mpx.end()){
mpx[x] = cntx++;
}
if(mpy.find(y) == mpy.end()){
mpy[y] = cnty++;
}
AddEdge(mpx[x], mpy[y]);
}
hungary();
}
return 0;
}