Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14399   Accepted: 7836

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

好吧。。这类型的简单题就先刷到这把。。改深化投入了。。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream> using namespace std; const int M = 1000 + 5;
int n, m;
int link[M];
bool MAP[M][M];
bool cover[M];
int ans; void init()
{
int num;
int x;
int y;
memset(MAP, false, sizeof(MAP));
for(int i=1; i<=m; i++)
{
scanf("%d%d", &x, &y);
MAP[x][y]=true;
} } bool dfs(int x)
{
for(int y=1; y<=n; y++)
{
if(MAP[x][y] && !cover[y])
{
cover[y]=true;
if(link[y]==-1 || dfs(link[y]))
{
link[y]=x;
return true;
}
}
}
return false;
} int main()
{
while(scanf("%d%d", &n, &m)!=EOF)
{
ans=0;
init();
memset(link, -1, sizeof(link));
for(int i=1; i<=n; i++)
{
memset(cover, false, sizeof(cover));
if( dfs(i) )
ans++;
}
printf("%d\n", ans);
} return 0;
}

POJ 3014:Asteroids(二分匹配,匈牙利算法)的更多相关文章

  1. hdu 2444 The Accomodation of Students(二分匹配 匈牙利算法 邻接表实现)

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

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

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

  3. POJ 3041 Asteroids(二分图 && 匈牙利算法 && 最小点覆盖)

    嗯... 题目链接:http://poj.org/problem?id=3041 这道题的思想比较奇特: 把x坐标.y坐标分别看成是二分图两边的点,如果(x,y)上有行星,则将(x,y)之间连一条边, ...

  4. 【01染色法判断二分匹配+匈牙利算法求最大匹配】HDU The Accomodation of Students

    http://acm.hdu.edu.cn/showproblem.php?pid=2444 [DFS染色] #include<iostream> #include<cstdio&g ...

  5. (二分匹配“匈牙利算法”)无题II --HDU --2236

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=2236 代码: #include<cstdio> #include<cstring> ...

  6. POJ3041 Asteroids 二分图匹配 匈牙利算法

    原文链接http://www.cnblogs.com/zhouzhendong/p/8229200.html 题目传送门 - POJ3041 题意概括 有一个n*n的矩阵,有些点是障碍物. 现在每次可 ...

  7. HDU-3729 二分匹配 匈牙利算法

    题目大意:学生给出其成绩区间,但可能出现矛盾情况,找出合理组合使没有说谎的人尽可能多,并按maximum lexicographic规则输出组合. //用学生去和成绩匹配,成绩区间就是学生可以匹配的成 ...

  8. hdu 2063 (二分匹配 匈牙利算法)

    过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  9. 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  10. HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))

    Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

随机推荐

  1. [terry笔记]11gR2_DataGuard搭建_primary零停机

    11gR2搭建dataguard环境,此篇文章是利用rman搭建dataguard,这样的好处是primary不用停机,当然,前提条件是primary已经开启归档. 相对于可以停机,零停机传送数据文件 ...

  2. git checkout -b 报错

    有时候在git中checkout -b 出现如下报错 $ git checkout -b test --track origin/master fatal: Cannot update paths a ...

  3. RPC架构

    RPC架构 学习了: https://www.cnblogs.com/ChrisMurphy/p/6550184.html RPC架构简介,有一个Java例子: http://blog.csdn.ne ...

  4. RIP协议的配置

    拓扑图: 要求: 理解RIP协议的工作原理,并通过 RIP 路由协议实现不同网络互通. 配置步骤: 1.配置好 PC 机的 IP 地址等參数. PC 0>ipconfig IP Address. ...

  5. Java - Thinking in Java 第2章 一切都是对象

    Java是"纯粹"的面向对象的语言. 操作的标示符是对象的一个引用, new是创建一个对象. 存储位置: 寄存器\堆栈(引用)\堆(new)\常量存储(程序代码内部)\非RAM存储 ...

  6. 【剑指Offer面试题】 九度OJ1516:调整数组顺序使奇数位于偶数前面

    题目链接地址: http://ac.jobdu.com/problem.php?pid=1516 题目1516:调整数组顺序使奇数位于偶数前面 时间限制:1 秒内存限制:128 兆特殊判题:否提交:2 ...

  7. JAVA学习第四十五课 — 其它对象API(一)System、Runtime、Math类

    一.System类 1. static long currentTimeMillis() 返回以毫秒为单位的当前时间. 实际上:当前时间与协调世界时 1970 年 1 月 1 日午夜之间的时间差(以毫 ...

  8. vim-YCM插件安装

    这两天开始使用vim来学习C++语言,中间少不了要进行编译.才刚刚写了两个小例子就开始发现,每次都要退出vim来进行编译,实在太麻烦了.这时候才想到之前有在一本关于vim的书籍上看到quickfix的 ...

  9. thinkphp5项目--个人博客(四)

    thinkphp5项目--个人博客(四) 项目地址 fry404006308/personalBlog: personalBloghttps://github.com/fry404006308/per ...

  10. rest_framework-节流-总结完结篇

    列表从后往前读 #1.在request中获取IP#2.访问记录 VISIT_RECORD = {} 放缓存 数据库 都可以 建议缓存import timeclass VisitThrottle(obj ...