| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 16344 | Accepted: 8906 |
Description
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
* 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
Sample Input
3 4 1 1 1 3 2 2 3 2
Sample Output
2
Hint
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
每个点i(Ri,Ci)要么被x=Ri,要么被y=Ci直线所覆盖。将直线看作图的点,这样可以分为水平的和竖直的两类点,每个小行星对应图中一条边(一个水平坐标和一个竖直坐标确定了一个位置)。每条边只需要被一个顶点覆盖就好了,问题就转化为最小顶点覆盖,因为是二分图,所以求解最大匹配就可以了。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
#define maxn 505
#define maxk 10005
int n, k;
vector<int> g[2*maxn];
int match[2*maxn];
int vis[2*maxn];
int dfs(int u)
{
int v;
for(int i = 0; i < g[u].size(); i++){
v = g[u][i];
if(vis[v]) continue;
if(!match[v]){
match[v] = u;
match[u] = v;
return 1;
}
else{
vis[v] = 1;
if(dfs(match[v])){
match[v] = u;
match[u] = v;
return 1;
}
}
}
return 0;
}
int main()
{
while(~scanf("%d%d", &n, &k)){
int r,c;
for(int i = 0; i < 2*n+1; i++) g[i].clear();
for(int i = 0; i < k; i++){
scanf("%d%d", &r, &c);
g[r].push_back(n+c);
g[n+c].push_back(r);
}
memset(match, 0, sizeof(match));
int ans = 0;
for(int i = 1; i <= n; i++)
if(!match[i]){
memset(vis, 0, sizeof(vis));
ans += dfs(i);
}
printf("%d\n", ans);
}
return 0;
}
277



被折叠的 条评论
为什么被折叠?



