洛谷:紫
二分图+dinic
随便抓一个国际象棋的棋盘,发现可互相攻击的装置一定是处于不同颜色上的,因此考虑进行二分图匹配,对所有能够互相攻击到的装置间连一条边,每条边表示连线两端的点同时至多只能选择一个。
让源点 s s s 向所有白色点连容量为 1 1 1 的边,所有黑色点向汇点 t t t 连容量为 1 1 1 的边,能够互相攻击到的装置间连容量为 1 1 1 的边,最终答案即为 n ∗ n − m a x f l o w − s u m n*n-maxflow-sum n∗n−maxflow−sum,其中 s u m sum sum 为 1 1 1 的格子数目。
参考代码
#include <bits/stdc++.h>
#define itn int
#define int long long
#define endl "\n"
#define PII pair<int, int>
using namespace std;
const int N = 40000;
const int M = 40000;
const itn inf = 0x3f3f3f;
const int mod = 998244353;
// const int mod = 1e9 + 7;
int n, m, s, t;
int nex[M];
struct Edge {
int from, to, cap, flow;
Edge(int f, int t, int c, int fl) {
from = f;
to = t;
cap = c;
flow = fl;
}
};
struct Dinic {
int n, m, s, t; //结点数,边数(包括反向弧),源点编号和汇点编号
vector<Edge> edges; //边表。edge[e]和edge[e^1]互为反向弧
vector<int> G[N]; //邻接表,G[i][j]表示节点i和第j条边在e数组中的序号
bool vis[N]; // BFS使用
int d[N]; //从起点到i的距离
int cur[N]; //当前弧下标
void clear_all(int n) {
for (int i = 0; i < n; i++)
G[i].clear();
edges.clear();
}
void clear_flow() {
int len = edges.size();
for (int i = 0; i < len; i++)
edges[i].flow = 0;
}
void add_edge(int from, int to, int cap) {
edges.push_back(Edge(from, to, cap, 0));
edges.push_back(Edge(to, from, 0, 0));
m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
bool BFS() {
memset(vis, 0, sizeof(vis));
queue<int> q;
q.push(s);
d[s] = 0;
vis[s] = 1;
while (!q.empty()) {
int x = q.front();
q.pop();
int len = G[x].size();
for (int i = 0; i < len; i++) {
Edge& e = edges[G[x][i]];
if (!vis[e.to] && e.cap > e.flow) {
vis[e.to] = 1;
d[e.to] = d[x] + 1;
q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x, int a) {
if (x == t || a == 0)
return a;
int flow = 0, f, len = G[x].size();
for (int& i = cur[x]; i < len; i++) {
Edge& e = edges[G[x][i]];
if (d[x] + 1 == d[e.to] &&
(f = DFS(e.to, min(a, e.cap - e.flow))) > 0) {
e.flow += f;
edges[G[x][i] ^ 1].flow -= f;
flow += f;
a -= f;
if (a == 0)
break;
}
}
return flow;
}
int maxflow(int s, int t) {
this->s = s;
this->t = t;
int flow = 0;
while (BFS()) {
memset(cur, 0, sizeof(cur));
flow += DFS(s, inf);
}
return flow;
}
int mincut() { // call this after maxflow
int ans = 0;
int len = edges.size();
for (int i = 0; i < len; i++) {
Edge& e = edges[i];
if (vis[e.from] && !vis[e.to] && e.cap > 0)
ans++;
}
return ans;
}
void reduce() {
int len = edges.size();
for (int i = 0; i < len; i++)
edges[i].cap -= edges[i].flow;
}
} dinic;
int a[300][300], id[300][300], col[300][300];
void add(int x, int y, int n) { //屎山
if (x - 1 >= 1 && y - 2 >= 1 && a[x - 1][y - 2] == 0)
dinic.add_edge(id[x][y], id[x - 1][y - 2], 1);
if (x - 2 >= 1 && y - 1 >= 1 && a[x - 2][y - 1] == 0)
dinic.add_edge(id[x][y], id[x - 2][y - 1], 1);
if (x - 1 >= 1 && y + 2 <= n && a[x - 1][y + 2] == 0)
dinic.add_edge(id[x][y], id[x - 1][y + 2], 1);
if (x - 2 >= 1 && y + 1 <= n && a[x - 2][y + 1] == 0)
dinic.add_edge(id[x][y], id[x - 2][y + 1], 1);
if (x + 1 <= n && y - 2 >= 1 && a[x + 1][y - 2] == 0)
dinic.add_edge(id[x][y], id[x + 1][y - 2], 1);
if (x + 2 <= n && y - 1 >= 1 && a[x + 2][y - 1] == 0)
dinic.add_edge(id[x][y], id[x + 2][y - 1], 1);
if (x + 1 <= n && y + 2 <= n && a[x + 1][y + 2] == 0)
dinic.add_edge(id[x][y], id[x + 1][y + 2], 1);
if (x + 2 <= n && y + 1 <= n && a[x + 2][y + 1] == 0)
dinic.add_edge(id[x][y], id[x + 2][y + 1], 1);
}
void solve() {
int n;
cin >> n;
int cnt = 0;
int sum = 0, s = 0, t = n * n + 1;
for (int i = 1; i <= n; i++) {
for (itn j = 1; j <= n; j++) {
scanf("%1d", &a[i][j]);
sum += a[i][j];
}
}
for (int i = 1; i <= n; i++) {
for (itn j = 1; j <= n; j++) {
id[i][j] = ++cnt;
if (a[i][j])
continue;
if ((i + j) % 2 == 1) {
col[i][j] = 1;
dinic.add_edge(cnt, t, 1);
} else {
col[i][j] = 0;
dinic.add_edge(s, cnt, 1);
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (a[i][j])
continue;
if (col[i][j] == 0) {
// cout << i << " " << j << endl;
add(i, j, n); //白点向所有能连到的黑点连边
}
}
}
int ans = dinic.maxflow(s, t);
cout << n * n - ans - sum << endl;
}
signed main() {
// ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(12);
int T = 1;
// cin >> T;
while (T--)
solve();
return 0;
}