-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1711-distinct-routes.cpp
105 lines (82 loc) · 2.07 KB
/
1711-distinct-routes.cpp
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
#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long
#define vi vector<int>
#define vl vector<long long>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define umap unordered_map
#define uset unordered_set
#define rall(x) (x).rbegin(), (x).rend()
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
const long long LLINF = LLONG_MAX;
const int INF = INT_MAX;
const int MOD = 1e9 + 7;
const int MAX_N = 501;
int n, m, match_count, flows[MAX_N][MAX_N];
bool visited[MAX_N], alt_edges[MAX_N][MAX_N];
vi cycle;
bool dfs(int node) {
if (node == n) {
return true;
}
visited[node] = true;
for (int i = 1; i <= n; i++) {
if (!visited[i] && flows[node][i]) {
if (dfs(i)) {
alt_edges[node][i] = !alt_edges[node][i];
alt_edges[i][node] = !alt_edges[i][node];
flows[node][i]--;
flows[i][node]++;
return true;
}
}
}
return false;
}
void inverseDfs(int node) {
cycle.pb(node);
for (int i = 1; i <= n; i++) {
if (flows[i][node] && alt_edges[node][i]) {
flows[i][node] = alt_edges[node][i] = alt_edges[i][node] = 0;
inverseDfs(i);
return;
}
}
}
void solve() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
flows[a][b]++;
}
while (dfs(1)) {
match_count++;
for (int i = 1; i <= n; i++) {
visited[i] = false;
}
}
cout << match_count << "\n";
for (int i = 1; i <= match_count; i++) {
inverseDfs(1);
cout << cycle.size() << "\n";
for (int j : cycle) {
cout << j << " ";
}
cout << "\n";
cycle.clear();
}
}
int main() {
fastio
int tc = 1; // Number of test cases
// cin >> tc;
while (tc--) {
solve();
}
return 0;
}