2021 ICPC Southeastern Europe Regional Contest I Flood Fill

题目描述

https://codeforces.com/gym/103438/problem/I

简要题意:给定两个 $n\times m$ 的 $01$ 矩阵 $A$ 和 $B$,现在可以对 $A$ 进行任意次操作,每次操作可以选择一个颜色相同的连通块,然后将它的颜色置反,求最少能使 $A$ 和 $B$ 有多少不同的位置,连通是四连通

$n,m\le 100$

Solution

注意到 $A$ 的任意两个相邻连通块不能同时变更颜色,如果我们将连通块看成点,将相邻看成边,那么能够发现这是一个二分图,因为相当于已经完成了一个二分图染色

每个点的点权就是将这个点颜色翻转能够得到的贡献减掉未翻转的贡献,那么我们现在要求这张图的最大点权独立集,这是一个经典做法

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
#include <iostream>
#include <tuple>
#include <vector>
#include <algorithm>
#include <queue>
#define maxn 10010
#define maxm 110
#define INF 1000000000
#define ll long long
using namespace std;

int dx[] = { 0, 0, 1, -1 };
int dy[] = { 1, -1, 0, 0 };

int n, m, a[maxm][maxm], b[maxm][maxm];
char str[maxn];

int id[maxm][maxm], cnt, w[maxn];
void bfs(int sx, int sy) {
queue<pair<int, int>> Q; Q.push({ sx, sy });
id[sx][sy] = ++cnt;
while (!Q.empty()) {
int i = Q.front().first, j = Q.front().second; Q.pop();
w[cnt] += a[i][j] == b[i][j] ? -1 : 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 > m) continue;
if (id[x][y] || a[x][y] != a[i][j]) continue;
id[x][y] = cnt; Q.push({ x, y });
}
}
}

vector<int> G[maxn]; int col[maxn];
void dfs(int u) {
for (int v : G[u])
if (!col[v]) col[v] = col[u] * -1, dfs(v);
}

struct Edge {
int to, next, w;
} e[1000000]; 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 dinic() {
int mf = 0;
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 >> m; int ans = 0;
for (int i = 1; i <= n; ++i) {
cin >> str + 1;
for (int j = 1; j <= m; ++j) a[i][j] = str[j] == '1';
}
for (int i = 1; i <= n; ++i) {
cin >> str + 1;
for (int j = 1; j <= m; ++j) b[i][j] = str[j] == '1', ans += a[i][j] == b[i][j];
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) if (!id[i][j]) bfs(i, j);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
for (int k = 0; k < 4; ++k) {
int x = i + dx[k], y = j + dy[k];
if (x < 1 || x > n || y < 1 || y > m) continue;
if (id[x][y] == id[i][j]) continue;
G[id[i][j]].push_back(id[x][y]);
}
col[1] = 1; dfs(1); s = 0; t = cnt + 1;
for (int u = 1; u <= cnt; ++u) {
if (col[u] == 1) Add_edge(s, u, w[u]), ans += max(0, w[u]);
else Add_edge(u, t, w[u]), ans += max(0, w[u]);
if (col[u] == 1) for (int v : G[u]) Add_edge(u, v, INF);
} cout << n * m - (ans - dinic()) << "\n";
return 0;
}