Poj 3041 asteroids (binary matching template)

Source: Internet
Author: User

Question link: http://poj.org/problem? Id = 3041


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 41 11 32 23 2

Sample output

2

Hint

Input details:
The following dimo-represents the data, where "X" is an asteroid and "." Is empty space:
X. x
. X.
. X. 

Output details:
Bessie may fire your SS Row 1 to destroy the asteroids at () and (), and then she may fire down column 2 to destroy the asteroids at () and ).

Source

Usaco 2005 November gold


Question:

A matrix of N * n, which contains M minor planets. Each weapon can eliminate one or more minor series.
Q: Is there a minimum number of weapons used to destroy all planets?


The Code is as follows:

# Include <cstdio> # include <cstring> # include <algorithm> # include <iostream> using namespace STD; # define maxn 517int N; int G [maxn] [maxn], linker [maxn]; bool used [maxn]; int DFS (int l) // find the augmented path {int R; For (r = 1; r <= N; r ++) // The vertex number starts from 0. To start from 1, modify {If (G [l] [r]! = 0 &&! Used [R]) {// find the augmented path, reverse used [R] = true; If (linker [R] =-1 | DFS (linker [R]) {linker [R] = L; return 1 ;}} return 0; // do not forget this, often forget this sentence} int Hungary () {int res = 0; int L; memset (linker,-1, sizeof (linker); For (L = 1; L <= N; l ++) {memset (used, 0, sizeof (used); If (DFS (l )! = 0) RES ++;} return res;} int main () {int K, res, L, R; while (~ Scanf ("% d", & N, & K) {memset (G, 0, sizeof (g); For (INT I = 1; I <= K; I ++) {scanf ("% d", & L, & R); G [l] [r] = 1 ;} res = Hungary (); printf ("% d \ n", Res);} return 0 ;}


Poj 3041 asteroids (binary matching template)

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.