一名苦逼的OIer,想成为ACMer

Iowa_Battleship

CH6802 車的放置

原题链接

和棋盘覆盖(题解)差不多。
将行和列看成\(n+m\)个节点,且分属两个集合,如果某个节点没有被禁止,则行坐标对应节点向列坐标对应节点连边,然后就是求二分图最大匹配了。

#include<cstdio>
#include<cstring>
using namespace std;
const int N = 410;
int fi[N], di[N], ne[N], mtc[N], l;
bool v[N], a[N >> 1][N >> 1];
inline int re()
{
	int x = 0;
	char c = getchar();
	bool p = 0;
	for (; c < '0' || c > '9'; c = getchar())
		p |= c == '-';
	for (; c >= '0' && c <= '9'; c = getchar())
		x = x * 10 + c - '0';
	return p ? -x : x;
}
inline void add(int x, int y)
{
	di[++l] = y;
	ne[l] = fi[x];
	fi[x] = l;
}
bool dfs(int x)
{
	int i, y;
	for (i = fi[x]; i; i = ne[i])
		if (!v[y = di[i]])
		{
			v[y] = 1;
			if (!mtc[y] || dfs(mtc[y]))
			{
				mtc[y] = x;
				return true;
			}
		}
	return false;
}
int main()
{
	int i, j, n, m, x, y, k, s = 0;
	n = re();
	m = re();
	k = re();
	for (i = 1; i <= k; i++)
	{
		x = re();
		y = re();
		a[x][y] = 1;
	}
	for (i = 1; i <= n; i++)
		for (j = 1; j <= m; j++)
			if (!a[i][j])
				add(i, n + j);
	for (i = 1, x = n + m; i <= x; i++)
	{
		memset(v, 0, sizeof(v));
		if (dfs(i))
			s++;
	}
	printf("%d", s);
	return 0;
}

posted on 2018-09-13 14:26  Iowa_Battleship  阅读(163)  评论(0编辑  收藏  举报

导航