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

 
 
 
大意:K个目标在N*N的方阵中,一次攻击可以消除同一行或同一列的目标,问最少几次攻击消除所有目标。
题解:这题是看题解写的,建一个二分图,图一侧的定点代表行,另一侧的定点代表列,如果(x,y)有目标,就连边x->y。
那么这题就转换为最少选多少个点,使所有边的两个定点至少有一个被选中,这就是经典模型最小点覆盖。
结论:最小点覆盖=最大匹配。
写的很好
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
int read(){
int xx=0,ff=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){xx=(xx<<3)+(xx<<1)+ch-'0';ch=getchar();}
return xx*ff;
}
const int maxn=1010;
int N,K,lin[maxn],len;
struct edge{
int y,next;
}e[10010];
inline void insert(int xx,int yy){
e[++len].next=lin[xx];
lin[xx]=len;
e[len].y=yy;
}
int tim,pretim,match[maxn],vis[maxn],ans;
bool hun(int x){
for(int i=lin[x];i;i=e[i].next)
if(vis[e[i].y]<=pretim){
vis[e[i].y]=++tim;
if(match[e[i].y]==0||hun(match[e[i].y])){
match[x]=e[i].y;
match[e[i].y]=x;
return 1;
}
}
return 0;
}
int main(){
//freopen("in","r",stdin);
//freopen("out","w",stdout);
N=read(),K=read();
for(int i=1;i<=K;i++){
int t1=read(),t2=read()+N;
insert(t1,t2);
}
for(int i=1;i<=N*2;i++)
if(!match[i]){
pretim=tim;
tim++;
if(hun(i))
ans++;
}
printf("%d\n",ans);
return 0;
}

  

poj3041——最小点覆盖的更多相关文章

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

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

  2. 二分图变种之最小路径覆盖、最小点覆盖集【poj3041】【poj2060】

    [pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=54859604 向大(hei)佬(e)势力学(di ...

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

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

  4. hdu1498 50 years, 50 colors --- 最小点覆盖

    给一个矩阵,里面有一些不同颜色的气球.每次能够消灭一行或一列中某一种颜色的气球,问你在k次及以内,有哪些颜色的气球是不管怎样也消不完的. 那么思路就是,对每一种颜色的气球求最小点覆盖.>k 则为 ...

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

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

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

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

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

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

  8. 【POJ 3041】Asteroids (最小点覆盖)

    每次选择清除一行或者一列上的小行星.最少选择几次. 将行和列抽象成点,第i行为节点i+n,第j列为节点j,每个行星则是一条边,连接了所在的行列. 于是问题转化成最小点覆盖.二分图的最小点覆盖==最大匹 ...

  9. POJ 2226 最小点覆盖(经典建图)

    Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8881   Accepted: 3300 Desc ...

随机推荐

  1. IC验证概念总结

    一份代码,在写完之后,一定要再经过一次或多次整理和打磨,才能算完成的:一份代码,一定要把其有效代码行,精简.锤炼到最少.最短.最有效,才能算完成的.   下面这些问题和回答是忘记是在哪里看到的了,参考 ...

  2. postgres外部表如何修改源码适配pg升级

    postgres中外部表的应用如下: 但是许多在github上的fdw开源代码都是基于9.3以及9.4版本开发,原作者没有随着pg的版本升级而将外部表扩展升级,那只能靠自己去手动修改源码来让这些扩展能 ...

  3. Java_Web三大框架之Hibernate+jsp+selvect+HQL登入验证

    刚开始接触Hibernate有些举手无措,觉得配置信息太多.经过一个星期的适应,Hibernate比sql简单方便多了.下面做一下Hibernate+jsp+selvect+HQL登入验证. 第一步: ...

  4. Centos7搭建lamp环境

    首先安装apache Centos7默认已经安装httpd服务,只是没有启动. 如果需要重新安装,输入 yum install -y httpd 启动服务: systemctl start httpd ...

  5. 实例分割:MaskXRCnn 与Visual Genome数据集

    一.VG数据集 机器学习领域的突破突然让计算机获得了以未曾有的高精度识别图像中物体的能力--几乎达到了让人惊恐的程度.现在的问题是机器是否还能更上层楼,学会理解这些图片中所发生的事件. Visual ...

  6. day12-闭包函数、装饰器

    目录 闭包函数 装饰器 无参装饰器 有参装饰器 装饰器模板 闭包函数 之前我们都是通过参数将外部的值传给函数,而闭包打破了层级关系,把局部变量拿到全局使用,并把外部的变量封装到内部函数中,然后下次直接 ...

  7. Python虚拟环境和requirements.txt文件的使用

    参考: https://www.centos.bz/2018/05/centos-7-4-%E5%AE%89%E8%A3%85python3%E5%8F%8A%E8%99%9A%E6%8B%9F%E7 ...

  8. 关联API

    在类或者函数定义之前加上关联代码 API_XXX

  9. C语言数据结构链栈(创建、入栈、出栈、取栈顶元素、遍历链栈中的元素)

    /**创建链栈*创建一个top指针代表head指针*采用链式存储结构*采用头插法创建链表*操作 创建 出栈 入栈 取栈顶元素*创建数据域的结构体*创建数据域的名称指针*使用随机函数对数据域的编号进行赋 ...

  10. MATLAB学习笔记之界面基本操作

    一.命令窗口 1.对于较长的命令,可以用...连接符将断开的命令连接 s=/+/+/4 ... +/+/ 注意: 连接符...与表达式之间要留一个空格: 对于单引号内的字符串必须在一行完全引起来. a ...