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<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
const ll mod=1e9+100;
const double eps=1e-8;
using namespace std;
const double pi=acos(-1.0);
const int inf=0xfffffff;
const int N=505;
int pre[N];
int visit[N],line[N][N];
int n,m,y,x;
bool find(int x)
{
rep(i,1,n+1)
{
if(line[x][i]&&visit[i]==0)
{
visit[i]=1;
if(pre[i]==0||find(pre[i]))
{
pre[i]=x;
return true;
}
}
}
return false;
}
int main()
{
while(~sf("%d%d",&n,&m))
{
mm(line,0);
mm(pre,0);
while(m--)
{
sf("%d%d",&x,&y);
line[x][y]=1;
}
int ans=0;
rep(i,1,n+1)
{
mm(visit,0);
if(find(i)) ans++;
} pf("%d\n",ans);
}
}

N - Asteroids的更多相关文章

  1. POJ 3041 Asteroids

     最小点覆盖数==最大匹配数 Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12678 Accepted:  ...

  2. Asteroids(匈牙利算法入门)

    Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16211   Accepted: 8819 Descri ...

  3. hdu 1240:Asteroids!(三维BFS搜索)

    Asteroids! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  4. POJ 3041 Asteroids(最小点覆盖集)

                                                                      Asteroids Time Limit: 1000MS   Mem ...

  5. Asteroids (最小覆盖)

    题目很简单,但是需要推到出二分图最大匹配 = 最小覆盖 最小覆盖:证明过程http://blog.sina.com.cn/s/blog_51cea4040100h152.html Descriptio ...

  6. poj 3041 Asteroids(最小点覆盖)

    http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  7. poj 3041 Asteroids (最大匹配最小顶点覆盖——匈牙利模板题)

    http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  8. HDU-1240 Asteroids! (BFS)这里是一个三维空间,用一个6*3二维数组储存6个不同方向

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission ...

  9. POJ 3041 Asteroids 最小点覆盖 == 二分图的最大匹配

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

  10. Asteroids(二分图最大匹配模板题)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12323   Accepted: 6716 Description Bess ...

随机推荐

  1. tk.mybatis通用插件updateByPrimaryKeySelective无法自动更新ON UPDATE CURRENT_TIMESTAMP列的解决办法

    tk.mybatis是一个很好用的通用插件,把CRUD这些基本的数据操作全都用动态SQL语句自动生成了,mapper和xml里十分清爽,但是昨天发现有一个小坑,记录在此: 有一张表,结构如下(已经简化 ...

  2. securecrt通过ssh连接板子: 密钥交换失败,没有兼容的加密程序

    在用securecrt连接板子时遇到如下问题: 需要修改板子上的/etc/ssh/ssh_config和/etc/ssh/sshd_config. 修改/etc/ssh/ssh_config,取消下面 ...

  3. MyEclipse 2015 SVN 安装

    SVN的在线安装 1.打开MyEclipse,找到顶部菜单栏 Help(帮助)-Install from Site…(从网站安装),如下图 2. 点击Install from Site…后会出现让你选 ...

  4. postgresql中使用distinct去重

    select语法 [ WITH [ RECURSIVE ] with_query [, ...] ] SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ...

  5. 阿里云物联网平台体验(NetGadgeteer+C#篇)

    目前对接阿里云物联网平台有多种软件和硬件,可以有多种不同语言来实现对接,比如c/c++,Java,JS,Python,C#等等,不过C#版本只有PC对接云平台的代码,嵌入式的目前还没有看到,所以本篇文 ...

  6. 全栈JavaScript之路(十九)HTML5 插入 html标记 ( 一 )innerHTML 与outerHTML

    在须要给文档插入大量的html 标记下.通过DOM操作非常麻烦,你不仅要创建一系列的节点,并且还要小心地依照顺序把它们接结起来. 利用html 标签 插入技术,能够直接插入html代码字符串,简单.高 ...

  7. Android 蓝牙开发

    今天给大家带来蓝牙开发的基础,主要展示的是程序搜到蓝牙会通过list展示出来,并实时排序,远近与信号强弱 首先我们要有次jar包 watermark/2/text/aHR0cDovL2Jsb2cuY3 ...

  8. 多线程处理N维度topk问题demo--[c++]

    问题 -对多维度特征进行topk排序,使用c++ 优先队列模拟最大堆. /* ---------------------------------- Version : ?? File Name : d ...

  9. Vue2的独立构建与运行时构建的差别

    其实这个问题在你使用vue-cli构建项目的时候是不会出现的,因为你在创建项目的构建过程中已经让你勾选了,然后会写入webpack.config.js中. 这就在这,会让你选择Vue的构建方式. 如果 ...

  10. php项目踩到的empty函数的一个坑

    报错信息: PHP Fatal error: Can't use function return value in write context in /目录省略.../XXService.php on ...