Skip to content

Commit 1967c09

Browse files
committed
Max Flow and SCC
1 parent 067608f commit 1967c09

File tree

3 files changed

+357
-0
lines changed

3 files changed

+357
-0
lines changed

Diff for: Max Flow/AtCoder - G_-_Builder_Takahashi.cpp

+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
#define SMALL_INF (ll)1e17
8+
9+
template<typename T>
10+
class MaxFlow {
11+
public:
12+
int n;
13+
vector<vector<T>> adj;
14+
vector<vector<T>> capacity; // stores the residual flow
15+
vector<vector<T>> flows; // stores the flow
16+
vector<vector<bool>> direction; // stores the edges direction
17+
T INF = numeric_limits<T>::max();
18+
19+
struct Flow {
20+
T node;
21+
T flow;
22+
};
23+
24+
MaxFlow(int n) {
25+
this->n = n;
26+
this->adj.resize(n, vector<T>());
27+
this->capacity.resize(n, vector<T>(n, 0));
28+
this->direction.resize(n, vector<bool>(n, false));
29+
this->flows.resize(n, vector<T>(n, 0));
30+
}
31+
32+
void addEdge(int u, int v, T c) {
33+
direction[u][v] = true;
34+
capacity[u][v] += c;
35+
adj[u].push_back(v);
36+
adj[v].push_back(u);
37+
}
38+
39+
T bfs(int source, int sink, vector<int>& parent) {
40+
fill(parent.begin(), parent.end(), -1);
41+
parent[source] = -2; // NULL value
42+
queue<Flow> q;
43+
q.push({ source, INF });
44+
45+
while(!q.empty()) {
46+
T currentNode = q.front().node;
47+
T flow = q.front().flow;
48+
q.pop();
49+
50+
for (int nextNode: adj[currentNode]) {
51+
if (parent[nextNode] == -1 && capacity[currentNode][nextNode] > 0) {
52+
parent[nextNode] = currentNode;
53+
T new_flow = min(flow, capacity[currentNode][nextNode]);
54+
if (nextNode == sink) return new_flow;
55+
q.push({ nextNode, new_flow });
56+
assert(new_flow != INF);
57+
}
58+
}
59+
}
60+
61+
return 0;
62+
}
63+
64+
T getMaxFlow(int source, int sink) {
65+
vector<int> parent(n);
66+
T flow = 0;
67+
T new_flow;
68+
69+
while (new_flow = bfs(source, sink, parent)) {
70+
flow += new_flow;
71+
T current = sink;
72+
while (current != source) {
73+
int prev = parent[current];
74+
flows[prev][current] += new_flow;
75+
flows[current][prev] -= new_flow;
76+
capacity[prev][current] -= new_flow;
77+
capacity[current][prev] += new_flow;
78+
current = prev; // go back
79+
}
80+
}
81+
82+
return flow;
83+
};
84+
85+
map<pair<int, int>, T> getEdges() {
86+
map<pair<int, int>, T> result; // edge (u, v) mapped to flow
87+
for (int i = 0; i < (int)adj.size(); i++) {
88+
for (int j = 0; j < (int)adj[i].size(); j++) {
89+
int nextNode = adj[i][j];
90+
if (flows[i][nextNode] > 0 && direction[i][nextNode]) {
91+
result[{ i, nextNode }] = flows[i][nextNode];
92+
}
93+
}
94+
}
95+
96+
return result;
97+
}
98+
99+
set<int> getCut(int source, int sink) {
100+
set<int> nodes;
101+
queue<int> q;
102+
q.push(source);
103+
vector<bool> visited(n, false);
104+
while (!q.empty()) {
105+
auto node = q.front();
106+
q.pop();
107+
visited[node] = true;
108+
for (auto v: adj[node]) {
109+
if (!visited[v] && capacity[node][v]) {
110+
visited[v] = true;
111+
q.push(v);
112+
}
113+
}
114+
}
115+
116+
for (int i = 0; i < n; i++) {
117+
if (visited[i]) {
118+
for (auto v: adj[i]) {
119+
if (flows[i][v] > 0 && !visited[v]) {
120+
nodes.insert(v);
121+
}
122+
}
123+
}
124+
}
125+
126+
nodes.erase(source);
127+
nodes.erase(sink);
128+
return nodes;
129+
}
130+
};
131+
132+
int main() {
133+
int n, m;
134+
cin >> n >> m;
135+
136+
int source = 1, sink = (n - 1) * 2;
137+
138+
MaxFlow<ll> mf(n * 2 + 2);
139+
140+
for (int i = 0; i < m; i++) {
141+
int s, e; cin >> s >> e;
142+
s--; e--;
143+
144+
mf.addEdge(2 * s + 1, 2 * e, SMALL_INF);
145+
mf.addEdge(2 * e + 1, 2 * s, SMALL_INF);
146+
}
147+
148+
for (int i = 0; i < n; i++) {
149+
int c;
150+
cin >> c;
151+
mf.addEdge(2 * i, 2 * i + 1, c);
152+
mf.addEdge(2 * i + 1, 2 * i, c);
153+
}
154+
155+
ll flow = mf.getMaxFlow(source, sink);
156+
cout << flow << endl;
157+
158+
set<int> vertices = mf.getCut(source, sink);
159+
vertices.erase(0);
160+
cout << vertices.size() << endl;
161+
for (auto v: vertices) {
162+
cout << (v-1) / 2 + 1 << " ";
163+
} cout << endl;
164+
165+
cout << flush;
166+
167+
return 0;
168+
}

