【BZOJ】1693: [Usaco2007 Demo]Asteroids(匈牙利)

http://www.lydsy.com/JudgeOnline/problem.php?id=1693

裸匈牙利。。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }

const int N=505;
int ly[N+N], vis[N+N], ihead[N], cnt, n, m;
struct dat { int to, next; }e[10005];
void add(int u, int v) {
	e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v;
}
bool ifind(int x) {
	int y;
	for(int i=ihead[x]; i; i=e[i].next) if(!vis[y=e[i].to]) {
		vis[y]=1;
		if(!ly[y] || ifind(ly[y])) {
			ly[y]=x;
			return true;
		}
	}
	return false;
}

int main() {
	read(n); read(m);
	for1(i, 1, m) {
		int u=getint(), v=getint();
		add(u, v+n);
	}
	int ans=0;
	for1(i, 1, n) {
		CC(vis, 0);
		if(ifind(i)) ++ans;
	}
	print(ans);
	return 0;
}

 

 


 

 

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 4
1 1
1 3
2 2
3 2

INPUT DETAILS:

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

Sample Output


2

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

HINT

Source

 

posted @ 2014-09-13 06:06  iwtwiioi  阅读(332)  评论(0编辑  收藏  举报