http://acm.pku.edu.cn/JudgeOnline/problem?id=3041
Asteroids
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 4643 |
|
Accepted: 2410 |
Description Bessie wants to navigate she 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 <=), which is conveniently located at the lattice points of the grid.
Fortunately, Bessie have a powerful weapon that can vaporize all the asteroids on any given row or column of the grid with A single shot. This weapon was quite expensive, so she wishes to use it sparingly. Given the location of the asteroids in the field, find the minimum number of shots Bessie needs to fire to eliminate a ll of the asteroids.
Input * Line 1:two integers N and K, separated to a single space.
* Lines 2..k+1:each line contains, 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 a asteroid and "." is empty space:
x.x
. X.
. X.
OUTPUT DETAILS:
Bessie may fire across row 1 to destroy the asteroids at (all) and (1,3), and then she could fire down column 2 to destroy T He asteroids at (2,2) and (3,2).
Source Usaco 2005 November Gold title: In a n*n matrix, there are M asteroids that can first be destroyed by a row or column of planets, asking at least how many times to hit. Analysis: The classic maximum matching topic. The problem can be converted to a minimal vertex overlay problem by extracting each row as a node, with the planets ' row and column edges. The minimum vertex coverage problem is equivalent to the maximum matching problem. The problem was solved.
Codes
Var n,m,k:longint; a:array[1..500,1..500] of Boolean; match,v:array[1..500] of longint; procedure Init; var I,x,y:long int Begin READLN (N,M); For I:=1 to M do begin READLN (x, y); A[x,y]:=true; End End Procedure main; function Find (X:longint): boolean; var i,t:longint; Begin for I:=1 to N does if a[x,i] and (v[i]<>k) then begin v[i]:=k; if (match[i]=0) or (Find (match[i)) then begin match[i]:=x; Exit (TRUE); End End Exit (FALSE); End var i:longint; Begin K:=1; For I:=1 to N does if find (i) then Inc (K); Writeln (k-1); End Begin init; Main end.