-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1709.cpp
97 lines (92 loc) · 1.68 KB
/
1709.cpp
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
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int n, matchl[N], matchr[N], d[N];
bool markl[N], markr[N];
vector<int> g[N], row, col;
queue<int> q;
bool bfs() {
for (int i = 1; i <= n; ++i) {
if (!matchl[i]) {
d[i] = 0;
q.emplace(i);
} else {
d[i] = -1;
}
}
d[0] = -1;
while (!q.empty()) {
int u = q.front(); q.pop();
for (int v: g[u]) {
if (d[matchr[v]] == -1) {
d[matchr[v]] = d[u] + 1;
q.emplace(matchr[v]);
}
}
}
return (d[0] != -1);
}
bool dfs(int u) {
if (u == 0) return true;
for (int v: g[u]) {
if (d[matchr[v]] == d[u] + 1 && dfs(matchr[v])) {
matchl[u] = v;
matchr[v] = u;
return true;
}
}
return false;
}
void mark(int u) {
markl[u] = true;
for (int v: g[u]) {
if (!markr[v]) {
markr[v] = true;
if (matchr[v]) {
mark(matchr[v]);
}
}
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
char c; cin >> c;
if (c == 'o') {
g[i].emplace_back(j);
}
}
}
while (bfs()) {
for (int i = 1; i <= n; ++i) {
if (!matchl[i]) {
dfs(i);
}
}
}
for (int i = 1; i <= n; ++i) {
if (!matchl[i] && !markl[i]) {
mark(i);
}
}
for (int i = 1; i <= n; ++i) {
if (!markl[i]) {
row.emplace_back(i);
}
}
for (int i = 1; i <= n; ++i) {
if (markr[i]) {
col.emplace_back(i);
}
}
cout << row.size() + col.size() << "\n";
for (int r: row) {
cout << "1 " << r << "\n";
}
for (int c: col) {
cout << "2 " << c << "\n";
}
return 0;
}