Poj 3041 (max matching problem)

Source: Internet
Author: User
Http://poj.org/problem? Id = 3041 Asteroids
Time limit:1000 ms   Memory limit:65536 K
Total submissions:14476   Accepted:7880

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. 

The question is that a missile can eliminate obstacles in a straight line, give the obstacle location, ask the minimum number of missiles required, put the rows in the matrix on one side, put the columns on the other side, and follow the bipartite graph, it is not very difficult. The main topic of this category is that the main function is a bit different, and Other templates are used.

Template question

AC code:

#include <stdio.h>#include <string.h>#define MAXN 510int N,K,ans;bool map[MAXN][MAXN];int match[MAXN];bool vis[MAXN];bool find(int u){int i;for ( i=1; i<=N; i++){if (map[u][i] && !vis[i] ){vis[i] = true;if ( match[i] == -1 || find(match[i]) ){match[i] = u;return true;}}}return false;}int main(){int i,x,y;ans = 0;scanf("%d%d",&N,&K);memset(map,0,sizeof(map));memset(match,-1,sizeof(match));for (i=0; i<K; i++){scanf("%d%d",&x,&y);map[x][y] = 1;}for (i=1; i<=N; i++){memset(vis,0,sizeof(vis));if (find(i))ans++;}printf("%d\n",ans);return 0;}



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.