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

 
题意:有一个n*n的方格,方格上有m个点,每次可以消去一行或一列的所有点,请问至少需要几次才能消去所有点?
题解:一开始真没想到是二分图,后面想想一个点要么是从x坐标被消去,要么是从y坐标
把x坐标和y坐标分成两边,一个点则是一条边,那么问题就变成了如何用最少的点连接所有的边?
这是很典型的最小点覆盖问题
在二分图中最小点覆盖=最大匹配数
然后跑一边匈牙利就可以了
代码如下:
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; vector<int> g[];
int vis[],link[];
int n,m; bool check(int x)
{
int sz=g[x].size();
for(int i=;i<sz;i++)
{
int y=g[x][i];
if(!vis[y])
{
vis[y]=;
if(!link[y]||check(link[y]))
{
link[y]=x;
return ;
}
}
}
return ;
} int search()
{
int ans=;
for(int i=;i<=n;i++)
{
memset(vis,,sizeof(vis));
if(check(i))
{
ans++;
}
}
return ans;
} int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int f,t;
scanf("%d%d",&f,&t);
g[f].push_back(t);
}
int cnt=search();
printf("%d\n"
,cnt);
}
 
 
 
 
 
 

POJ3041 Asteroids(二分图最小点覆盖)的更多相关文章

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

    题目链接:http://poj.org/problem?id=3041 在一个n*n的地图中,有m和障碍物,你每一次可以消除一行或者一列的障碍物,问你最少消除几次可以将障碍物全部清除. 用二分图将行( ...

  2. [POJ3041] Asteroids(最小点覆盖-匈牙利算法)

    传送门 题意: 给一个N*N的矩阵,有些格子有障碍,要求我们消除这些障碍,问每次消除一行或一列的障碍,最少要几次.   解析: 把每一行与每一列当做二分图两边的点. 某格子有障碍,则对应行与列连边. ...

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

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

  4. 二分图 最小点覆盖 poj 3041

    题目链接:Asteroids - POJ 3041 - Virtual Judge  https://vjudge.net/problem/POJ-3041 第一行输入一个n和一个m表示在n*n的网格 ...

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

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

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

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

  7. hihoCoder #1127:二分图最小点覆盖和最大独立集

    题目大意:求二分图最小点覆盖和最大独立集. 题目分析:如果选中一个点,那么与这个点相连的所有边都被覆盖,使所有边都被覆盖的最小点集称为最小点覆盖,它等于最大匹配:任意两个点之间都没有边相连的最大点集称 ...

  8. [POJ] 2226 Muddy Fields(二分图最小点覆盖)

    题目地址:http://poj.org/problem?id=2226 二分图的题目关键在于建图.因为“*”的地方只有两种木板覆盖方式:水平或竖直,所以运用这种方式进行二分.首先按行排列,算出每个&q ...

  9. HihoCoder1127 二分图三·二分图最小点覆盖和最大独立集

    二分图三·二分图最小点覆盖和最大独立集 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在上次安排完相亲之后又过了挺长时间,大家好像都差不多见过面了.不过相亲这个事不是说 ...

随机推荐

  1. str_split的用法(PHP学习)

    str_split的用法: 先看看PHP手册是怎么说的 array str_split ( string $string [, int $split_length = 1 ] ) str_split返 ...

  2. Ubuntu14.04安装搜狗输入法的一点小问题

    难得搜狗输入法支持ubuntu,果断下载尝试一把. 官网:http://pinyin.sogou.com/linux/ 官网教程:http://pinyin.sogou.com/linux/help. ...

  3. 在rebar发布的项目中添加监视工具

    默认使用rebar创建的项目没法使用observer,可以如下操作 修改app.src {application, tcp_server, [ {description, ""}, ...

  4. 【转】JMeter代理录制脚本

    JMeter代理录制脚本 使用JMeter代理录制脚本的过程如下: 1.启动JMeter,在测试计划中添加“线程组”. 2.在“线程组”中添加“HTTP请求默认值”,参数设定如下: 3.在“”中添加“ ...

  5. codeforces 985E Pencils and Boxes(dp+思维)

    E. Pencils and Boxes time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  6. 从零开始搭建包含多个子系统的Vue工程项目

    本文以windows为例,介绍支持多个子系统的Vue工程项目的搭建过程,相对于单一系统的工程,多个子系统引入了如下一些问题: 项目目录结构设计 打包结果设计:每个子系统可以独立发布上线 多布局实现:多 ...

  7. N卡控制面板把physx设置为cpu

    一般笔记本官方配置独显都会强于核显,若是独显不够强这里教你一个小技巧来缓解独显的压力:N卡控制面板把physx设置为cpu,可以试试看.对于不强大的独显有点效果.

  8. Python web框架 Tornado(二)异步非阻塞

    异步非阻塞 阻塞式:(适用于所有框架,Django,Flask,Tornado,Bottle) 一个请求到来未处理完成,后续一直等待 解决方案:多线程,多进程 异步非阻塞(存在IO请求): Torna ...

  9. Halcon学习之五:有关图像的定义域的函数

    1.add_channels ( Regions, Image : GrayRegions : : ) 将Image图像的灰度值赋给区域Regions,得到GrayRegions. 2.change_ ...

  10. ubuntu主目录下的中文文件夹名改回英文

    linux下经常用命令行,目录有中文输起来非常麻烦,想把他改回英文于是登录的时候选择英文发现没装英文语言环境,为这个重新装麻烦,只能再想办法 找了一下发现传话里有个用户文件夹更新,命令是xdg-use ...