天天看点

poj3041(二分匹配简单题)

题目链接:http://poj.org/problem?id=3041

asteroids

time limit: 1000ms

memory limit: 65536k

total submissions: 14022

accepted: 7629

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

sample output

hint

input details: 

the following diagram represents the data, where "x" is an asteroid and "." is empty space: 

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

source

这也是一道比较简单的二分匹配的题目。把光束当作图的顶点,而把小行星当作连接对应光束的边,如此一来,光束的攻击方案即对应一个顶点集合s。图中每一条边至少有一个属于s的端点。转换成了最小顶点覆盖问题。

在二分图中     最大匹配=最小顶点覆盖

于是用二分匹配可解: