Blog of RuSun

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

P4055 [JSOI2009] 游戏

P4055 [JSOI2009] 游戏

二分图博弈模板题。求所有存在情况不在最大匹配的点。对于每个点,如果删除,最大匹配就减少,那么一定在最大匹配中。

查看代码
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
#include <cstdio>
#include <vector>
#define fi first
#define se second
#define eb emplace_back
using namespace std;
template <class Type>
void read (Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
if (c == '-') flag = true;
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 3) + (x << 1) + c - '0';
if (flag) x = ~x + 1;
}
template <class Type, class ...Rest>
void read (Type &x, Rest &...y) { read(x), read(y...); }
template <class Type>
void write (Type x)
{
if (x < 0) putchar('-'), x = ~x + 1;
if (x > 9) write(x / 10);
putchar('0' + x % 10);
}
typedef pair <int, int> PII;
const int N = 1e4 + 10, M = 110;
vector <int> g[N];
PII p[N];
int T, vis[N];
int n, m, tot, id[M][M], f[N];
bool dfs (int u)
{
for (int v : g[u]) if (vis[v] ^ T)
{
vis[v] = T;
if (!f[v] || dfs(f[v]))
{
f[u] = v, f[v] = u;
return true;
}
}
return false;
}
int main ()
{
read(n, m);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
{
char c;
while ((c = getchar()) != '.' && c != '#');
if (c == '#') continue;
p[id[i][j] = ++tot] = { i, j };
if (id[i - 1][j])
{
g[tot].eb(id[i - 1][j]);
g[id[i - 1][j]].eb(tot);
}
if (id[i][j - 1])
{
g[tot].eb(id[i][j - 1]);
g[id[i][j - 1]].eb(tot);
}
}
for (int i = 1; i <= tot; ++i)
if (p[i].fi + p[i].se & 1) ++T, dfs(i);
vector <int> res;
for (int i = 1; i <= tot; ++i)
if (!f[i]) res.eb(i);
else
{
vis[i] = ++T;
int t = f[i];
f[t] = f[i] = 0;
if (dfs(t)) res.eb(i);
else f[t] = i, f[i] = t;
}
if (res.empty()) return puts("LOSE"), 0;
puts("WIN");
for (int i : res)
write(p[i].fi), putchar(' '), write(p[i].se), puts("");
return 0;
}