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).

题意:

给一个N*N的矩阵,一些星球位于一些点上,要求消除所有星球,每次操作可消除一行或一列的星球,求最少要几次。

最小点覆盖定义:二分图中,选取最少的点数,使这些点和所有的边都有关联(把所有的边的覆盖),叫做最小点覆盖。

结论:最小点覆盖数 = 最大匹配数

思路:把横坐标作为集合X,纵坐标作为集合Y,对于点(x,y),由X中的x连向Y中的y。对于每条边,只要x和y中有一个被删除即可,明显的最小点覆盖模型。

AC代码:

 #include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
#define maxn 5555
int vis[maxn];
int match[maxn];
int e[maxn][maxn];
int n,m;
int dfs(int u){ //匈牙利算法模板
for(int i=;i<=n;i++){
if(vis[i]==&&e[u][i]==){
vis[i]=;
if(match[i]==||dfs(match[i])){
match[i]=u;
return ;
}
}
}
return ;
}
int main(){
cin>>n>>m;
for(int i=;i<=m;i++){
int x,y;
cin>>x>>y;
e[x][y]=;
}
memset(match,,sizeof(match));
int ans=;
for(int i=;i<=n;i++){
memset(vis,,sizeof(vis));
if(dfs(i))
ans++;
}
printf("%d\n",ans);
return ;
}

Asteroids POJ - 3041 【最小点覆盖集】的更多相关文章

  1. POJ 3041 Asteroids (二分图最小点覆盖集)

    Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24789   Accepted: 13439 Descr ...

  2. poj 3041 最小点覆盖=最大匹配

    #include<stdio.h> #include<string.h> #define  N  510 int map[N][N],n,mark[N],link[N]; in ...

  3. POJ 3041 Asteroids (最小点覆盖集)

    题意 给出一个N*N的矩阵,有些格子上有障碍,要求每次消除一行或者一列的障碍,最少消除多少次可以全部清除障碍. 思路 把关键点取出来:一个障碍至少需要被它的行或者列中的一个消除. 也许是最近在做二分图 ...

  4. POJ 2226 Muddy Fields (最小点覆盖集,对比POJ 3041)

    题意 给出的是N*M的矩阵,同样是有障碍的格子,要求每次只能消除一行或一列中连续的格子,最少消除多少次可以全部清除. 思路 相当于POJ 3041升级版,不同之处在于这次不能一列一行全部消掉,那些非障 ...

  5. Asteroids POJ - 3041 二分图最小点覆盖

       Asteroids POJ - 3041 Bessie wants to navigate her spaceship through a dangerous asteroid field in ...

  6. Asteroids POJ - 3041

    Asteroids POJ - 3041 题目大意:N*N的地图里,存在一些小行星,Bessie有个很牛x但又很耗蓝的武器,一次可以消灭一行或者一列的所有小行星,问最少使用多少次这个武器可以消灭所有的 ...

  7. ACM/ICPC 之 机器调度-匈牙利算法解最小点覆盖集(DFS)(POJ1325)

    //匈牙利算法-DFS //求最小点覆盖集 == 求最大匹配 //Time:0Ms Memory:208K #include<iostream> #include<cstring&g ...

  8. POJ2226 Muddy Fields(二分图最小点覆盖集)

    题目给张R×C的地图,地图上*表示泥地..表示草地,问最少要几块宽1长任意木板才能盖住所有泥地,木板可以重合但不能盖住草地. 把所有行和列连续的泥地(可以放一块木板铺满的)看作点且行和列连续泥地分别作 ...

  9. POJ1325 Machine Schedule(二分图最小点覆盖集)

    最小点覆盖集就是在一个有向图中选出最少的点集,使其覆盖所有的边. 二分图最小点覆盖集=二分图最大匹配(二分图最大边独立集) 这题A机器的n种模式作为X部的点,B机器的m种模式作为Y部的点: 每个任务就 ...

  10. Jewelry Exhibition(最小点覆盖集)

    Jewelry Exhibition 时间限制: 1 Sec  内存限制: 64 MB提交: 3  解决: 3[提交][状态][讨论版] 题目描述 To guard the art jewelry e ...

随机推荐

  1. python学习-15 基本数据类型4

    1.range a = range(0 ,100 , 5) #创建>=0,<100的连续数字,步长为5 for b in a: print(b) 运算结果: 0 5 10 15 20 25 ...

  2. (十一)springmvc和spring的整合

    1:Maven引入相关的jar包. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht ...

  3. (五)springmvc之获取表单提交的数据

    8.1:使用Request <form method="post" id="form1" action="<%=request.getCo ...

  4. 监控SQL:通过SQL Server的DDL触发器来监控数据库结构的变化(1)

    原文:监控SQL:通过SQL Server的DDL触发器来监控数据库结构的变化(1) 如果你要同步不同数据库之间的数据,首先会想到的是数据库复制技术,但如果让你同步数据库的结构,你会想到什么呢? 下面 ...

  5. 巧用Ajax的beforeSend 提高用户体验--防止重复数据

    巧用Ajax的beforeSend 提高用户体验 jQuery是经常使用的一个开源js框架,其中的$.ajax请求中有一个beforeSend方法,用于在向服务器发送请求前执行一些动作.具体可参考jQ ...

  6. D盘Program Files 文件夹里文件不显示,没隐藏。怎么才能显示出来?

    D盘里有两个一模一样的Program Files 文件夹,文件夹里文件不显示,没隐藏.怎么才能显示出来?新买不久的电脑,win8.1系统 点击开始---运行---输入“cmd”(没有引号)---在弹出 ...

  7. pmtk3

    summary A list of every PMTK3 demo auto-generated by publishDemos on 27-Mar-2012 Book Demos (1) Intr ...

  8. 使用Canvas压缩图片

    讲干货,不啰嗦,当涉及对图片有质量压缩要求的时候,可以使用Canvas实现图片压缩. 步骤: 1.获取img元素,既要压缩的图片 2.创建canvas对象 3.使用canvas的drawImage方法 ...

  9. 对WAF的一些认知

    WAF分为非嵌入型与嵌入型, 非嵌入型指的是硬WAF.云WAF.虚拟机WAF之类的:嵌入型指的是web容器模块类型WAF.代码层WAF.非嵌入型对WEB流量的解析完全是靠自身的,而嵌入型的WAF拿到的 ...

  10. goroutine的设计与实现

    goroutine背后的系统知识 http://www.sizeofvoid.net/goroutine-under-the-hood/ 下周写完