-网络流-最大流-二分图-强连通分量- [洛谷 P3731][HAOI2017]新型城市化

题目描述

Link

Solution

这道题太技巧了

用人话说这道题:求出二分图最大匹配的必须边

于是这道题就简单了一半

先按照题意建好二分图,然后在用Dinic跑二分图最大匹配。

对于每一条边 $(u,v)$,如果这条边满流,那么就连新边 $v -> u$,否则,连 $u -> v$。

存在这样一个结论:如果边 $(x,y)$ 在最大匹配中,则 $x,y$ 不在同一强连通分量内(我不会证明)

所以在残量网络上跑Tarjan就行了。

Code

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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define MAXN 1000010
#define INF 2000000000
struct Edge {
int v, nx, w;
}e[MAXN << 2], _e[MAXN << 2];
struct E {
int x, y;
}a[MAXN << 2];
int _head[MAXN], head[MAXN], ecnt = 1, _ecnt, n, m, col[MAXN], dep[MAXN], cur[MAXN];
int dfn[MAXN], low[MAXN], tim, st[MAXN], top, num, in[MAXN], r, k, cnt;
bool vis[MAXN];
bool cmp(E x, E y) {
return x.x < y.x || (x.x == y.x && x.y < y.y);
}
int min(int a, int b) {
if (a < b) return a;
else return b;
}
void add(int f, int t, int w) {
e[++ecnt] = (Edge) {t, head[f], w};
head[f] = ecnt;
e[++ecnt] = (Edge) {f, head[t], 0};
head[t] = ecnt;
}
void _add(int f, int t) {
_e[++_ecnt] = (Edge) {t, _head[f], 0};
_head[f] = _ecnt;
}
bool bfs(int s, int t) {
memset(dep, 0x7f, sizeof(dep));
dep[s] = 0;
for (int i = 1; i <= k; i++) {
cur[i] = head[i];
}
std::queue <int> q;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = head[u]; i; i = e[i].nx) {
int to = e[i].v;
if (dep[to] > INF && e[i].w) {
dep[to] = dep[u] + 1;
q.push(to);
}
}
}
return dep[t] < INF;
}
int dfs(int s, int t, int l) {
if (!l || s == t) {
return l;
}
int fl = 0, f;
for (int i = cur[s]; i; i = e[i].nx) {
cur[s] = i;
int to = e[i].v;
if (dep[to] == dep[s] + 1 && (f = dfs(to, t, min(l, e[i].w)))) {
fl += f;
l -= f;
e[i].w -= f;
e[i ^ 1].w += f;
if (!l) break;
}
}
return fl;
}
int Dinic(int s, int t) {
int maxf = 0;
while (bfs(s, t)) {
maxf += dfs(s, t, INF);
}
return maxf;
}
void tarjan(int u) {
dfn[u] = low[u] = ++tim;
st[++top] = u;
vis[u] = 1;
for (int i = _head[u]; i; i = _e[i].nx) {
int v = _e[i].v;
if (!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
}
else if (vis[v]) low[u] = min(low[u], dfn[v]);
}
if (dfn[u] == low[u]) {
num++;
int v;
do {
v = st[top--];
vis[v] = 0;
in[v] = num;
} while (u != v);
}
}
void dfscol(int x, int cr) {
col[x] = cr;
for (int i = _head[x]; i; i = _e[i].nx) {
int to = _e[i].v;
if (!col[to]) {
dfscol(to, 3 - cr);
}
}
}
int main() {
scanf("%d%d", &n, &m);
r = n + 1;
k = r + 1;
for (int i = 1; i <= m; i++) {
scanf("%d%d", &a[i].x, &a[i].y);
_add(a[i].x, a[i].y);
_add(a[i].y, a[i].x);
}
for (int i = 1; i <= n; i++) {
if (!col[i]) dfscol(i, 1);
}
for (int i = 1; i <= n; i++) {
if (col[i] == 1) {
add(r, i, 1);
}
if (col[i] == 2) {
add(i, k, 1);
}
}
memset(_head, 0, sizeof(_head));
_ecnt = 0;
memset(_e, 0, sizeof(_e));
for (int i = 1; i <= m; i++) {
if (col[a[i].x] == 1) {
add(a[i].x, a[i].y, 1);
}
if (col[a[i].y] == 1) {
add(a[i].y, a[i].x, 1);
}
}
int tot = Dinic(r, k);
// for (int i = 1; i <= n; i++) {
// if (col[i] == 1) {
// for (int j = head[i]; j; j = e[j].nx) {
// if (!e[j].w && e[j].v != r) {
// printf("%d -> %d\n", i, e[j].v);
// }
// }
// }
// }
for (int i = head[r]; i; i = e[i].nx) {
int to = e[i].v;
if (!e[i].w) {
_add(to, r);
}
if (e[i].w) {
_add(r, to);
}
for (int j = head[to]; j; j = e[j].nx) {
int v = e[j].v;
if (v == r) continue;
if (e[j].w) {
_add(to, v);
}
else {
_add(v, to);
}
}
}
for (int i = head[k]; i; i = e[i].nx) {
int to = e[i].v;
// printf("%d -- %d --> %d\n", k, e[i].w, to);
if (e[i].w) {
_add(k, to);
// printf("%d -> %d\n", to, k);
}
if (!e[i].w) {
_add(to, k);
// printf("%d -> %d\n", k, to);
}
}
for (int i = 1; i <= n + 2; i++) {
if (!dfn[i]) tarjan(i);
}
memset(a, 0, sizeof(a));
for (int i = 1; i <= n; i++) {
if (col[i] == 1) {
for (int j = head[i]; j; j = e[j].nx) {
int v = e[j].v;
if (v == r) continue;
if (!e[j].w) {
a[++cnt].x = i;
a[cnt].y = v;
if (a[cnt].x > a[cnt].y) std::swap(a[cnt].x, a[cnt].y);
}
}
}
}
tot = 0;
// for (int i = 1; i <= n; i++) {
// printf("%d ", in[i]);
// }
// puts("");
for (int i = 1; i <= cnt; i++) {
if (in[a[i].x] != in[a[i].y]) {
if (a[i].x > a[i].y) std::swap(a[i].x, a[i].y);
tot++;
}
}
std::sort(a + 1, a + 1 + cnt, cmp);
printf("%d\n", tot);
for (int i = 1; i <= cnt; i++) {
if (in[a[i].x] != in[a[i].y]) {
printf("%d %d\n", a[i].x, a[i].y);
}
}
return 0;
}
文章作者: RiverFun
文章链接: https://stevebraveman.github.io/blog/2019/05/13/80/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 RiverFun

评论