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

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<stdio.h>
#include<string.h>
int map[][];
int link[];
int vis[];
int n;
bool dfs(int u)
{
int v;
for(v = ; v <= n; v++)
{
if(map[u][v] && !vis[v])
{
vis[v] = ;
if(link[v] == - || dfs(link[v]))
{
link[v] = u;
return true;
}
}
}
return false;
}
int main()
{ int k,u,v,res;
memset(map,,sizeof(map));
scanf("%d %d",&n,&k);
while(k--)
{
scanf("%d %d",&u,&v);
map[u][v] = ;
}
memset(link,-,sizeof(link));
res = ;
for(u = ; u <= n; u++)
{
memset(vis,,sizeof(vis));
if(dfs(u)) res++;
}
printf("%d\n",res);
return ; }

Asteroids(二分图最大匹配模板题)的更多相关文章

  1. 【HDU 2063】过山车(二分图最大匹配模板题)

    题面 RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做partner和她同坐.但是,每 ...

  2. HDU - 1054 Strategic Game (二分图匹配模板题)

    二分图匹配模板题 #include <bits/stdc++.h> #define FOPI freopen("in.txt", "r", stdi ...

  3. 二分图最大匹配模板【匈牙利;Dinic最大流】

    二分图最大匹配模板[匈牙利:Dinic最大流] 匈牙利算法 int n,m; vector<int> map[100010]; int match[100010];//保存匹配的互相点 b ...

  4. POJ 3041 Asteroids(二分图模板题)

    Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N g ...

  5. hdu1083二分图匹配模板题

    onsider a group of N students and P courses. Each student visits zero, one or more than one courses. ...

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

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

  7. PKU 3041 Asteroids 最小点覆盖(最大匹配模板题)

    题目大意:给你一个N*N的矩阵, 里面有K个星球, 我们可以让武器攻击矩阵的一行或者一列来使得这个星球被击碎, 现在问你最少需要几个这种武器才能把所有的星球击碎? 解题思路:关键是建模构图 把每一行当 ...

  8. HDU-2063(二分图匹配模板题)

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

  9. POJ 3041 Asteroids 二分图

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

随机推荐

  1. IE6和IE7下绝对定位position:absolute和margin的冲突问题解决

    绝对定位的Position:absoulte的元素,会让相邻的兄弟元素的margin-top失效.而如果去掉了兄弟元素的高度又会正常. <div id="layer1" st ...

  2. python文档字符串

    #coding=utf-8 #文档字符串def d(i,j): """这个函数实现了一个乘法运算. 函数会返回一个乘法运算的结果.""" k ...

  3. ACM比赛技巧

    一.语言是最重要的基本功   无论侧重于什么方面,只要是通过计算机程序去最终实现的竞赛,语言都是大家要过的第一道关.亚洲赛区的比赛支持的语言包括C/C++与JAVA.笔者首先说说JAVA,众所周知,作 ...

  4. js正则表达式验证大全

    /判断输入内容是否为空    function IsNull(){        var str = document.getElementById('str').value.trim();      ...

  5. 织梦DeDeCms列表分页和内容页分页错位解决办法

    文章页分页代码在这里/include/arc.archives.class.php列表页分页/include/arc.listview.class.php 很多入门的站长会碰到这样的问题,织梦的通病, ...

  6. Iframe之间及iframe与父窗体之间值的传递

    方法一:ScriptManager.RegisterClientScriptBlock(this,typeof(Page), "NoInformation", "wind ...

  7. Android打开系统的Document文档图片选择

    打开Document UI 过滤图片 private void startAcitivty() { Intent intent = new Intent(); intent.setAction(&qu ...

  8. C#部分方法定义

    C#部分方法定义 部分类也可以定义部分方法.部分方法在部分类中定义,但没有方法体,在另一个部分类中执行.在这两个部分类中,都要使用partial关键字. public partial class My ...

  9. 017_bcp_bulk_openrowset

    017_bcp_bulk_openrowset --bcp*********************************************************************** ...

  10. 用Hopper搞定Mac迅雷的会员以及离线下载功能

    转自 用Hopper搞定Mac迅雷的会员以及离线下载功能 先定位Mac迅雷的可执行文件 snakeninnys-iMac:~ snakeninny$ ls /Applications/Thunder. ...