CSES - Aalto Competitive Programming 2024 - wk7 - Mon - Results
Submission details
Task:Distinct Routes
Sender:arnxxau
Submission time:2024-10-21 17:43:54 +0300
Language:C++ (C++20)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.01 sdetails
#4ACCEPTED0.01 sdetails
#5ACCEPTED0.01 sdetails
#6ACCEPTED0.00 sdetails
#7ACCEPTED0.00 sdetails
#8ACCEPTED0.00 sdetails
#9ACCEPTED0.00 sdetails
#10ACCEPTED0.00 sdetails
#110.00 sdetails
#12ACCEPTED0.00 sdetails
#13ACCEPTED0.00 sdetails
#14ACCEPTED0.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:100:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |     for (int v = 0; v < graph.size(); v++) {
      |                     ~~^~~~~~~~~~~~~~
input/code.cpp: In function 'int main()':
input/code.cpp:142:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  142 |         for (int i = 0; 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;
}
vector<vector<int>> paths;
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)) {
        vector<int> path;
        int x = target;
        while (x != source) {
            // cout << '<' << x << endl;
            path.push_back(x);
            x = parent[x];
        }
        path.push_back(0);
        reverse(path.begin(), path.end());
        paths.push_back(path);

        // 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;
        }

    */

    if (k > 0) {
        for (int i = 0; i < paths.size(); i++) {
            cout << paths[i].size() << endl;
            for (int j = 0; j < paths[i].size(); j++) {
                cout << paths[i][j] + 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: 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 3 40 
3
1 4 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
3
1 54 75 
4
1 3 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
2
1 100 
4
1 6 75 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:

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
4
1 2 3 8 
8
1 4 5 3 2 6 7 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
4
1 2 5 7 
7
1 4 5 2 3 6 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
4
1 10 438 500 
4
1 56 132 500 
...
Truncated