CSES - Aalto Competitive Programming 2024 - wk7 - Mon - Results
Submission details
Task:Distinct Routes
Sender:fabiank
Submission time:2024-10-21 16:49:08 +0300
Language:C++ (C++17)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.06 sdetails
#4ACCEPTED0.01 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.00 sdetails
#7ACCEPTED0.00 sdetails
#8ACCEPTED0.00 sdetails
#9ACCEPTED0.00 sdetails
#10ACCEPTED0.00 sdetails
#110.00 sdetails
#12ACCEPTED0.00 sdetails
#13ACCEPTED0.01 sdetails
#14ACCEPTED0.00 sdetails
#150.02 sdetails

Compiler report

input/code.cpp: In function 'std::vector<std::vector<int> > ford_fulkerson(std::vector<std::vector<long long int> >&, int, int)':
input/code.cpp:137:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  137 |         for (int i = 1; i < path_reversed.size(); i++)
      |                         ~~^~~~~~~~~~~~~~~~~~~~~~

Code

#include <bits/stdc++.h>

#define REP(i, a, b) for (int i = a; i < b; i++)

// Type Aliases for 1D and 2D vectors with initialization
#define vll(n, val) vector<long long>(n, val) // 1D vector of long longs with size n, initialized to val
#define ll long long
#define vvi(n, m, val) vector<vector<int>>(n, vector<int>(m, val))              // 2D vector of ints (n x m), initialized to val
#define vvll(n, m, val) vector<vector<long long>>(n, vector<long long>(m, val)) // 2D vector of long longs (n x m), initialized to val

using namespace std;

void print_vector(vector<int> &x)
{
    for (int v : x)
    {
        cout << v << " ";
    }
    cout << "\n";
}

void print_matrix(vector<vector<int>> &matrix)
{
    cout << "\n"
         << "----------------" << "\n";
    for (vector<int> row : matrix)
    {
        print_vector(row);
    }
    cout << "\n"
         << "----------------" << "\n";
}

int calc_max_digit(int n)
{
    int max_digit = 0;
    while (n > 0 && max_digit < 9)
    {
        int digit = n % 10;
        if (digit > max_digit)
        {
            max_digit = digit;
        }
        n /= 10;
    }
    return max_digit;
}

// edges as edge list for outgoing node as pairs (end, cost)
vector<ll> dijkstras(int start_point, vector<vector<pair<int, int>>> edges)
{
    int n = edges.size();
    vector<bool> processed(n, false);
    vector<ll> distances(n, LLONG_MAX);
    distances[start_point] = 0;
    priority_queue<pair<ll, int>> pq;
    pq.push({0, start_point});
    while (!pq.empty())
    {
        int curr = pq.top().second;
        pq.pop();
        if (processed[curr])
        {
            continue;
        }
        processed[curr] = true;
        ll distance = distances[curr];

        for (pair<int, int> edge : edges[curr])
        {

            if (distance + edge.second < distances[edge.first])
            {
                distances[edge.first] = distance + edge.second;
                pq.push({-distances[edge.first], edge.first});
            }
        }
    }
    return distances;
}

int bfs_edmondson_karp(const vector<vector<ll>> &connections,
                       const int source, const int target, vector<int> &path_reversed)
{
    int n = connections.size();

    queue<pair<int, ll>> queue;
    queue.push({source, LLONG_MAX});
    vector<int> predecessor(n, -2);
    predecessor[source] = -1;

    while (!queue.empty())
    {
        int current = queue.front().first;
        ll current_bottleneck = queue.front().second;
        queue.pop();

        if (current == target)
        {
            while (current != -1)
            {
                path_reversed.push_back(current);
                current = predecessor[current];
            }
            return current_bottleneck;
        }

        for (int edge_end = 0; edge_end < n; edge_end++)
        {
            ll edge_cap = connections[current][edge_end];
            if (edge_cap > 0 && predecessor[edge_end] == -2)
            {
                predecessor[edge_end] = current;
                queue.push({edge_end, min(current_bottleneck, edge_cap)});
            }
        }
    }

    return -1;
}

vector<vector<int>> ford_fulkerson(vector<vector<ll>> &residual_graph, const int source, const int target)
{
    vector<vector<int>> results;

    while (true)
    {
        vector<int> path_reversed;
        int path_capacity = bfs_edmondson_karp(residual_graph, source, target, path_reversed);

        if (path_capacity < 0)
        {
            break;
        }
        results.push_back(path_reversed);

        for (int i = 1; i < path_reversed.size(); i++)
        {
            int edge_end = path_reversed[i - 1];
            int edge_start = path_reversed[i];
            // reduce forwards edge
            residual_graph[edge_start][edge_end] -= path_capacity;
            // assert(residual_graph[edge_start][edge_end] >= 0);
            // add to backwards edge
            residual_graph[edge_end][edge_start] += path_capacity;
            // assert(residual_graph[edge_end][edge_start] >= 0);
        }
    }
    return results;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;

    vector<vector<ll>> adj_matrix(n + 1, vector<ll>(n + 1, 0));
    for (int i = 1; i <= m; i++)
    {
        int start, end;
        cin >> start >> end;
        adj_matrix[start][end] = 1;
    }

    vector<vector<int>> results = ford_fulkerson(adj_matrix, 1, n);
    cout << results.size() << "\n";

    for (auto path : results)
    {
        cout << path.size() << "\n";
        for (int i = path.size() - 1; i >= 0; i--)
        {
            cout << path[i] << " ";
        }
        cout << "\n";
    }
}

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