372. 棋盘覆盖(二分图的最大匹配:匈牙利算法)
作者:
闪回
,
2024-04-08 19:56:27
,
所有人可见
,
阅读 4
这题的转化比较抽象,需要特殊记忆
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 110;
typedef pair<int, int> PII;
#define x first
#define y second
PII match[N][N];
bool g[N][N],st[N][N];
int n,m;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
bool find(int x,int y)
{
for(int i = 0;i<4;i++)
{
int a = x+dx[i],b = y+dy[i];
if(a<1||a>n||b<1||b>n)continue;
if(st[a][b] || g[a][b])continue;
st[a][b] = true;
auto t = match[a][b];
if(t.x == -1 || find(t.x,t.y))
{
match[a][b] = {x,y};
return true;
}
}
return false;
}
int main()
{
cin>>n>>m;
while (m -- )
{
int a,b;
cin>>a>>b;
g[a][b] = true;
}
memset(match,-1,sizeof match);
int res = 0;
for(int i = 1;i<=n;i++)
{
for(int j = 1;j<=n;j++)
{
if((i+j)%2 && !g[i][j])
{
memset(st, 0, sizeof st);
if(find(i,j))res++;
}
}
}
cout<<res<<endl;
return 0;
}