「CH6801」棋盘覆盖
Posted zsbzsb
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了「CH6801」棋盘覆盖相关的知识,希望对你有一定的参考价值。
「CH6801」棋盘覆盖
传送门
考虑将棋盘黑白染色,两个都无障碍的相邻的点之间连边,边的容量都为1,然后就求一次最大匹配即可
参考代码:
#include <cstring>
#include <cstdio>
#include <queue>
#define rg register
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
using namespace std;
template < class T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while ('0' > c || c > '9') f |= c == '-', c = getchar();
while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
s = f ? -s : s;
}
const int _ = 1e4 + 5, __ = 5e4 + 5, INF = 2147483647;
const int dx[] = { 0, 0, 1, -1 }, dy[] = { 1, -1, 0, 0 };
int tot = 1, head[_], nxt[__ << 1], ver[__ << 1], cap[__ << 1];
inline void Add_edge(int u, int v, int d)
{ nxt[++tot] = head[u], head[u] = tot, ver[tot] = v, cap[tot] = d; }
inline void link(int u, int v, int d) { Add_edge(u, v, d), Add_edge(v, u, 0); }
int N, T, ch[102][102];
int s, t, dep[_], cur[_];
inline int bfs() {
static queue < int > Q;
while (!Q.empty()) Q.pop();
memset(dep, 0, sizeof dep);
Q.push(s), dep[s] = 1;
while (!Q.empty()) {
int u = Q.front(); Q.pop();
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i];
if (dep[v] == 0 && cap[i] > 0)
dep[v] = dep[u] + 1, Q.push(v);
}
}
return dep[t] > 1;
}
inline int dfs(int u, int flow) {
if (u == t) return flow;
for (rg int &i = cur[u]; i; i = nxt[i]) {
int v = ver[i];
if (dep[v] == dep[u] + 1 && cap[i] > 0) {
int res = dfs(v, min(cap[i], flow));
if (res) { cap[i] -= res, cap[i ^ 1] += res; return res; }
}
}
return 0;
}
inline int Dinic() {
int res = 0;
while (bfs()) {
for (rg int i = s; i <= t; ++i) cur[i] = head[i];
while (int d = dfs(s, INF)) res += d;
}
return res;
}
inline int id(int x, int y) { return y + (x - 1) * N; }
int main() {
#ifndef ONLINE_JUDGE
file("cpp");
#endif
read(N), read(T);
for (rg int x, y; T--; ) read(x), read(y), ch[x][y] = 1;
s = 0, t = N * N + 1;
for (rg int i = 1; i <= N; ++i)
for (rg int j = 1; j <= N; ++j) {
if (i + j & 1) {
if (!ch[i][j]) link(s, id(i, j), 1);
for (rg int k = 0; k < 4; ++k) {
int ni = i + dx[k], nj = j + dy[k];
if (ni >= 1 && ni <= N && nj >= 1 && nj <= N)
link(id(i, j), id(ni, nj), 1);
}
} else {
if (!ch[i][j]) link(id(i, j), t, 1);
}
}
printf("%d
", Dinic());
return 0;
}
以上是关于「CH6801」棋盘覆盖的主要内容,如果未能解决你的问题,请参考以下文章