Diff for: Max Flow/Kattis - maxflow.cpp

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class MaxFlow {
6+
public:
7+
int n;
8+
vector<vector<int>> adj;
9+
vector<vector<int>> capacity; // stores the residual flow
10+
vector<vector<int>> flows; // stores the flow
11+
vector<vector<bool>> direction; // stores the edges direction
12+
13+
struct Flow {
14+
int node;
15+
int flow;
16+
};
17+
18+
MaxFlow(int n) {
19+
this->n = n;
20+
this->adj.resize(n, vector<int>());
21+
this->capacity.resize(n, vector<int>(n, 0));
22+
this->direction.resize(n, vector<bool>(n, false));
23+
this->flows.resize(n, vector<int>(n, 0));
24+
}
25+
26+
int bfs(int source, int sink, vector<int>& parent) {
27+
fill(parent.begin(), parent.end(), -1);
28+
parent[source] = -2; // NULL value
29+
queue<Flow> q;
30+
q.push({ source, INT_MAX });
31+
32+
while(!q.empty()) {
33+
int currentNode = q.front().node;
34+
int flow = q.front().flow;
35+
q.pop();
36+
37+
for (int nextNode: adj[currentNode]) {
38+
if (parent[nextNode] == -1 && capacity[currentNode][nextNode] > 0) {
39+
parent[nextNode] = currentNode;
40+
int new_flow = min(flow, capacity[currentNode][nextNode]);
41+
if (nextNode == sink) return new_flow;
42+
q.push({ nextNode, new_flow });
43+
assert(new_flow != INT_MAX);
44+
}
45+
}
46+
}
47+
48+
return 0;
49+
}
50+
51+
int getMaxFlow(int source, int sink) {
52+
int flow = 0;
53+
vector<int> parent(n);
54+
int new_flow;
55+
56+
while (new_flow = bfs(source, sink, parent)) {
57+
flow += new_flow;
58+
int current = sink;
59+
while (current != source) {
60+
int prev = parent[current];
61+
flows[prev][current] += new_flow;
62+
flows[current][prev] -= new_flow;
63+
capacity[prev][current] -= new_flow;
64+
capacity[current][prev] += new_flow;
65+
current = prev; // go back
66+
}
67+
}
68+
69+
return flow;
70+
};
71+
72+
map<pair<int, int>, int> getEdges() {
73+
map<pair<int, int>, int> result; // edge (u, v) mapped to flow
74+
for (int i = 0; i < (int)adj.size(); i++) {
75+
for (int j = 0; j < (int)adj[i].size(); j++) {
76+
int nextNode = adj[i][j];
77+
if (flows[i][nextNode] > 0 && direction[i][nextNode]) {
78+
result[{ i, nextNode }] = flows[i][nextNode];
79+
}
80+
}
81+
}
82+
83+
return result;
84+
}
85+
};
86+
87+
int main() {
88+
int n, m;
89+
cin >> n >> m;
90+
91+
int source, sink;
92+
cin >> source >> sink;
93+
94+
MaxFlow mf(n);
95+
96+
for (int i = 0; i < m; i++) {
97+
int s, e, c; cin >> s >> e >> c;
98+
mf.direction[s][e] = true;
99+
mf.adj[s].push_back(e);
100+
mf.adj[e].push_back(s);
101+
mf.capacity[s][e] += c;
102+
}
103+
104+
int flow = mf.getMaxFlow(source, sink);
105+
auto edges = mf.getEdges();
106+
107+
cout << n << " " << flow << " " << (int)edges.size() << endl;
108+
109+
for (auto it = edges.begin(); it != edges.end(); it++) {
110+
cout << it->first.first << " " << it->first.second << " " << it->second << endl;
111+
}
112+
113+
return 0;
114+
}

Diff for: SCC/AtCoder - G_-_SCC.cpp

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
struct Tarjan {
5+
int N;
6+
vector<vector<int>> adj;
7+
vector<int> scc, ids, low;
8+
int scc_id = 0;
9+
int node_id = 0;
10+
11+
stack<int> s;
12+
vector<bool> in_stack;
13+
14+
vector<vector<int>> comps;
15+
16+
void dfs(int u) {
17+
low[u] = ids[u] = node_id++;
18+
s.push(u);
19+
in_stack[u] = true;
20+
for (int v: adj[u]) {
21+
if (ids[v] == -1) dfs(v), low[u] = min(low[u], low[v]);
22+
else if (in_stack[v]) low[u] = min(low[u], ids[v]);
23+
}
24+
25+
if (low[u] == ids[u]) {
26+
comps.push_back({ });
27+
while (true) {
28+
int v = s.top();
29+
s.pop();
30+
comps.back().push_back(v);
31+
scc[v] = scc_id;
32+
in_stack[v] = false;
33+
if (u == v) break;
34+
}
35+
36+
scc_id++;
37+
}
38+
}
39+
40+
Tarjan(int n) {
41+
N = n;
42+
adj.resize(n);
43+
scc.resize(n, -1);
44+
ids.resize(n, -1);
45+
low.resize(n);
46+
in_stack.resize(n);
47+
}
48+
49+
void solve() {
50+
for (int i = 0; i < N; i++) if (scc[i] == -1) dfs(i);
51+
reverse(comps.begin(), comps.end());
52+
}
53+
};
54+
55+
int main() {
56+
int n, m;
57+
cin >> n >> m;
58+
59+
Tarjan tarjan(n);
60+
for (int i = 0; i < m; i++) {
61+
int a, b;
62+
cin >> a >> b;
63+
tarjan.adj[a].push_back(b);
64+
}
65+
66+
tarjan.solve();
67+
cout << tarjan.comps.size() << endl;
68+
for (auto &comp: tarjan.comps) {
69+
cout << comp.size() << " ";
70+
for (int i = 0; i < comp.size(); i++) cout << comp[i] << " ";
71+
cout << endl;
72+
}
73+
74+
return 0;
75+
}

0 commit comments

Comments
 (0)