Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17258   Accepted: 9386

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

 
这题利用的是二分图的最大匹配问题,将每一行,每一列看做一个点,将所有行构成的点组成一个集合,而所有列构成的的点看成一个集合,将输入的数据看成是连接两个集合的边,这样就是一个二分图了。
 #include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int map[][];
int vis[],match[];
int m,n;
int DFS(int p){
int v;
for(int i=;i<=m;i++){
if(map[p][i]&&!vis[i]){
vis[i]=;
v=match[i];
if( v==- || DFS(v)){
match[i]=p;
return ;
} }
}
return ;
}
int main() {
while(~scanf("%d %d",&m,&n)){
memset(map,,**sizeof(int));
for(int i=;i<n;i++){
int a,b;
scanf("%d %d",&a,&b);
map[a][b]=;
}
int result=;
memset(match,-,*sizeof(int));
for(int i=;i<=m;i++){
memset(vis,,*sizeof(int));
result+=DFS(i);
}
printf("%d\n",result);
}
return ;
}

Asteroids - poj 3041(二分图最大匹配问题)的更多相关文章

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

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

  2. Asteroids POJ - 3041

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

  3. poj 2239 二分图最大匹配,基础题

    1.poj 2239   Selecting Courses   二分图最大匹配问题 2.总结:看到一个题解,直接用三维数组做的,很巧妙,很暴力.. 题意:N种课,给出时间,每种课在星期几的第几节课上 ...

  4. POJ 2226二分图最大匹配

    匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名.匈牙利算法是基于Hall定理中充分性证明的思想,它是二部图匹配最常见的算法,该算法的核心就是寻找增广路径,它是一种用增广路径求二分图 ...

  5. POJ Evacuation /// 二分图最大匹配

    题目大意: 在一个n*m的房间中 ‘X’为墙 ‘D’为门 ‘.’为人 门只存在与外围 人每秒钟只能向四连通区域走一步 门比较狭窄 每秒钟只能通过一个人 求所有人逃脱的最短时间 如果不可能则输出impo ...

  6. Asteroids POJ - 3041 【最小点覆盖集】

    Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N g ...

  7. POJ 3041 -- 二分图匹配

    题意:有个N*N的网格,有一部分格子里有陨石,小明有很牛逼的武器,打一枪后一行或一列的陨石就没了,给出陨石分布,求最小打炮数. 分析:其实就是Konig定理.记最小打炮数为m,在网格里你最多可以找出M ...

  8. poj 2724 二分图最大匹配

    题意: 会给出M个串,我们要做的就是将这M个串给清除了.对于任意两个串,若二进制形式只有一位不一样,那么这两个串可以在一次操作消除,否则每个操作只能消除一个串. 3 3 *01 100 011 可以代 ...

  9. poj 2446 二分图最大匹配

    思路:由(i+j)为偶数的点向(i+j)为奇数的点建边.求一次最大匹配,若正好为空格数(不包含洞)的一半,即输出YES. #include<iostream> #include<cs ...

随机推荐

  1. 在C#中快速查询文件

    相信使用过Everything的人都对其超快的搜索速度印象非常深刻,它的主要原理是通过扫描NTFS磁盘的USN Journal读取的文件列表,而不是磁盘目录,由于USN Journal非常小,因此能实 ...

  2. 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:2.搭建环境-2.9. 配置用户等效性(可选项)

    2.9.配置用户等效性(可选项) Oracle 11g r2 ssh也可以在安装过程中配置. 2.9.1. grid用户等效性 1.以下均以grid用户执行: 在两个节点的grid主目录分别创建.ss ...

  3. Java计算两个日期相差的天数

    import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; impor ...

  4. SVN服务的部署及使用

      环境说明 系统版本     CentOS 7.2 x86_64   SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是 ...

  5. 子系统设计和FishiGUI的子系统设计

    目的和问题: 除了依赖关系.还要规范操作系统适配层的全部接口.仅仅要操作系统适配层的接口在移植过程中始终保持稳定.框架层的设计和实现就不会收到影响.可是为了实现同一接口的目标,为了保证相同的功能接口能 ...

  6. perl学习笔记——字符串和排序

    用index查找子字符串 查找子字符串在主字符串中的相对位置.如: $where=index($big,$small); 注意index是从0开始的,如果查找不到就会返回-1: 加入第三个参数来指定开 ...

  7. rails generate model/resource/scaffold的区别

    If you’re just learning Ruby on Rails, you may be confused as to when to generate individual models, ...

  8. NSight统计数据的颜色,缩写意义是什么?来自NV Jeff Kiel 比较官方的解释!

    结合这个图示来看:https://dl.dropboxusercontent.com/u/32077444/nsight.pdf 1) The bars you see in the Summary ...

  9. spring实现定时任务

    今天在项目里需要实现一个定时任务,每隔3个小时将过滤的广告通过邮件上报给运营一次.考虑了一下,从实现的技术上可以有三种做法: 1.Java自带的java.util.Timer类,这个类允许调度一个ja ...

  10. 分别在.NET Framework 与 .NET Core 框架下 编写Windows Service(windows服务程序)

    前言,为什么会分别在两个框架下编写Windows Service,是因为最近在做区块链这块,使用的是NEO(小蚁区块链)的相关技术,NEO使用的是.net core 2.1,业务上需要写两个程序,一个 ...