Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P4304 [TJOI2013]攻击装置

P4304 [TJOI2013]攻击装置

二分图最小点覆盖:对于一条边至少选择一个点。

二分图最大独立集:对于一条边只能选择一个点。

几点性质:

  • 最小点覆盖 = 最大匹配(换一种方式理解:中间连接 $inf$ 的边,对于一条边要么将左部割掉,要么将右部割掉)
  • 最大独立集 = 总点数 - 最小点覆盖 = 总点数 - 最大匹配(换一种方式理解:中间连接 $inf$ 的边,对于一条边只能将左部割掉,或只能将右部割掉)
  • 求方案,做完最大匹配后,从源点开始搜索,经过所有还有可以有流量的边,将访问过的点标记。这样,每次标记的右部点都是有匹配的,否则会存在增广路。对于最小点覆盖,选择左部中没有访问过的点和右部中访问过的点(对于左部没有被匹配的点 $x$ 不选,选择 $x$ 所有出边 $y$ ,对于所有 $y$ 的匹配 $z$ 不选 ,对于 $z$ 的出边不选 ……这样一对匹配只选择了一个);对于最大独立集,选择左部中访问过的点和右部中没有访问过的点(先选出左部没有被匹配的点 $x$ ,对于所有出边 $y$ 不选,选择所有 $y$ 的匹配 $z$,对于所有 $z$ 的出边不选……这样一对匹配只有一个点没有被选)。

这类问题通常先做二分图的染色(保证这是一个二分图)。

常见的四连通、日字形八连通染色后都是棋盘状的。所有的点 (x, y) x + y & 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
#include <iostream>
#include <cstdio>
#include <queue>
#include <climits>
#define INF INT_MAX
using namespace std;
const int N = 4e4 + 10, M = 1e8 + 10;
int dx[8] = { 1, 1, -1, -1, 2, 2, -2, -2 },
dy[8] = { 2, -2, 2, -2, 1, -1, 1, -1 };
bool vis[N];
int n, st, ed, tot, d[N], cur[N];
int idx = -1, hd[N], nxt[M], edg[M], wt[M];
bool bfs()
{
for (int i = st; i <= ed; i++)
d[i] = -1;
d[st] = 0;
cur[st] = hd[st];
queue <int> q;
q.push(st);
while (!q.empty())
{
int t = q.front();
q.pop();
for (int i = hd[t]; ~i; i = nxt[i])
if (d[edg[i]] == -1 && wt[i])
{
cur[edg[i]] = hd[edg[i]];
d[edg[i]] = d[t] + 1;
if (edg[i] == ed)
return true;
q.push(edg[i]);
}
}
return false;
}
int find(int x, int limit)
{
if (x == ed)
return limit;
int res = 0;
for (int i = cur[x]; ~i && res < limit; i = nxt[i])
{
cur[x] = i;
if (d[edg[i]] == d[x] + 1 && wt[i])
{
int t = find(edg[i], min(wt[i], limit - res));
if (!t)
d[edg[i]] = -1;
wt[i] -= t;
wt[i ^ 1] += t;
res += t;
}
}
return res;
}
int dinic()
{
int res = 0, flow;
while (bfs())
while (flow = find(st, INF))
res += flow;
return res;
}
void add (int x, int y, int z)
{
nxt[++idx] = hd[x];
hd[x] = idx;
edg[idx] = y;
wt[idx] = z;
}
bool inside (int x, int y)
{
return x > 0 && y > 0 && x <= n && y <= n;
}
int num (int x, int y)
{
return (x - 1) * n + y;
}
int main ()
{
char a;
cin >> n;
tot = n * n;
st = 0;
ed = tot + 1;
for (int i = st; i <= ed; i++)
hd[i] = -1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
{
int t = num(i, j);
cin >> a;
if (a == '1')
{
vis[t] = true;
tot--;
continue;
}
if (i + j & 1)
{
add(st, t, 1);
add(t, st, 0);
for (int k = 0; k < 8; k++)
{
int nx = i + dx[k],
ny = j + dy[k];
int h = num(nx, ny);
if (!inside(nx, ny) || vis[h])
continue;
add(t, h, INF);
add(h, t, 0);
}
}
else
{
add(t, ed, 1);
add(ed, t, 0);
}
}
cout << tot - dinic();
return 0;
}