Skip to content

Commit 2bf79b9

Browse files
committed
finish all 150 problems
1 parent dcbeaf1 commit 2bf79b9

File tree

16 files changed

+351
-169
lines changed

16 files changed

+351
-169
lines changed

Distinct Routes/attic/main.cpp.md5

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
66acd2820ab84bc8bd85cfaa5f45627b

Distinct Routes/attic/sol

556 KB
Binary file not shown.

Distinct Routes/main.cpp

+105-32
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,115 @@
11
#include <bits/stdc++.h>
2-
#include <ext/pb_ds/assoc_container.hpp>
3-
#include <ext/pb_ds/tree_policy.hpp>
4-
2+
53
using namespace std;
6-
using namespace __gnu_pbds;
7-
8-
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
9-
10-
const int MAX_N = 1e5 + 5;
11-
const int MAX_L = 20; // ~ Log N
12-
const long long MOD = 1e9 + 7;
13-
const long long INF = 1e9 + 7;
14-
const double EPS = 1e-9;
15-
16-
typedef long long ll;
17-
typedef vector<int> vi;
18-
typedef pair<int,int> ii;
19-
typedef vector<ii> vii;
20-
typedef vector<vi> vvi;
21-
22-
#define LSOne(S) (S & (-S))
23-
#define isBitSet(S, i) ((S >> i) & 1)
24-
25-
26-
4+
5+
#define ar array
6+
#define ll long long
7+
8+
const int MAX_N = 1e5 + 1;
9+
const int MOD = 1e9 + 7;
10+
const int INF = 1e9;
11+
const ll LINF = 1e18;
12+
13+
struct max_flow_graph {
14+
int V;
15+
vector<ar<ll,4>> edges; // {start, dest, cap, flow}
16+
vector<vector<int>> adj;
17+
vector<int> dist, ans;
18+
vector<ar<int,2>> par;
19+
max_flow_graph(int V): V(V), adj(V) {}
20+
void add_edge(int u, int v) {
21+
adj[u].push_back(edges.size());
22+
edges.push_back({u, v, 1, 0});
23+
adj[v].push_back(edges.size());
24+
edges.push_back({v, u, 0, 0});
25+
}
26+
bool bfs(int s, int t) {
27+
dist.assign(V, -1);
28+
par.assign(V, {-1, -1});
29+
queue<int> q;
30+
dist[s] = 0; q.push(s);
31+
while (q.size()) {
32+
int u = q.front(); q.pop();
33+
if (u == t) break;
34+
for (int idx : adj[u]) {
35+
auto [pv, v, cap, flow] = edges[idx];
36+
if (cap > flow && dist[v] == -1) {
37+
dist[v] = dist[u] + 1;
38+
par[v] = {u, idx};
39+
q.push(v);
40+
}
41+
}
42+
}
43+
return dist[t] != -1;
44+
}
45+
ll send_one_flow(int s, int t) {
46+
ll new_flow = INF;
47+
for (int u = t; u != s; u = par[u][0]) {
48+
int idx = par[u][1];
49+
auto [pv, v, cap, flow] = edges[idx];
50+
new_flow = min(new_flow, cap - flow);
51+
}
52+
for (int u = t; u != s; u = par[u][0]) {
53+
int idx = par[u][1];
54+
auto [pv, v, cap, flow] = edges[idx];
55+
edges[idx][3] += new_flow;
56+
edges[idx ^ 1][3] -= new_flow;
57+
}
58+
return new_flow;
59+
}
60+
ll edmonds_karp(int s, int t) {
61+
ll max_flow = 0;
62+
while (bfs(s, t)) {
63+
ll new_flow = send_one_flow(s, t);
64+
if (!new_flow) break;
65+
max_flow += new_flow;
66+
}
67+
return max_flow;
68+
}
69+
void dfs(int u = 0) {
70+
ans.push_back(u);
71+
if (u == V - 1) return;
72+
for (int idx : adj[u]) {
73+
auto &[pv, v, cap, flow] = edges[idx];
74+
if (flow == 1 && idx % 2 == 0) {
75+
dfs(v);
76+
flow = 0;
77+
return;
78+
}
79+
}
80+
}
81+
void print_path(int s, int t) {
82+
int max_flow = edmonds_karp(s, t);
83+
cout << max_flow << "\n";
84+
for (int i = 0; i < max_flow; i++) {
85+
dfs();
86+
cout << ans.size() << "\n";
87+
for (int x : ans) cout << x + 1 << " ";
88+
cout << "\n";
89+
ans.clear();
90+
}
91+
}
92+
};
93+
2794
void solve() {
28-
95+
int n, m; cin >> n >> m;
96+
max_flow_graph adj(n);
97+
for (int i = 0; i < m; i++) {
98+
int u, v; cin >> u >> v; u--; v--;
99+
adj.add_edge(u, v);
100+
}
101+
adj.print_path(0, n - 1);
29102
}
30-
103+
31104
int main() {
32105
ios_base::sync_with_stdio(0);
33106
cin.tie(0); cout.tie(0);
34-
//freopen("input.txt", "r", stdin);
35-
//freopen("output.txt", "w", stdout);
36-
37-
int tc; cin >> tc;
107+
// freopen("input.txt", "r", stdin);
108+
// freopen("output.txt", "w", stdout);
109+
110+
int tc; tc = 1;
38111
for (int t = 1; t <= tc; t++) {
39-
//cout << "Case #" << t << ": ";
112+
// cout << "Case #" << t << ": ";
40113
solve();
41114
}
42-
}
115+
}

