「BZOJ 1059」矩阵游戏-二分图匹配

给出一个 $n * n$ 的黑白方阵,问能否通过交换行或交换列使得主对角线均为黑色。

链接

BZOJ 1059

题解

设 $n$ 阶方阵中有 $n$ 个点,它们互不在同一行也互不在同一列,那么就可以用这 $n$ 个点构造出 $n$ 阶目标矩阵。

所以这个题就是能否找到 $n$ 个黑点,满足互不同行也互不同列。

考虑二分图匹配,我们把行看成一个集合,列看成另一个集合,如果 $(i, j)$ 是黑点,那么就把左边第 $i$ 个点,连向右边第 $j$ 个点,判断是否是完美匹配即可。

然而我还是写的最大流,并没有写匈牙利…

代码

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* Copyright (c) 2017, xehoth
* All rights reserved.
* 「BZOJ 1059」矩阵游戏 04-09-2017
* 二分图匹配
* @author xehoth
*/
#include <bits/stdc++.h>

namespace IO {

inline char read() {
static const int IN_LEN = 1000000;
static char buf[IN_LEN], *s, *t;
s == t ? t = (s = buf) + fread(buf, 1, IN_LEN, stdin) : 0;
return s == t ? -1 : *s++;
}

template <typename T>
inline void read(T &x) {
static char c;
static bool iosig;
for (c = read(), iosig = false; !isdigit(c); c = read()) {
if (c == -1) return;
c == '-' ? iosig = true : 0;
}
for (x = 0; isdigit(c); c = read()) x = x * 10 + (c ^ '0');
iosig ? x = -x : 0;
}

inline void read(char &c) {
while (c = read(), isspace(c) && c != -1)
;
}

inline int read(char *buf) {
register int s = 0;
register char c;
while (c = read(), isspace(c) && c != -1)
;
if (c == -1) {
*buf = 0;
return -1;
}
do
buf[s++] = c;
while (c = read(), !isspace(c) && c != -1);
buf[s] = 0;
return s;
}

const int OUT_LEN = 1000000;

char obuf[OUT_LEN], *oh = obuf;

inline void print(char c) {
oh == obuf + OUT_LEN ? (fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf) : 0;
*oh++ = c;
}

template <typename T>
inline void print(T x) {
static int buf[30], cnt;
if (x == 0) {
print('0');
} else {
x < 0 ? (print('-'), x = -x) : 0;
for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 | 48;
while (cnt) print((char)buf[cnt--]);
}
}

inline void print(const char *s) {
for (; *s; s++) print(*s);
}

inline void flush() { fwrite(obuf, 1, oh - obuf, stdout); }

struct InputOutputStream {
template <typename T>
inline InputOutputStream &operator>>(T &x) {
read(x);
return *this;
}

template <typename T>
inline InputOutputStream &operator<<(const T &x) {
print(x);
return *this;
}

~InputOutputStream() { flush(); }
} io;
}

namespace {

using IO::io;

const int MAXN = 200;
const int MAX_NODE = MAXN * 2 + 10;
const int INF = INT_MAX >> 1;

struct Node {
int v, f, index;

Node(int v, int f, int index) : v(v), f(f), index(index) {}
};

struct Graph {
typedef std::vector<Node> Vector;
Vector edge[MAX_NODE + 1];

inline void addEdge(const int u, const int v, const int f) {
edge[u].push_back(Node(v, f, edge[v].size()));
edge[v].push_back(Node(u, 0, edge[u].size() - 1));
}

inline void clear(const int n) {
for (register int i = 0; i <= n; i++) edge[i].clear();
}

inline Vector &operator[](const int i) { return edge[i]; }
};

struct ImprovedShortestAugmentPath {
Graph g;

int h[MAX_NODE + 1], gap[MAX_NODE + 1];

inline int sap(int v, int flow, int s, int t, int n) {
if (v == t) return flow;
register int rec = 0;
static int iter[MAX_NODE + 1];
for (register int i = iter[v]; i < g[v].size(); i++) {
Node *p = &g[v][i];
if (p->f > 0 && h[v] == h[p->v] + 1) {
register int ret =
sap(p->v, std::min(flow - rec, p->f), s, t, n);
p->f -= ret, g[p->v][p->index].f += ret, iter[v] = i;
if ((rec += ret) == flow || h[s] >= n) return iter[v] = 0, rec;
}
}
if (!(--gap[h[v]])) h[s] = n;
gap[++h[v]]++, iter[v] = 0;
return rec;
}

inline int sap(int s, int t, int n) {
register int ret = 0;
memset(h, 0, sizeof(int) * (n + 1));
memset(gap, 0, sizeof(int) * (n + 1));
for (gap[0] = n; h[s] < n;) ret += sap(s, INF, s, t, n);
return ret;
}

inline void solve() {
register int cases, n;
for (io >> cases; cases--;) {
io >> n;
const int S = 0, T = n * 2 + 1;
for (register int i = 1; i <= n; i++)
g.addEdge(S, i, 1), g.addEdge(n + i, T, 1);
for (register int i = 1, x; i <= n; i++) {
for (register int j = 1; j <= n; j++) {
io >> x;
if (x == 1) {
g.addEdge(i, j + n, 1);
}
}
}
io << (sap(S, T, T + 1) == n ? "Yes\n" : "No\n");
g.clear(T);
}
}
} task;
}

int main() {
task.solve();
return 0;
}

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×