-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmin_cut.cpp
More file actions
580 lines (536 loc) · 18.6 KB
/
Copy pathmin_cut.cpp
File metadata and controls
580 lines (536 loc) · 18.6 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
//
// min-cut (via Dinic's algorithm)
//
// verified:
// AOJ 2594 Reverse a Road II
// https://onlinejudge.u-aizu.ac.jp/problems/2594
//
// CODE FESTIVAL 2015 エキシビション A - 高橋王国と青木王国
// https://atcoder.jp/contests/code-festival-2015-exhibition/tasks/codefestival_2015_ex_a
//
#include <bits/stdc++.h>
using namespace std;
// edge class (for max-flow)
template<class FLOW> struct FlowEdge {
// core members
int rev, from, to;
FLOW cap, icap, flow;
// constructor
constexpr FlowEdge() noexcept = default;
constexpr FlowEdge(int rev, int from, int to, FLOW cap, FLOW rcap = 0)
: rev(rev), from(from), to(to), cap(cap), icap(cap), flow(rcap) {
}
void reset() {
flow -= icap - cap;
cap = icap;
}
// debug
friend ostream& operator << (ostream& s, const FlowEdge& e) {
return s << e.from << " -> " << e.to << " (" << e.cap << ", " << e.flow << ")";
}
};
// graph class (for max-flow)
template<class FLOW> struct FlowGraph {
// core members
vector<vector<FlowEdge<FLOW>>> list;
vector<pair<int,int>> pos; // pos[i] := {vertex, order of list[vertex]} of i-th edge
// constructor
FlowGraph(int n = 0) : list(n) { }
void init(int n = 0) {
list.clear(), list.resize(n);
pos.clear();
}
void clear() {
list.clear(), pos.clear();
}
// getter
vector<FlowEdge<FLOW>> &operator [] (int i) {
assert(0 <= i && i < (int)list.size());
return list[i];
}
const vector<FlowEdge<FLOW>> &operator [] (int i) const {
assert(0 <= i && i < (int)list.size());
return list[i];
}
size_t size() const noexcept {
return list.size();
}
FlowEdge<FLOW> &get_rev_edge(const FlowEdge<FLOW> &e) {
return list[e.to][e.rev];
}
const FlowEdge<FLOW> &get_rev_edge(const FlowEdge<FLOW> &e) const {
return list[e.to][e.rev];
}
FlowEdge<FLOW> &get_edge(int i) {
return list[pos[i].first][pos[i].second];
}
const FlowEdge<FLOW> &get_edge(int i) const {
return list[pos[i].first][pos[i].second];
}
vector<FlowEdge<FLOW>> get_edges() const {
vector<FlowEdge<FLOW>> edges;
for (int i = 0; i < (int)pos.size(); ++i) {
edges.push_back(get_edge(i));
}
return edges;
}
// change edges
void reset() const {
for (int i = 0; i < (int)list.size(); ++i) {
for (FlowEdge<FLOW> &e : list[i]) e.reset();
}
}
void change_edge(FlowEdge<FLOW> &e, FLOW new_cap, FLOW new_rcap) {
assert(new_cap >= 0 && new_rcap >= 0);
FlowEdge<FLOW> &re = get_rev_edge(e);
e.cap = new_cap, e.icap = new_cap + new_rcap, e.flow = new_rcap;
re.cap = new_rcap, re.icap = new_cap + new_rcap, re.flow = new_cap;
}
// add_edge
void add_edge(int from, int to, FLOW cap, FLOW rcap = 0) {
assert(0 <= from && from < (int)list.size() && 0 <= to && to < (int)list.size());
assert(cap >= 0);
int from_id = int(list[from].size()), to_id = int(list[to].size());
if (from == to) to_id++;
pos.emplace_back(from, from_id);
list[from].push_back(FlowEdge<FLOW>(to_id, from, to, cap, rcap));
list[to].push_back(FlowEdge<FLOW>(from_id, to, from, rcap, cap));
}
void add_bidirected_edge(int from, int to, FLOW cap) {
assert(0 <= from && from < (int)list.size() && 0 <= to && to < (int)list.size());
assert(cap >= 0);
add_edge(from, to, cap, cap);
}
// augment
FLOW augment(int s, int t, FLOW up_flow = numeric_limits<FLOW>::max()) {
vector<bool> seen(size(), false);
auto dfs = [&](auto &&dfs, int v, FLOW up_flow) -> FLOW {
if (v == t) return up_flow;
seen[v] = true;
for (int i = 0; i < (int)list[v].size(); i++) {
FlowEdge<FLOW> &e = list[v][i], &re = get_rev_edge(e);
if (seen[e.to] || e.cap <= 0) continue;
FLOW flow = dfs(dfs, e.to, min(up_flow, e.cap));
if (flow > 0) {
e.cap -= flow, e.flow += flow;
re.cap += flow, re.flow -= flow;
return flow;
}
}
return FLOW(0);
};
return dfs(dfs, s, up_flow);
};
// find reachable nodes from node s (1: s-domain, -1: t-domain, 0: no reach)
vector<int> find_cut(int s, int t) const {
vector<int> res(size(), 0);
auto dfs_s = [&](auto &&dfs_s, int v) -> void {
res[v] = 1;
for (const auto &e : list[v]) {
if (res[e.to] || e.cap <= 0) continue;
dfs_s(dfs_s, e.to);
}
};
auto dfs_t = [&](auto &&dfs_t, int v) -> void {
res[v] = -1;
for (const auto &e : list[v]) {
auto re = get_rev_edge(e);
if (res[e.to] || re.cap <= 0) continue;
dfs_t(dfs_t, e.to);
}
};
dfs_s(dfs_s, s), dfs_t(dfs_t, t);
return res;
}
// check if the s-t flow is feasible
bool is_feasible(int s, int t) const {
vector<FLOW> b(list.size(), FLOW(0));
for (int v = 0; v < (int)list.size(); v++) {
for (const auto &e : list[v]) {
b[v] += (e.flow - get_rev_edge(e).flow) / 2;
}
}
if (b[s] + b[t] != 0) return false;
for (int v = 0; v < (int)list.size(); v++) {
if (v != s && v != t && b[v] != FLOW(0)) return false;
}
return true;
}
bool is_feasible(int s, int t, FLOW flow) const {
vector<FLOW> b(list.size(), FLOW(0));
for (int v = 0; v < (int)list.size(); v++) {
for (const auto &e : list[v]) {
b[v] += (e.flow - get_rev_edge(e).flow) / 2;
}
}
if (b[s] != flow) return false;
if (b[t] != -flow) return false;
for (int v = 0; v < (int)list.size(); v++) {
if (v != s && v != t && b[v] != FLOW(0)) return false;
}
return true;
}
// decompose flow into s-t simple paths and cycles
using Path = vector<FlowEdge<FLOW>>;
pair<vector<Path>, vector<Path>> decompose(int s, int t) const {
struct Arc {
int to;
FLOW rem;
int eidx;
};
assert(is_feasible(s, t));
vector<vector<Arc>> fg(list.size());
for (int v = 0; v < (int)list.size(); v++) {
for (int j = 0; j < (int)list[v].size(); j++) {
FLOW f = list[v][j].icap - list[v][j].cap;
if (f > 0) fg[v].push_back({list[v][j].to, f, j});
}
}
vector<int> ptr(list.size(), 0), onpath(list.size(), -1);
vector<pair<int, int>> route;
vector<int> used;
vector<Path> paths, cycles;
auto next_arc = [&](int v) -> int {
while (ptr[v] < (int)fg[v].size() && fg[v][ptr[v]].rem <= 0) ptr[v]++;
return (ptr[v] < (int)fg[v].size() ? ptr[v] : -1);
};
auto extract = [&](int begin, bool is_cycle) {
FLOW mi = numeric_limits<FLOW>::max();
for (int k = begin; k < (int)route.size(); k++) {
auto [v, i] = route[k];
mi = min(mi, fg[v][i].rem);
}
vector<FlowEdge<FLOW>> seq;
for (int k = begin; k < (int)route.size(); k++) {
auto [v, i] = route[k];
fg[v][i].rem -= mi;
FlowEdge<FLOW> e = list[v][fg[v][i].eidx];
e.flow = mi;
seq.push_back(e);
}
if (is_cycle) cycles.push_back(std::move(seq));
else paths.push_back(std::move(seq));
};
auto walk = [&](int start, bool stop_at_t) {
route.clear();
int v = start;
onpath[v] = 0;
used.push_back(v);
while (true) {
int i = next_arc(v), u = fg[v][i].to;
route.push_back({v, i});
if (stop_at_t && u == t) {
extract(0, false);
break;
}
if (onpath[u] != -1) {
extract(onpath[u], true);
break;
}
onpath[u] = (int)route.size();
used.push_back(u);
v = u;
}
for (int w : used) onpath[w] = -1;
used.clear();
};
// extract all s-t paths
while (next_arc(s) != -1) walk(s, true);
// decompose remained circulation into cycles
for (int v = 0; v < (int)list.size(); v++) while (next_arc(v) != -1) walk(v, false);
return {paths, cycles};
}
// debug
friend ostream& operator << (ostream& s, const FlowGraph &G) {
const auto &edges = G.get_edges();
for (const auto &e : edges) s << e << endl;
return s;
}
};
// Dinic
template<class FLOW> FLOW Dinic(FlowGraph<FLOW> &G, int s, int t, FLOW limit_flow) {
assert(0 <= s && s < (int)G.size() && 0 <= t && t < (int)G.size() && s != t);
FLOW current_flow = 0;
vector<int> level((int)G.size(), -1), iter((int)G.size(), 0);
// Dinic BFS
auto bfs = [&]() -> void {
level.assign((int)G.size(), -1);
level[s] = 0;
queue<int> que;
que.push(s);
while (!que.empty()) {
int v = que.front();
que.pop();
for (const FlowEdge<FLOW> &e : G[v]) {
if (level[e.to] < 0 && e.cap > 0) {
level[e.to] = level[v] + 1;
if (e.to == t) return;
que.push(e.to);
}
}
}
};
// Dinic DFS
auto dfs = [&](auto self, int v, FLOW up_flow) {
if (v == t) return up_flow;
FLOW res_flow = 0;
for (int &i = iter[v]; i < (int)G[v].size(); ++i) {
FlowEdge<FLOW> &e = G[v][i], &re = G.get_rev_edge(e);
if (level[v] >= level[e.to] || e.cap <= 0) continue;
FLOW flow = self(self, e.to, min(up_flow - res_flow, e.cap));
if (flow <= 0) continue;
res_flow += flow;
e.cap -= flow, e.flow += flow;
re.cap += flow, re.flow -= flow;
if (res_flow == up_flow) break;
}
return res_flow;
};
// flow
while (current_flow < limit_flow) {
bfs();
if (level[t] < 0) break;
iter.assign((int)iter.size(), 0);
while (current_flow < limit_flow) {
FLOW flow = dfs(dfs, s, limit_flow - current_flow);
if (flow <= 0) break;
current_flow += flow;
}
}
return current_flow;
};
template<class FLOW> FLOW Dinic(FlowGraph<FLOW> &G, int s, int t) {
return Dinic(G, s, t, numeric_limits<FLOW>::max());
}
//------------------------------//
// Examples
//------------------------------//
// AOJ 2594 Reverse a Road II
void AOJ_2594() {
int N, M, s, t;
while (cin >> N >> M >> s >> t) {
if (N == 0) break;
--s, --t;
FlowGraph<long long> G(N);
for (int i = 0; i < M; ++i) {
int a, b;
cin >> a >> b;
--a, --b;
G.add_edge(a, b, 1);
}
int B = Dinic(G, s, t);
auto cut = G.find_cut(s, t);
int num = 0;
const auto edges = G.get_edges();
for (auto e : edges) {
// e.from が t-domain, e.to が s-domain
if (cut[e.from] == -1 && cut[e.to] == 1 && e.flow == 0) num++;
}
cout << (num ? B+1 : B) << " " << num << endl;
}
}
// CODE FESTIVAL 2015 エキシビション A - 高橋王国と青木王国
// SCC
template<class T = long long> struct Edge {
int from, to;
T val;
Edge() : from(-1), to(-1) { }
Edge(int f, int t, T v = 1) : from(f), to(t), val(v) {}
friend ostream& operator << (ostream& s, const Edge& e) {
return s << e.from << "->" << e.to << "(" << e.val << ")";
}
};
template<class T = long long> struct Graph {
int V;
bool record_reversed_edges = false, record_edge_index = false;
vector<vector<Edge<T>>> list;
vector<vector<Edge<T>>> reversed_list;
vector<unordered_map<int, int>> id; // id[v][w] := the index of node w in G[v]
// constructors
Graph(int n = 0, bool rre = false, bool rei = false) {
init(n, rre, rei);
}
void init(int n = 0, bool rre = false, bool rei = false) {
V = n, record_reversed_edges = rre, record_edge_index = rei;
list.assign(n, vector<Edge<T>>());
if (record_reversed_edges) reversed_list.assign(n, vector<Edge<T>>());
if (record_edge_index) id.assign(n, unordered_map<int, int>());
}
Graph(const Graph&) = default;
Graph& operator = (const Graph&) = default;
// getters
vector<Edge<T>> &operator [] (int i) { return list[i]; }
const vector<Edge<T>> &operator [] (int i) const { return list[i]; }
constexpr size_t size() const { return list.size(); }
constexpr void clear() { V = 0; list.clear(); }
constexpr void resize(int n) { V = n; list.resize(n); }
const vector<Edge<T>> &get_rev_edges(int i) const {
assert(record_reversed_edges);
return reversed_list[i];
}
Edge<T> &get_edge(int u, int v) {
assert(record_edge_index);
assert(u >= 0 && u < list.size() && v >= 0 && v < list.size());
assert(id[u].count(v) && id[u][v] >= 0 && id[u][v] < list[u].size());
return list[u][id[u][v]];
}
const Edge<T> &get_edge(int u, int v) const {
assert(record_edge_index);
assert(u >= 0 && u < list.size() && v >= 0 && v < list.size());
assert(id[u].count(v) && id[u].at(v) >= 0 && id[u].at(v) < list[u].size());
return list[u][id[u].at(v)];
}
// add edge
void add_edge(int from, int to, T val = 1) {
assert(0 <= from && from < list.size() && 0 <= to && to < list.size());
if (record_edge_index) id[from][to] = (int)list[from].size();
list[from].push_back(Edge(from, to, val));
if (record_reversed_edges) reversed_list[to].push_back(Edge(to, from, val));
}
void add_bidirected_edge(int from, int to, T val = 1) {
assert(0 <= from && from < list.size() && 0 <= to && to < list.size());
if (record_edge_index) id[from][to] = (int)list[from].size();
list[from].push_back(Edge(from, to, val));
if (record_reversed_edges) reversed_list[from].push_back(Edge(from, to, val));
if (from != to) {
if (record_edge_index) id[to][from] = (int)list[to].size();
list[to].push_back(Edge(to, from, val));
if (record_reversed_edges) reversed_list[to].push_back(Edge(to, from, val));
}
}
// input (only tree-case)
friend istream& operator >> (istream &is, Graph &G) {
for (int i = 0; i < G.V - 1; i++) {
int u, v;
is >> u >> v, u--, v--;
G.add_bidirected_edge(u, v);
}
return is;
}
// output
friend ostream &operator << (ostream &os, const Graph &G) {
os << endl;
for (int i = 0; i < (int)G.size(); ++i) {
os << i << " -> ";
for (int j = 0; j < (int)G[i].size(); j++) {
if (j) os << ", ";
os << G[i][j].to << "(" << G[i][j].val << ")";
}
os << endl;
}
return os;
}
};
template<class T> struct SCC {
// results
vector<int> cmp;
vector<vector<int>> groups;
Graph<T> dag;
// intermediate results
vector<bool> seen;
vector<int> vs, rvs;
// constructor
SCC() { }
SCC(const Graph<T> &G) {
solve(G);
}
void init(const Graph<T> &G) {
solve(G);
}
// getter, compressed dag(v: node-id of compressed dag)
int get_size(int v) const {
return groups[v].size();
}
vector<int> get_group(int v) const {
return groups[v];
}
// solver
void dfs(const Graph<T> &G, int v) {
seen[v] = true;
for (const auto &e : G[v]) if (!seen[e.to]) dfs(G, e.to);
vs.push_back(v);
}
void rdfs(const Graph<T> &G, int v, int k) {
seen[v] = true;
cmp[v] = k;
for (const auto &e : G.get_rev_edges(v)) if (!seen[e.to]) rdfs(G, e.to, k);
rvs.push_back(v);
}
void reconstruct(const Graph<T> &G) {
dag.init((int)groups.size());
set<pair<int,int>> new_edges;
for (int i = 0; i < (int)G.size(); ++i) {
int u = cmp[i];
for (const auto &e : G[i]) {
int v = cmp[e.to];
if (u == v) continue;
if (!new_edges.count({u, v})) {
dag.add_edge(u, v);
new_edges.insert({u, v});
}
}
}
}
void solve(const Graph<T> &G) {
// first dfs
seen.assign((int)G.size(), false);
vs.clear();
for (int v = 0; v < (int)G.size(); ++v) if (!seen[v]) dfs(G, v);
// back dfs
int k = 0;
groups.clear();
seen.assign((int)G.size(), false);
cmp.assign((int)G.size(), -1);
for (int i = (int)G.size()-1; i >= 0; --i) {
if (!seen[vs[i]]) {
rvs.clear();
rdfs(G, vs[i], k++);
groups.push_back(rvs);
}
}
reconstruct(G);
}
};
void CODE_FESTIVAL_2015_EX_A() {
long long N, M;
cin >> N >> M;
FlowGraph<long long> G(N);
for (int i = 0; i < M; i++) {
long long A, B;
cin >> A >> B; A--, B--;
G.add_edge(A, B, 1);
}
long long s = 0, t = N-1;
long long maxflow = Dinic(G, s, t);
// find reachable nodes from node s (1: s-domain, -1: t-domain, 0: no reach)
auto cut = G.find_cut(s, t);
// 0-domain について SCC する
Graph<long long> SG(N, true);
for (int v = 0; v < N; v++) {
if (cut[v] != 0) continue;
for (auto e : G[v]) {
if (cut[e.to] != 0) continue;
if (e.cap > 0) SG.add_edge(e.from, e.to);
}
}
SCC scc(SG);
auto cmp = scc.cmp;
long long Q;
cin >> Q;
for (int qid = 0; qid < Q; qid++) {
long long c, d;
cin >> c >> d, c--, d--;
if (cut[c] == 1 || cut[d] == 1 || cut[c] == -1 || cut[d] == -1) {
if (cut[c] == cut[d]) cout << "YES NO" << endl;
else if (cut[c] * cut[d] == -1) cout << "NO YES" << endl;
else cout << "YES YES" << endl;
} else {
if (cmp[c] == cmp[d]) cout << "YES NO" << endl;
else cout << "YES YES" << endl;
}
}
}
int main() {
//AOJ_2594();
CODE_FESTIVAL_2015_EX_A();
}