Distinct Routes/testcases/0.out

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2
2+
3
3+
1 2 6
4+
4
5+
1 3 4 6

Download Speed/main.cpp

+73-74
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,92 @@
11
#include <bits/stdc++.h>
2-
#include <ext/pb_ds/assoc_container.hpp>
3-
#include <ext/pb_ds/tree_policy.hpp>
4-
2+
53
using namespace std;
6-
using namespace __gnu_pbds;
7-
8-
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
9-
10-
const int MAX_N = 1e5 + 5;
11-
const int MAX_L = 20; // ~ Log N
12-
const long long MOD = 1e9 + 7;
13-
const long long INF = 1e9 + 7;
14-
const double EPS = 1e-9;
15-
16-
typedef long long ll;
17-
typedef vector<int> vi;
18-
typedef pair<int,int> ii;
19-
typedef vector<ii> vii;
20-
typedef vector<vi> vvi;
21-
22-
#define LSOne(S) (S & (-S))
23-
#define isBitSet(S, i) ((S >> i) & 1)
24-
25-
// Edmonds_Karp template
4+
5+
#define ar array
6+
#define ll long long
7+
8+
const int MAX_N = 1e5 + 1;
9+
const int MOD = 1e9 + 7;
10+
const int INF = 1e9;
11+
const ll LINF = 1e18;
12+
2613
struct max_flow_graph {
27-
int V;
28-
struct edge {
29-
int src, dst, cap, res;
30-
size_t rev;
31-
};
32-
edge &rev(edge e) {
33-
return adj[e.dst][e.rev];
34-
};
35-
vector<vector<edge>> adj;
36-
max_flow_graph(int V) : V(V), adj(V) {}
37-
void add_edge(int src, int dst, int cap) {
38-
adj[src].push_back({src, dst, cap, 0, adj[dst].size()}); // original edge
39-
adj[dst].push_back({dst, src, 0, 0, adj[src].size() - 1}); // residual back edge (notice cap = 0)
40-
}
41-
ll max_flow(int s, int t) {
42-
for (int u = 0; u < V; u++)
43-
for (auto &e : adj[u])
44-
e.res = e.cap;
45-
ll total = 0;
46-
while (true) {
47-
vector<int> prev(V, -1); prev[s] = -2;
48-
queue<int> q; q.push(s);
49-
while (!q.empty() && prev[t] == -1) {
50-
int u = q.front(); q.pop();
51-
for (edge &e : adj[u]) {
52-
if (prev[e.dst] == -1 && e.res > 0) {
53-
prev[e.dst] = e.rev;
54-
q.push(e.dst);
55-
}
56-
}
57-
}
58-
if (prev[t] == -1) break;
59-
int inc = INF;
60-
for (int u = t; u != s; u = adj[u][prev[u]].dst)
61-
inc = min(inc, rev(adj[u][prev[u]]).res);
62-
for (int u = t; u != s; u = adj[u][prev[u]].dst) {
63-
adj[u][prev[u]].res += inc;
64-
rev(adj[u][prev[u]]).res -= inc;
65-
}
66-
total += inc;
67-
}
68-
return total;
69-
}
14+
int V;
15+
vector<ar<ll,3>> edges; // {dest, cap, flow}
16+
vector<vector<int>> adj;
17+
vector<int> dist;
18+
vector<ar<int,2>> par;
19+
max_flow_graph(int V): V(V), adj(V) {}
20+
void add_edge(int u, int v, int w) {
21+
adj[u].push_back(edges.size());
22+
edges.push_back({v, w, 0});
23+
adj[v].push_back(edges.size());
24+
edges.push_back({u, 0, 0});
25+
}
26+
bool bfs(int s, int t) {
27+
dist.assign(V, -1);
28+
par.assign(V, {-1, -1});
29+
queue<int> q;
30+
dist[s] = 0; q.push(s);
31+
while (q.size()) {
32+
int u = q.front(); q.pop();
33+
if (u == t) break;
34+
for (int idx : adj[u]) {
35+
auto [v, cap, flow] = edges[idx];
36+
if (cap > flow && dist[v] == -1) {
37+
dist[v] = dist[u] + 1;
38+
par[v] = {u, idx};
39+
q.push(v);
40+
}
41+
}
42+
}
43+
return dist[t] != -1;
44+
}
45+
ll send_one_flow(int s, int t) {
46+
ll new_flow = INF;
47+
for (int u = t; u != s; u = par[u][0]) {
48+
int idx = par[u][1];
49+
auto [v, cap, flow] = edges[idx];
50+
new_flow = min(new_flow, cap - flow);
51+
}
52+
for (int u = t; u != s; u = par[u][0]) {
53+
int idx = par[u][1];
54+
auto [v, cap, flow] = edges[idx];
55+
edges[idx][2] += new_flow;
56+
edges[idx ^ 1][2] -= new_flow;
57+
}
58+
return new_flow;
59+
}
60+
ll edmonds_karp(int s, int t) {
61+
ll max_flow = 0;
62+
while (bfs(s, t)) {
63+
ll new_flow = send_one_flow(s, t);
64+
if (!new_flow) break;
65+
max_flow += new_flow;
66+
}
67+
return max_flow;
68+
}
7069
};
71-
70+
7271
void solve() {
7372
int n, m; cin >> n >> m;
7473
max_flow_graph adj(n);
7574
for (int i = 0; i < m; i++) {
7675
int u, v, w; cin >> u >> v >> w; u--; v--;
7776
adj.add_edge(u, v, w);
7877
}
79-
cout << adj.max_flow(0, n - 1) << "\n";
78+
cout << adj.edmonds_karp(0, n - 1) << "\n";
8079
}
81-
80+
8281
int main() {
8382
ios_base::sync_with_stdio(0);
8483
cin.tie(0); cout.tie(0);
85-
//freopen("input.txt", "r", stdin);
86-
//freopen("output.txt", "w", stdout);
87-
84+
// freopen("input.txt", "r", stdin);
85+
// freopen("output.txt", "w", stdout);
86+
8887
int tc; tc = 1;
8988
for (int t = 1; t <= tc; t++) {
90-
//cout << "Case #" << t << ": ";
89+
// cout << "Case #" << t << ": ";
9190
solve();
9291
}
93-
}
92+
}

