匈牙利T两个点是什么情况
查看原帖
匈牙利T两个点是什么情况
482610
Mortidesperatslav楼主2024/6/22 18:34

rt。

#include<bits/stdc++.h>
using namespace std;
int n, c1, c2, res, gz;
char mp[205][205];
int bh[205][205];
bool lr[205][205];
int dx[] = {0, -1, 1, -1, 1, -2, 2, -2, 2};
int dy[] = {0, 2, 2, -2, -2, 1, 1, -1, -1};
vector<int> G[100005];
int a[100005], vis[100005];
bool dfs(int u, int t){
	if (vis[u] == t)
		return 0;
	vis[u] = t;
	for (auto &v : G[u]){
		if (!a[v] || dfs(a[v], t)){
			a[v] = u;
			return 1;
		}
	}
	return 0;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			cin >> mp[i][j];
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			if ((i + j) & 1){
				c2++;
				bh[i][j] = c2;
				lr[i][j] = 1;
			}else{
				c1++;
				bh[i][j] = c1;
			}
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++){
			if (mp[i][j] == '1')
				continue;
			gz++;
			for (int op = 1; op <= 8; op++){
				int nx = i + dx[op], ny = j + dy[op];
				if (nx < 1 || ny < 1 || nx > n || ny > n)
					continue;
				if (mp[nx][ny] == '1')
					continue;
				if (!lr[i][j])
					G[bh[i][j]].push_back(bh[nx][ny]);
				else
					G[bh[nx][ny]].push_back(bh[i][j]);
			}
		}
	res = 0;
	for (int i = 1; i <= c1; i++)
		if(dfs(i, i))
			res++;
	cout << gz - res;
}
2024/6/22 18:34
加载中...