【BZOJ】1693:[Usaco 2007 Demo]Asteroid(ハンガリー)

3384 ワード

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 gride(1<=N==500).The grid contains K asteroid(1<=K==10,000)、which are conventlythe PointBessie has a powerful weapantthat can vapopopopolize all the asteroidin in in giverow or column of the grid with a single sht.Thisweaaapensive、so she wishes to usititititititititititititisisisisininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininteroDS.
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 cooremonters。
Output
*Line 1:The integer representing the minimum number of times Bessie must shot.
Sample Input
3 4
1
1 3
2
3 2
INPUT DETAILS:
The follwing diagram represents the data,where"X"is an
asteroid and「.」is empty space:
X.X
.X.
.X.
Sample Output
2
OUT DETAILS:
Bessie may fire acros 1 to destroy the asteroidast(1,1)and
(1、3)、and then she may fire down column 2 to destroy the asteroid
at(2,2)and(3,2)
HINT
ソurce
Gold