Police Chase/attic/main.cpp.md5

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5574151354c04ba5ad5cc7cf1294f5b9

Police Chase/attic/sol

556 KB
Binary file not shown.

Police Chase/attic/stderr

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/mnt/e/Compeitive Programming/CSES - CSES Problem Set/Police Chase/main.cpp: In constructor ‘max_flow_graph::max_flow_graph(int)’:
2+
/mnt/e/Compeitive Programming/CSES - CSES Problem Set/Police Chase/main.cpp:19:41: error: expected ‘{’ before ‘vis’
3+
19 | max_flow_graph(int V): V(V), adj(V) vis(V) {}
4+
| ^~~
5+
/mnt/e/Compeitive Programming/CSES - CSES Problem Set/Police Chase/main.cpp: In member function ‘bool max_flow_graph::bfs(int, int)’:
6+
/mnt/e/Compeitive Programming/CSES - CSES Problem Set/Police Chase/main.cpp:35:22: error: only 3 names provided for structured binding
7+
35 | auto [v, cap, flow] = edges[idx];
8+
| ^~~~~~~~~~~~~~
9+
/mnt/e/Compeitive Programming/CSES - CSES Problem Set/Police Chase/main.cpp:35:22: note: while ‘std::array<long long int, 4>’ decomposes into 4 elements

Police Chase/main

82.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)