Asteroids

td>Accepted: 9375

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17237  

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

(以行列分别作为两个顶点集V1、V2,其中| V1|=| V2|)

然后把每行x或者每列y看成一个点,而障碍物(x,y)可以看做连接x和y的边。按照这种思路构图后。问题就转化成为选择最少的一些点(x或y),使得从这些点与所有的边相邻,其实这就是最小点覆盖问题。

再利用二分图最大匹配的König定理:

最小点覆盖数 = 最大匹配数

 

(PS:最小点覆盖:假如选了一个点就相当于覆盖了以它为端点的所有边,你需要选择最少的点来覆盖图的所有的边。)

因此本题自然转化为求 二分图的最大匹配 问题

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int g[][:span style="color: #000000;">];
int link[];
bool vis[];
int tx,ty;
bool find(int u){
for(int i=;i<=ty;i++){
if(!vis[i]&&g[u][i]){
vis[i]=true;
if(link[i]==-||find(link[i])){
link[i]=u;
return true;
}
} }
return false;
}
int solve(){
int sum=;
memset(link,-,sizeof(link));
for(int i=;i<=tx;i++){
memset(vis,false,sizeof(vis));
if(find(i))
sum++;
}
return sum;
}
int main(){
int n;
while(scanf("%d%d",&tx,&n)!=EOF){
memset(g,,sizeof(g));
ty=tx;
int x,y;
for(int i=;i<=n;i++){
scanf("%d%d",&x,&y);
g[x][y]=;
}
printf("%d\n",solve());
}
return ;
}

poj3041 二分图最小顶点覆盖的更多相关文章

  1. poj3041 Asteroids(二分图最小顶点覆盖、二分图匹配)

    Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape o ...

  2. HDU ACM 1054 Strategic Game 二分图最小顶点覆盖?树形DP

    分析:这里使用树形DP做. 1.最小顶点覆盖做法:最小顶点覆盖 == 最大匹配(双向图)/2. 2.树形DP: dp[i][0]表示i为根节点,而且该节点不放,所需的最少的点数. dp[i][1]表示 ...

  3. POJ1325 Machine Schedule 【二分图最小顶点覆盖】

    Machine Schedule Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11958   Accepted: 5094 ...

  4. POJ 3041.Asteroids 最小顶点覆盖

    Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22905   Accepted: 12421 Descr ...

  5. HDU1054(KB10-H 最小顶点覆盖)

    Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. [poj3041]Asteroids(二分图的最小顶点覆盖)

    题目大意:$N*N$的网格中有$n$颗行星,若每次可以消去一整行或一整列,求最小的攻击次数使得消去所有行星. 解题关键:将光束当做顶点,行星当做连接光束的边建图,题目转化为求该图的最小顶点覆盖,图的最 ...

  7. POJ3041 二分图最大匹配

    问题:POJ3041 分析: 构造二分图:令A = B = { 1, 2, ... , n }, 分别代表行号集与列号集.假如第i行第j列有一颗行星,则连接Ai与Bj, 表示必须从Ai(即第i行),B ...

  8. 【HDU3861 强连通分量缩点+二分图最小路径覆盖】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 题目大意:一个有向图,让你按规则划分区域,要求划分的区域数最少. 规则如下:1.有边u到v以及有 ...

  9. POJ2226 Muddy Fields 二分匹配 最小顶点覆盖 好题

    在一个n*m的草地上,.代表草地,*代表水,现在要用宽度为1,长度不限的木板盖住水, 木板可以重叠,但是所有的草地都不能被木板覆盖. 问至少需要的木板数. 这类题的建图方法: 把矩阵作为一个二分图,以 ...

随机推荐

  1. gradle构建android项目

    工具: Android Studio2.0 gradle-2.10 一.Android常识 在做Android开发的时候我们首先必须要有一个SDK.一般SDK的主要作用就是将硬件和软件进行分离,做软件 ...

  2. JavaScript表单处理(下)

    内容提纲: 1.文本框脚本 2.选择框脚本 发文不易,转载请亲注明链接出处,谢谢!   一.文本框脚本 在HTML中,有两种方式来表现文本框: 一种是单行文本框<input type=" ...

  3. JavaScript事件---事件绑定和深入

    发文不易,转载传播,请亲注明链接出处,谢谢! 内容提纲: 1.传统事件绑定的问题 2.W3C事件处理函数 3.IE事件处理函数 4.事件对象的其他内容 事件绑定分为两种:一种是传统事件绑定(内联模型, ...

  4. jsonp与跨域

    <script>标签的src属性并不被同源策略所约束,所以可以获取任何服务器上脚本并执行. JSONP是JSON with Padding的略称.它是一个非官方的协议,它允许在服务器端集成 ...

  5. MVC4 code first 增加属性,对应自动修改列的方法笔记

    VS工具>库程序包管理器>程序包管理控制台,然后输入以下命令 enable-migrations -contexttypename Mvc4Application1.Models.Movi ...

  6. HDU 3401 Trade dp+单调队列优化

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3401 Trade Time Limit: 2000/1000 MS (Java/Others)Mem ...

  7. hdu1853 km算法

    //hdu1853 #include<stdio.h> #include<string.h> #define INF 99999999 ][],pr[],pl[],visr[] ...

  8. hdu3374 KMP+最大最小表示法

    这题要求的是字符串左移时字典序最小和最大的第几次出现,并求出现次数.考虑一会可以发现,出现次数和循环节是有关系的. 出现了几次,就是循环了几次,如果循环节是他本身,也就是无循环,那这个字符串不管怎么移 ...

  9. java初学的分析

    java初学的分析第一阶段:入门阶段学习目标:简单项目开发学习内容:1.Java入门书籍,Java基础知识.关于Java入门级的书,给大家推荐过<Java编程思想>.<Java核心技 ...

  10. IDE 集成开发环境

    集成开发环境(IDE,Integrated Development Environment )是用于提供程序开发环境的应用程序,一般包括代码编辑器.编译器.调试器和图形用户界面工具.集成了代码编写功能 ...