[Link] https://www.acmicpc.net/problem/17412
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
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <stack>
#include <cmath>
// #include <bits/stdc++.h>
using namespace std;
#define for0(i, n) for(int i = 0; i < n; i++)
#define for1(i, n) for(int i = 1; i < n; i++)
#define fori(s, e) for(int i = s; i < e; i++)
#define ll long long
#define endl "\n"
#define nulls '\0'
#define dkdk " "
const int MAX = 5e5 + 1;
const int INF = 987654321;
int n, p, flow_tot;
vector<vector<int>> adj;
vector<vector<int>> capacity;
vector<vector<int>> flow;
void edmonds_karp() {
// vector<int> prev(n+1);
queue<int> q;
while(true) {
vector<int> prev(n+1, -1);
q.push(1);
while(!q.empty()) {
int cur = q.front(); q.pop();
for(int next: adj[cur]) {
if(prev[next] == -1 && capacity[cur][next] - flow[cur][next] > 0) {
prev[next] = cur;
q.push(next);
if(next == 2) break;
}
}
}
if(prev[2] == -1) break;
int min_flow = INF;
for(int i = 2; i != 1; i = prev[i]) min_flow = min(min_flow, capacity[prev[i]][i] - flow[prev[i]][i]);
for(int i = 2; i != 1; i = prev[i]) {
flow[prev[i]][i] += min_flow;
flow[i][prev[i]] -= min_flow;
}
flow_tot += min_flow;
}
}
int main(int argc, char *argv[]) {
cin >> n >> p;
adj.resize(n+1);
flow.resize(n+1); for0(i, n+1) flow[i].resize(n+1);
capacity.resize(n+1); for0(i, n+1) capacity[i].resize(n+1);
for0(i, p) {
int from, to; cin >> from >> to;
adj[from].push_back(to);
adj[to].push_back(from);
capacity[from][to] = 1;
}
edmonds_karp();
cout << flow_tot;
return 0;
}