CSES - Aalto Competitive Programming 2024 - wk7 - Mon - Results
Submission details
Task:Distinct Routes
Sender:louaha1
Submission time:2024-10-21 17:43:27 +0300
Language:C++ (C++11)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.01 sdetails
#4ACCEPTED0.01 sdetails
#50.00 sdetails
#60.01 sdetails
#70.01 sdetails
#80.01 sdetails
#9ACCEPTED0.00 sdetails
#100.00 sdetails
#11ACCEPTED0.01 sdetails
#120.00 sdetails
#13ACCEPTED0.00 sdetails
#140.00 sdetails
#150.01 sdetails

Code

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;

const int MAXN = 501;
const int INF = 1e9;

int capacity[MAXN][MAXN];  // Capacity for each teleporter
int parent[MAXN];          // To reconstruct the path
vector<int> adj[MAXN];     // Adjacency list for the graph

// BFS function to find an augmenting path and calculate the flow
int bfs(int n) {
    memset(parent, -1, sizeof(parent));
    parent[1] = -2;  // Start from room 1
    queue<pair<int, int>> q;
    q.push({1, INF});

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

        for (int v : adj[u]) {
            if (parent[v] == -1 && capacity[u][v]) {  // If v is not yet visited and there is capacity
                parent[v] = u;
                int new_flow = min(flow, capacity[u][v]);
                if (v == n) {
                    return new_flow;  // Found an augmenting path to room n
                }
                q.push({v, new_flow});
            }
        }
    }

    return 0;  // No augmenting path found
}

// Edmonds-Karp algorithm to compute the maximum flow
int maxflow(int n) {
    int flow = 0, new_flow;

    while ((new_flow = bfs(n))) {
        flow += new_flow;
        int u = n;

        // Update the capacities along the path
        while (u != 1) {
            int p = parent[u];
            capacity[p][u] -= new_flow;
            capacity[u][p] += new_flow;
            u = p;
        }
    }

    return flow;
}

// Function to find the actual paths based on residual capacities
void find_paths(int n, int m) {
    vector<vector<int>> paths;
    while (true) {
        vector<int> path;
        int u = 1;
        while (u != n) {
            bool found = false;
            for (int v : adj[u]) {
                if (capacity[u][v] == 0 && capacity[v][u] == 1) {  // Residual capacity indicates part of the path
                    path.push_back(u);
                    capacity[v][u] = 0;  // Remove this edge from the path
                    u = v;
                    found = true;
                    break;
                }
            }
            if (!found) break;
        }
        path.push_back(n);
        if (path.size() > 1) {
            paths.push_back(path);
        } else {
            break;
        }
    }

    // Output the results
    cout << paths.size() << endl;
    for (const auto &p : paths) {
        cout << p.size() << endl;
        for (int room : p) {
            cout << room << " ";
        }
        cout << endl;
    }
}

int main() {
    int n, m;
    cin >> n >> m;

    // Initialize the capacity and adjacency list
    memset(capacity, 0, sizeof(capacity));
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);  // Add reverse edge for the residual graph
        capacity[a][b] = 1;   // Each teleporter has a capacity of 1 (can be used once)
    }

    // Find the maximum flow (number of edge-disjoint paths)
    maxflow(n);

    // Find and output the paths
    find_paths(n, m);

    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:

input
2 1
2 1

correct output
0

user output
1
2
1 2 

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
22
29
1 20 8 33 5 7 32 14 28 25 10 2...
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
18
27
1 29 56 53 27 13 16 72 4 33 34...
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
16
30
1 96 67 36 29 67 65 27 98 84 5...
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:

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 
6
1 6 7 8 9 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
4
1 3 2 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:

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
3
3
1 2 7 
2
1 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
5
1 129 44 372 500 
5
1 63 311 141 500 
...
Truncated