[Max match] Pku3041--asteroids

Source: Internet
Author: User

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 her spaceship through a dangerous asteroid field with the shape of an n x N grid (1 ; = N <= 500). The grid contains K asteroids (1 <= k <= 10,000), which are the conveniently located at the lattice of the 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 and so she wishes to use it sparingly. Given the location of the asteroids in the field and 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 by a.
* 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 a 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 fire down column 2 to destroy T He asteroids at (2,2) and (3,2).

Source Usaco November Gold title: In a n*n matrix, there are M asteroids, you can first use weapons to destroy a row or a row of planets, ask at least how many times to play. Analysis: The classic maximum matching problem. The problem can be transformed into a minimum vertex overlay problem by pulling each row out as a node, with the row and column edges of the planet. The minimum vertex cover problem is equivalent to the maximum matching problem. The problem has to be 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 do 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 did if find (i) then Inc (K); Writeln (k-1); End Begin init; Main end. 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.