Loj 2276 新型城市化

二分图最大匹配的必须边.

首先可以在反图上考虑这个问题(即将边集替换为其补集).

问题变为给出一张二分图,询问删掉哪些边后最大匹配会减少,即哪些边是最大匹配的必须边.

首先用最大流跑出一个最大匹配,仅保留未满流的边做 tarjan,将每个点所属的 SCC 求出.

边 $(u,v)$ 是最大匹配的可行边,当且仅当它满流,或 $u,v$ 在相同的 SCC 中.

证明:若其满流,显然是可行边,若在相同的 SCC 中,则可以替换掉原来的一条匹配边.

边 $(u,v)$ 是最大匹配的必须边,当且仅当它满流,且 $u,v$ 在不同的 SCC 中.

证明:若其不满流,显然不是必须边,若在相同的 SCC 中,则可以被其他边替换掉.

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
//%std
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read()
{
int out = 0, fh = 1;
char jp = getchar();
while ((jp > '9' || jp < '0') && jp != '-')
jp = getchar();
if (jp == '-')
fh = -1, jp = getchar();
while (jp >= '0' && jp <= '9')
out = out * 10 + jp - '0', jp = getchar();
return out * fh;
}
void print(int x)
{
if (x >= 10)
print(x / 10);
putchar('0' + x % 10);
}
void write(int x, char c)
{
if (x < 0)
putchar('-'), x = -x;
print(x);
putchar(c);
}
const int N = 1e4 + 10, M = 3e5 + 10, inf = 1e9;
int S, T, n, m, ecnt = 1, head[N], cur[N], dep[N];
struct Edge
{
int nx, to, flow;
} E[M];
void addedge(int u, int v, int w)
{
E[++ecnt] = (Edge){head[u], v, w};
head[u] = ecnt;
}
void ins(int u, int v, int w)
{
addedge(u, v, w);
addedge(v, u, 0);
}
queue<int> q;
bool bfs()
{
for (int i = 1; i <= T; ++i)
dep[i] = -1, cur[i] = head[i];
dep[S] = 0, q.push(S);
while (!q.empty())
{
int u = q.front();
q.pop();
for (int i = head[u]; i; i = E[i].nx)
{
int v = E[i].to;
if (E[i].flow && dep[v] == -1)
dep[v] = dep[u] + 1, q.push(v);
}
}
return dep[T] != -1;
}
int dfs(int u, int limit)
{
if (u == T || !limit)
return limit;
int flow = 0, f;
for (int &i = cur[u]; i; i = E[i].nx)
{
int v = E[i].to;
if (dep[v] == dep[u] + 1 && E[i].flow && (f = dfs(v, min(limit, E[i].flow))))
{
E[i ^ 1].flow += f;
E[i].flow -= f;
flow += f;
limit -= f;
if (!limit)
break;
}
}
return flow;
}
vector<int> G[N];
int col[N];
void paint(int u)
{
for (int v : G[u])
{
if (!col[v])
{
col[v] = 3 - col[u];
paint(v);
}
}
}
void Dinic()
{
while (bfs())
dfs(S, inf);
}
int dfn[N], low[N], idx = 0, in[N], stk[N], tp = 0, cnt = 0, scc[N];
void tarjan(int u)
{
dfn[u] = low[u] = ++idx;
in[u] = 1, stk[++tp] = u;
for (int i = head[u]; i; i = E[i].nx)
{
if (E[i].flow == 0)
continue;
int v = E[i].to;
if (!dfn[v])
{
tarjan(v);
low[u] = min(low[u], low[v]);
}
else if (in[v])
low[u] = min(low[u], dfn[v]);
}
if (dfn[u] == low[u])
{
++cnt;
int x = 0;
while (x != u)
{
x = stk[tp--];
scc[x] = cnt;
in[x] = 0;
}
}
}
vector<pair<int, int> > ans;
int main()
{
n = read(), m = read();
S = n + 1, T = n + 2;
for (int i = 1; i <= m; ++i)
{
int u = read(), v = read();
G[u].push_back(v);
G[v].push_back(u);
}
for (int i = 1; i <= n; ++i)
if (!col[i])
{
col[i] = 1;
paint(i);
}
for (int i = 1; i <= n; ++i)
{
if (col[i] == 1)
ins(S, i, 1);
else
ins(i, T, 1);
if (col[i] == 1)
for (int j : G[i])
ins(i, j, 1);
}
Dinic();
for (int i = 1; i <= T; ++i)
if (!dfn[i])
tarjan(i);
for (int u = 1; u <= n; ++u) if (col[u] == 1)
for (int i = head[u]; i; i = E[i].nx)
{
int v = E[i].to;
if (v <= n && E[i].flow == 0 && scc[u] != scc[v])
ans.push_back(make_pair(min(u, v), max(u, v)));
}
sort(ans.begin(), ans.end());
printf("%llu\n", ans.size());
for (auto p : ans)
write(p.first, ' '), write(p.second, '\n');
return 0;
}