Skip to content

Commit 8a26ade

Browse files
committed
Slight optimized
1 parent acb6b48 commit 8a26ade

1 file changed

Lines changed: 21 additions & 17 deletions

File tree

3/tournament.cpp

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,30 @@
1313
#include <iostream>
1414
#include <limits>
1515
#include <queue>
16-
#include <tuple>
1716
#include <vector>
1817

1918
using namespace std;
2019

2120
constexpr int32_t INF = numeric_limits<int32_t>::max();
2221

2322
struct Edge {
24-
int to; // Destination node of the edge
25-
int capacity; // Maximum capacity of the edge
26-
int flow; // Current flow through the edge
27-
int reverse_edge; // Index of the reverse edge in the 'to' node's adjacency list
23+
uint32_t to; // Destination node of the edge
24+
uint32_t reverse_edge; // Index of the reverse edge in the 'to' node's adjacency list
25+
int capacity; // Maximum capacity of the edge
26+
int flow; // Current flow through the edge
2827
};
2928

30-
void add_edge(vector<vector<Edge>>& adj, int u, int v, int capacity) {
31-
adj[u].emplace_back(v, capacity, 0, (int)adj[v].size());
32-
adj[v].emplace_back(u, 0, 0, (int)adj[u].size() - 1);
29+
namespace {
30+
constexpr void add_edge(vector<vector<Edge>>& adj, uint32_t u, uint32_t v, int capacity) {
31+
adj[u].emplace_back(v, adj[v].size(), capacity, 0);
32+
adj[v].emplace_back(u, adj[u].size() - 1, 0, 0);
3333
}
3434

35-
int bfs(int s, int t, span<int> parent_node, span<int> parent_edge_index, span<vector<Edge>> adj) {
35+
int bfs(uint32_t s,
36+
uint32_t t,
37+
span<int32_t> parent_node,
38+
span<uint32_t> parent_edge_index,
39+
span<vector<Edge>> adj) {
3640
fill(parent_node.begin(), parent_node.end(), -1);
3741
queue<pair<int, int>> q;
3842
q.emplace(s, INF);
@@ -46,7 +50,7 @@ int bfs(int s, int t, span<int> parent_node, span<int> parent_edge_index, span<v
4650
const Edge& edge = adj[u][i];
4751
int v = edge.to;
4852

49-
if (parent_node[v] == -1 && edge.capacity - edge.flow > 0) {
53+
if (parent_node[v] < 0 && edge.capacity - edge.flow > 0) {
5054
parent_node[v] = u;
5155
parent_edge_index[v] = i;
5256
int new_flow = min(f, edge.capacity - edge.flow);
@@ -60,9 +64,9 @@ int bfs(int s, int t, span<int> parent_node, span<int> parent_edge_index, span<v
6064
return 0;
6165
}
6266

63-
int64_t maxflow(int s, int t, size_t n, span<vector<Edge>> adj) {
64-
vector<int> parent_node(n);
65-
vector<int> parent_edge_index(n);
67+
int64_t maxflow(uint32_t s, uint32_t t, size_t n, span<vector<Edge>> adj) {
68+
vector<int32_t> parent_node(n);
69+
vector<uint32_t> parent_edge_index(n);
6670

6771
int64_t total_flow = 0;
6872
int64_t new_flow;
@@ -88,10 +92,11 @@ int64_t maxflow(int s, int t, size_t n, span<vector<Edge>> adj) {
8892

8993
return total_flow;
9094
}
95+
}
9196

9297
int main() {
9398
ios_base::sync_with_stdio(false);
94-
cin.tie(NULL);
99+
cin.tie(nullptr);
95100

96101
size_t n;
97102
cin >> n;
@@ -129,8 +134,7 @@ int main() {
129134

130135
for (size_t i = 2; i <= n; ++i) {
131136
limits[i] = max_possible_victories_team_1 - wins[i] - 1;
132-
auto already_lost = limits[i] < 0;
133-
if (already_lost) {
137+
if (auto already_lost = limits[i] < 0; already_lost) {
134138
cout << "false\n";
135139
return EXIT_SUCCESS;
136140
}
@@ -199,5 +203,5 @@ int main() {
199203
cout << "false\n";
200204
}
201205

202-
return 0;
206+
return EXIT_SUCCESS;
203207
}

0 commit comments

Comments
 (0)