HDU-1281 棋盘游戏

行列二分匹配。位于重要点的判断,删边后再判断最大匹配是否变小。

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <fstream>
#include <iostream>

#define rep(i, l, r) for(int i=l; i<=r; i++)
#define down(i, l, r) for(int i=l; i>=r; i--)
#define N 123
#define MAX 1<<30

using namespace std;
int read()
{
	int x=0, f=1; char ch=getchar();
	while (ch<'0' || ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
	while (ch>='0' && ch<='9') { x=x*10+ch-'0'; ch=getchar(); }
	return x*f;
}

struct node{int y, n; bool b;} e[N*N]; int fir[N*2], en; 
int n, m, c, k[N], kans[N], ans, ans2, t;
bool b[N];

inline void Add(int x, int y)
{
	en++, e[en].y=y, e[en].b=1, e[en].n=fir[x], fir[x]=en;
}

bool Find(int x)
{
	int o=fir[x], y=e[o].y;
	while (o)
	{
		if (e[o].b && !b[y])
		{ 
			b[y]=1; if (!k[y] || Find(k[y])) { k[y]=x; return true; } 
		}
		o=e[o].n, y=e[o].y;
	}
	return false;
}

inline int Check()
{
	rep(i, 1, n) k[i]=0; int a=0;
	rep(i, 1, m) { rep(j, 1, n) b[j]=0; if (Find(i+N)) a++; }
	return a;
}

int main()
{
	while(~scanf("%d%d%d", &n, &m, &c))
	{
		t++; ans=ans2=0;
		en=0; rep(i, 1, m) fir[i+N]=0; 
		rep(i, 1, c) { int x=read(), y=read(); Add(y+N, x); }
		ans = Check();
		rep(i, 1, n) kans[i]=k[i];
		rep(i, 1, n) if (kans[i])
		{
			int o=fir[kans[i]]; while (e[o].y != i) o=e[o].n;
			e[o].b=0;
			if (Check() != ans) ans2++;
			e[o].b=1;
		}
		printf("Board %d have %d important blanks for %d chessmen.
", t, ans2, ans);
	}
	return 0;
}
原文地址:https://www.cnblogs.com/NanoApe/p/4342787.html