Kattis Dots and Boxes

题目描述

https://open.kattis.com/problems/dotsboxes

简要题意:给定一个 $n \times n$ 的网格图,有一些格点之间存在边,但保证不存在变长为 $1$ 的正方形,求最多加多少条边使得仍然不存在边长为 $1$ 的正方形

$n\le 80$

Solution

首先我们将四个格点看成一个点,那么现在有 $(n-1)\times (n-1)$ 个点,原图中格点上的边我们认为是一个障碍,那么首先原图上边界上的边我们肯定是能选则选,然后我们考虑限制,发现不存在边长为 $1$ 的正方形的条件是每个点都至少有一条边,那么我们答案我们可以转换为保留最少的边,即求一个新图的最小边覆盖

我们黑白染色后直接跑最大匹配即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <queue>
#define maxm 210
#define maxn 7010
#define INF 1000000000
using namespace std;

int n, g[maxn][maxn], du[maxn], ok[maxn];
char str[maxm];

int dx[] = { 0, 0, 1, -1 };
int dy[] = { 1, -1, 0, 0 };
inline int id(int x, int y) { return (x - 1) * (n - 1) + y; }

struct Edge {
int to, next, w;
} e[10000000]; int c1, head[maxn];
inline void add_edge(int u, int v, int w) {
e[c1].to = v; e[c1].w = w;
e[c1].next = head[u]; head[u] = c1++;
}

inline void Add_edge(int u, int v, int w) {
add_edge(u, v, w); add_edge(v, u, 0);
}

int dep[maxn], s, t;
bool bfs() {
queue<int> Q; Q.push(s);
fill(dep, dep + maxn, 0); dep[s] = 1;
while (!Q.empty()) {
int u = Q.front(); Q.pop();
for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to, w = e[i].w;
if (w > 0 && !dep[v]) {
dep[v] = dep[u] + 1;
Q.push(v); if (v == t) return 1;
}
}
} return 0;
}

int dfs(int u, int _w) {
if (!_w || u == t) return _w;
int s = 0;
for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to, w = e[i].w;
if (w > 0 && dep[v] == dep[u] + 1) {
int di = dfs(v, min(_w - s, w));
e[i].w -= di; e[i ^ 1].w += di;
s += di; if (s == _w) break;
}
}
if (s < _w) dep[u] = 0;
return s;
}

int mf;
int dinic() {
while (bfs()) mf += dfs(s, INF);
return mf;
}


int main() { fill(head, head + maxn, -1);
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);

cin >> n;
for (int i = 1; i < 2 * n; ++i) {
cin >> str + 1;
for (int j = 1; j < 2 * n; ++j) {
if ((i & 1) == (j & 1)) continue;
if ((~i & 1) && str[j] == '.') {
int x = i / 2, y = (j + 1) / 2;
if (y > 1 && y < n) {
g[id(x, y - 1)][id(x, y)] = g[id(x, y)][id(x, y - 1)] = 1;
++du[id(x, y)], ++du[id(x, y - 1)];
} else {
if (y == 1) ++du[id(x, y)], ++ok[id(x, y)];
if (y == n) ++du[id(x, y - 1)], ++ok[id(x, y - 1)];
}
}
if ((i & 1) && str[j] == '.') {
int x = (i + 1) / 2, y = j / 2;
if (x > 1 && x < n) {
g[id(x - 1, y)][id(x, y)] = g[id(x, y)][id(x - 1, y)] = 1;
++du[id(x, y)], ++du[id(x - 1, y)];
} else {
if (x == 1) ++du[id(x, y)], ++ok[id(x, y)];
if (x == n) ++du[id(x - 1, y)], ++ok[id(x - 1, y)];
}
}
}
} int cnt = (n - 1) * (n - 1), sum = 0, ans = 0;
for (int i = 1; i < n; ++i)
for (int j = 1; j < n; ++j) {
if (i != 1 && i != n - 1 && j != 1 && j != n - 1) continue;
if (i == 1 && j == 1 || i == n - 1 && j == n - 1 || i == 1 && j == n - 1 || i == n - 1 && j == 1) {
if (ok[id(i, j)] == du[id(i, j)]) --cnt;
if (ok[id(i, j)] && du[id(i, j)] != 1) {
if (ok[id(i, j)] == 1) ++ans;
else {
if (du[id(i, j)] > 2) ans += 2;
else ++ans;
}
}
continue;
}
if (ok[id(i, j)] && du[id(i, j)] == 1) --cnt;
else if (ok[id(i, j)]) ++ans;
}
s = 0; t = (n - 1) * (n - 1) + 1;
for (int i = 1; i < n; ++i)
for (int j = 1; j < n; ++j) {
if (i + j & 1) { Add_edge(id(i, j), t, 1); continue; }
Add_edge(s, id(i, j), 1);
for (int k = 0; k < 4; ++k) {
int x = i + dx[k], y = j + dy[k];
if (x < 1 || x >= n || y < 1 || y >= n || !g[id(i, j)][id(x, y)]) continue;
Add_edge(id(i, j), id(x, y), 1);
++sum;
}
}
cout << ans + sum - (cnt - dinic()) + 1 << "\n";
return 0;
}