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<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<vector>
#include<map>
using namespace std;
const int maxn=;
int n,m,ok[maxn];
bool a[maxn][maxn],vis[maxn]; bool Find(int x)
{
for(int i=;i<=n;i++)
{
if(a[x][i]&&!vis[i])
{
vis[i]=true;
if(!ok[i]||Find(ok[i]))
{
ok[i]=x;
return true;
}
}
}
return false;
}
int maxP()
{
int ans=;
memset(ok,,sizeof(ok));
for(int i=; i<=n; i++)
{
memset(vis,false,sizeof(vis));
if(Find(i)) ans++;
}
return ans;
}
int main()
{
cin>>n>>m; memset(a,false,sizeof(a));
int x,y,z;
while(m--)
{
cin>>x>>y;
a[x][y]=true;
}
int ans=maxP();
printf("%d\n",ans); return ;
}

POJ 3041 Asteroids(二分图模板题)的更多相关文章

  1. POJ 3041 Asteroids 二分图

    原题连接:http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  2. POJ 3041 匈牙利算法模板题

    一开始预习是百度的算法 然后学习了一下 然后找到了学长的ppt 又学习了一下.. 发现..居然不一样... 找了模板题试了试..百度的不好用 反正就是wa了..果然还是应当跟着学长混.. 图两边的点分 ...

  3. poj 3041 Asteroids (二分图的最大匹配 第一题)

    题目:http://poj.org/problem?id=3041 题意:在某个n*n的空间内,分布有一些小行星,某人在里面打炮,放一枪后某一行或某一列的行星就都没了,让求最少的打炮数. 然后把每行x ...

  4. POJ 3041 Asteroids(模板——二分最大匹配(BFS增广))

    题目链接: http://poj.org/problem?id=3041 Description Bessie wants to navigate her spaceship through a da ...

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

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

  6. poj - 3041 Asteroids (二分图最大匹配+匈牙利算法)

    http://poj.org/problem?id=3041 在n*n的网格中有K颗小行星,小行星i的位置是(Ri,Ci),现在有一个强有力的武器能够用一发光速将一整行或一整列的小行星轰为灰烬,想要利 ...

  7. POJ 3041 Asteroids 二分图之最大匹配

    题意:在一个网格中有若干个点,每一次可以清除一行或者一列,问最少几次可以将网格中的点全部清除. 思路:这个题是一个入门的最大匹配题(这个好像不是思路..).一般的方式就是将 行 看作集合A,列 看作集 ...

  8. POJ 3041 Asteroids 二分图匹配

    以行列为点建图,每个点(x,y) 对应一条边连接x,y.二分图的最小点覆盖=最大匹配 //#pragma comment(linker, "/STACK:1024000000,1024000 ...

  9. 【网络流#6】POJ 3041 Asteroids 二分图最大匹配 - 《挑战程序设计竞赛》例题

    学习网络流中ing...作为初学者练习是不可少的~~~构图方法因为书上很详细了,所以就简单说一说 把光束作为图的顶点,小行星当做连接顶点的边,建图,由于 最小顶点覆盖 等于 二分图最大匹配 ,因此求二 ...

随机推荐

  1. 怎样用JS给,option添加“选中”属性

    <html> <head> <script> window.onload = function(){ var opts = document.getElementB ...

  2. thinkphp 5.1 去掉 .html 后缀

    thinkphp 5.1 去掉 .html 后缀  

  3. PHP 发送get请求

    PHP 发送get请求 file_get_contents 方法: $s = file_get_contents("http://apis.map.qq.com/ws/distance/v1 ...

  4. poj 3974 Palindrome (manacher)

    Palindrome Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 12616   Accepted: 4769 Desc ...

  5. 使用class关键字创建类组件、props参数

    import React,{Component} from 'react' import {render} from 'react-dom' // 使用class创建组件 class Movie ex ...

  6. 2019-11-22:xss绕过笔记

    xss变形 大小写混合,sCRipt重复写,scrscriptipt通过某些标签来src属性,构造pyload,src找不到数据源的时候内部会出错,此时使用onerror触发,或iframe标签,&l ...

  7. python中return和print的区别

    之前遇到这个问题,就试着对比几种不同的结果,总结啦一下return和print的区别. 总结: return的作用之一是返回计算的值print的作用是输出数据到控制端 在第一个结果中什么都没有输出:在 ...

  8. Css搭建

    教你做css比较好的网站: https://www.jianshu.com/p/23b2bfc9a90d?tdsourcetag=s_pcqq_aiomsg https://cloud.tencent ...

  9. Erlang/Elixir精选-第1期

    第1期(20191202) 文章 A short guide to the structure and internals of the Erlang distributed messaging fa ...

  10. Linux 命令之 scp 命令详解

    Linux 命令之 scp 命令详解 一.scp 简介 scp 命令用于不同主机之间复制文件和目录. scp 是 secure copy 的缩写,是 基于 ssh 协议进行安全的远程文件拷贝命令. s ...