CSES - Aalto Competitive Programming 2024 - wk7 - Mon - Results
Submission details
Task:Distinct Routes
Sender:arnxxau
Submission time:2024-10-21 17:15:36 +0300
Language:C++ (C++20)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.01 sdetails
#4ACCEPTED0.02 sdetails
#5ACCEPTED0.00 sdetails
#60.01 sdetails
#70.01 sdetails
#80.01 sdetails
#9ACCEPTED0.00 sdetails
#10ACCEPTED0.00 sdetails
#11ACCEPTED0.00 sdetails
#120.00 sdetails
#13ACCEPTED0.00 sdetails
#140.00 sdetails
#150.02 sdetails

Compiler report

input/code.cpp: In function 'int Ford_Fulkerson(int, int, int, std::vector<std::vector<int> >&)':
input/code.cpp:58:20: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   58 |     while (min_cap = bfs(source, target, n, parent, graph)) {
      |            ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
input/code.cpp: In function 'bool residual_dfs(std::vector<std::vector<int> >&, std::vector<int>&, int, int)':
input/code.cpp:89:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |     for (int v = 0; v < graph.size(); v++) {
      |                     ~~^~~~~~~~~~~~~~
input/code.cpp: In function 'int main()':
input/code.cpp:121:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |     for (int i = 0; i < path.size(); i++...

Code

#include <climits>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int bfs(int source, int target, int n, vector<int>& parent, vector<vector<int>>& graph) {
    // Update the parent vector as each node value to be -1
    fill(parent.begin(), parent.end(), -1);
    // parent of source node to be -2
    parent[source] = -2;
    // Initializing queue for storing and min capacity so far
    queue<pair<int, int>> q;
    // Source node min capacity to be 1e9
    q.push({source, 1e9});

    // Looping while queue is not empty
    while (!q.empty()) {
        // storing top node and min capacity so far
        int u = q.front().first;
        int cap = q.front().second;
        // Removing top node from queue
        q.pop();
        // Looping all edges from u
        for (int v = 0; v < n; v++) {
            // finding v node has edge from u
            if (u != v && graph[u][v] != 0 && parent[v] == -1) {
                // storing parent v to be u
                parent[v] = u;
                // Updating the minimum capacity
                int min_cap = min(cap, graph[u][v]);
                // If v is the target node then return minimum capacity
                if (v == target) {
                    return min_cap;
                }
                // if we didn't find target node
                // Insert the v node and minimum capacity so far in queue
                q.push({v, min_cap});
            }
        }
    }
    // if we didn't find path between source to target return 0
    return 0;
}

int Ford_Fulkerson(int source, int target, int n, vector<vector<int>>& graph) {
    // Initializing parent vector for finding the path from source to target
    // In which we add parent of the node
    vector<int> parent(n, -1);
    // Initializing maximum flow for storing ans
    int max_flow = 0;
    int min_cap = 0;  // storing minimum capacity in each path

    // looping while minimum capacity become zero
    // For finding path and minimum capacity we call function bfs()
    while (min_cap = bfs(source, target, n, parent, graph)) {
        // Adding the min_cap from this path
        max_flow += min_cap;
        // storing target node in v
        int v = target;

        // while we didn't find the source node
        // Looping through path stored in parent
        while (v != source) {
            // finding parent of v node
            int u = parent[v];
            // Subtracting minimum capacity from u to v
            // And adding minimum capacity from v to u
            graph[u][v] -= min_cap;
            graph[v][u] += min_cap;
            v = u;
        }
    }
    // Returning maximum flow in the graph
    return max_flow;
}

void addEdge(vector<vector<int>>& graph,
             int u, int v, int w) {
    graph[u][v] = w;
}

bool residual_dfs(vector<vector<int>>& graph, vector<int>& path, int u, int end) {
    path.push_back(u);
    if (u == end) return true;

    for (int v = 0; v < graph.size(); v++) {
        auto it = find(path.begin(), path.end(), v);

        if (graph[v][u] == 1 and it==path.end()) {
            graph[v][u] = 0;
            if (residual_dfs(graph, path, v, end)) return true;
        }
    }
    path.pop_back();
    return false;
}

int main() {
    int n, m;

    cin >> n >> m;
    vector<vector<int>> graph(n, vector<int>(n, 0));
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        addEdge(graph, a - 1, b - 1, 1);
    }

    int k = Ford_Fulkerson(0, n - 1, n, graph);

    cout << k << endl;

    for (int x = 0; x < k; x++)
    {
    vector<int> path;
    residual_dfs(graph, path, 0, n - 1);
    cout << path.size() << endl;
    for (int i = 0; i < path.size(); i++) {
        cout << path[i] + 1 << ' ';
    }
    cout << endl;
    }
    


    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:

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
39
1 2 4 5 6 3 7 8 9 10 12 11 13 ...
Truncated

Test 7

Verdict:

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
69
1 3 12 4 19 2 6 14 38 9 13 16 ...
Truncated

Test 8

Verdict:

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
91
1 4 3 22 10 11 20 24 17 2 16 7...
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:

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
0

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:

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
4
1 2 5 7 
3
1 4 7 

Test 15

Verdict:

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
6
1 3 102 455 333 500 
5
1 10 264 229 500 
...
Truncated