CSES - Aalto Competitive Programming 2024 - wk7 - Mon - Results
Submission details
Task:Distinct Routes
Sender:MallocManfred
Submission time:2024-10-21 17:44:17 +0300
Language:C++ (C++20)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.01 sdetails
#4ACCEPTED0.01 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.00 sdetails
#7ACCEPTED0.00 sdetails
#8ACCEPTED0.00 sdetails
#9ACCEPTED0.01 sdetails
#10ACCEPTED0.00 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.00 sdetails
#13ACCEPTED0.00 sdetails
#14ACCEPTED0.00 sdetails
#15ACCEPTED0.01 sdetails

Compiler report

input/code.cpp: In function 'long long int maxflow(long long int, long long int)':
input/code.cpp:75:21: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   75 |     while (new_flow = bfs(s, t, parent)) {
      |            ~~~~~~~~~^~~~~~~~~~~~~~~~~~~

Code

#include <bits/stdc++.h>

using namespace std;
#define int long long
#define vout(x) for(int i=0;i<(long long)x.size();i++) printf("%lld ",x[i]);

// g++ <filename>.cpp -g -Wall -Wextra -DDEBUG -o <executable>

// copied from: https://codeforces.com/blog/entry/79024
// === Debug macro starts here ===

int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) {++recur_depth; auto x_=x; --recur_depth; cerr<<string(recur_depth, '\t')<<"\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<#x<<" = "<<x_<<"\e[39m"<<endl;}
#else
#define dbg(x)
#endif
template<typename Ostream, typename Cont>
typename enable_if<is_same<Ostream,ostream>::value, Ostream&>::type operator<<(Ostream& os,  const Cont& v){
	os<<"[";
	for(auto& x:v){os<<x<<", ";}
	return os<<"]";
}
template<typename Ostream, typename ...Ts>
Ostream& operator<<(Ostream& os,  const pair<Ts...>& p){
	return os<<"{"<<p.first<<", "<<p.second<<"}";
}

// === Debug macro ends here ===

const int INF = LONG_MAX;

int n, m;
const int maxN = 501;
const int maxM = 1001;
int capacity[maxN][maxN];
vector<int> adj[maxN];
vector<pair<int,int>> adj_dir[maxN];
vector<int> path;
bool visited[maxM];

// code taken and adapted from https://cp-algorithms.com/graph/edmonds_karp.html
int bfs(int s, int t, vector<int>& parent) {
    
    fill(parent.begin(), parent.end(), -1);
    parent[s] = -2;
    queue<pair<int, int>> q;
    q.push({s, INF});

    while (!q.empty()) {
        int cur = q.front().first;
        int flow = q.front().second;
        q.pop();

        for (int next : adj[cur]) {
            if (parent[next] == -1 && capacity[cur][next]) {
                parent[next] = cur;
                int new_flow = min(flow, capacity[cur][next]);
                if (next == t) 
                    return new_flow;
                q.push({next, new_flow});
            }
        }
    }
    return 0;
}

// code taken and adapted from https://cp-algorithms.com/graph/edmonds_karp.html
int maxflow(int s, int t) {

    int flow = 0;
    vector<int> parent(n + 1);

    int new_flow;
    while (new_flow = bfs(s, t, parent)) {
        flow += new_flow;
        int cur = t;
        while (cur != s) {
            int prev = parent[cur];
            capacity[prev][cur] -= new_flow;
            capacity[cur][prev] += new_flow;
            cur = prev;
        }
    }
    return flow;
}

void dfs(int current_node) {

    path.push_back(current_node);

    if (current_node == n) return;

    for (auto neighbour : adj_dir[current_node]) {

        int n_node = neighbour.first;
        int edge_idx = neighbour.second;

        if (capacity[current_node][n_node] == 0 && !visited[edge_idx]) {
            visited[edge_idx] = true;
            dfs(n_node);
            return;
        }
    }
}

signed main() {

    cin >> n >> m;

    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        capacity[a][b] = 1;
        adj[a].push_back(b);
        adj[b].push_back(a);
        adj_dir[a].push_back(make_pair(b, i));
    }

    int source = 1;
    int sink = n;
    int distinct_routes = maxflow(source, sink);

    cout << distinct_routes << "\n";

    for (int i = 0; i < distinct_routes; i++) {
        path.clear();
        dfs(1);
        cout << path.size() << "\n";
        vout(path);
        cout << "\n";
    }


    return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
2 1
1 2

correct output
1
2
1 2 

user output
1
2
1 2 

Test 2

Verdict: ACCEPTED

input
4 2
1 2
3 4

correct output
0

user output
0

Test 3

Verdict: ACCEPTED

input
500 996
1 2
2 500
1 3
3 500
...

correct output
498
3
1 2 500 
3
1 3 500 
...

user output
498
3
1 2 500 
3
1 3 500 
...
Truncated

Test 4

Verdict: ACCEPTED

input
500 499
1 2
2 3
3 4
4 5
...

correct output
1
500
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

user output
1
500
1 2 3 4 5 6 7 8 9 10 11 12 13 ...
Truncated

Test 5

Verdict: ACCEPTED

input
2 1
2 1

correct output
0

user output
0

Test 6

Verdict: ACCEPTED

input
40 1000
25 22
15 24
7 33
16 32
...

correct output
21
44
1 35 39 34 29 32 22 38 20 30 1...

user output
21
3
1 35 40 
3
1 3 40 
...
Truncated

Test 7

Verdict: ACCEPTED

input
75 1000
72 6
46 66
63 45
70 46
...

correct output
12
30
1 29 24 9 18 63 45 31 66 72 6 ...

user output
12
5
1 29 49 14 75 
4
1 70 12 75 
...
Truncated

Test 8

Verdict: ACCEPTED

input
100 1000
75 97
7 62
88 25
36 44
...

correct output
9
51
1 35 15 86 79 34 43 94 83 75 9...

user output
9
4
1 35 23 100 
5
1 62 25 28 100 
...
Truncated

Test 9

Verdict: ACCEPTED

input
3 2
1 2
2 3

correct output
1
3
1 2 3 

user output
1
3
1 2 3 

Test 10

Verdict: ACCEPTED

input
11 12
1 2
2 3
3 4
4 5
...

correct output
2
6
1 2 3 4 5 11 
7
1 6 7 8 9 10 11 

user output
2
6
1 2 3 4 5 11 
7
1 6 7 8 9 10 11 

Test 11

Verdict: ACCEPTED

input
8 9
1 2
2 3
3 8
1 4
...

correct output
2
5
1 2 6 7 8 
5
1 4 5 3 8 

user output
2
5
1 2 6 7 8 
5
1 4 5 3 8 

Test 12

Verdict: ACCEPTED

input
8 9
1 2
1 3
2 3
3 4
...

correct output
1
8
1 2 3 4 5 6 7 8 

user output
1
7
1 3 4 5 6 7 8 

Test 13

Verdict: ACCEPTED

input
7 9
1 2
1 3
2 7
3 4
...

correct output
3
3
1 2 7 
4
1 3 5 7 
...

user output
3
3
1 2 7 
4
1 3 4 7 
...

Test 14

Verdict: ACCEPTED

input
7 15
3 6
5 2
5 4
3 5
...

correct output
2
5
1 2 3 6 7 
4
1 4 5 7 

user output
2
5
1 2 3 6 7 
4
1 4 5 7 

Test 15

Verdict: ACCEPTED

input
500 986
244 252
224 22
81 484
273 432
...

correct output
116
5
1 129 142 473 500 
5
1 63 158 171 500 
...

user output
116
5
1 129 142 473 500 
5
1 63 158 171 500 
...
Truncated