Submission details
Task:Distinct Routes
Sender:aalto26gm_019
Submission time:2026-10-19 17:46:51 +0300
Language:C++ (C++20)
Status:READY
Result:
Test results
testverdicttime
#10.00 sdetails
#2ACCEPTED0.00 sdetails
#30.01 sdetails
#40.00 sdetails
#5ACCEPTED0.00 sdetails
#60.00 sdetails
#70.00 sdetails
#80.00 sdetails
#90.00 sdetails
#100.00 sdetails
#110.00 sdetails
#120.00 sdetails
#130.00 sdetails
#140.00 sdetails
#150.00 sdetails

Code

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
typedef long long ll;

int n, m;
struct Edge {
    int to;
    int cost;
};
vector<Edge> edges;
vector<vector<int>> adj;
vector<vector<pair<int, bool>>> adj2;
vector<char> vis;
vector<int> path;

void add_edge(int a, int b) {
    adj[a].push_back(edges.size());
    edges.push_back({b, 1});
    adj[b].push_back(edges.size());
    edges.push_back({a, 0});
    adj2[a].push_back({(int)edges.size(), false});
}

ll dfs(int node, int threshold, int flow) {

    if (node == n)
        return flow;

    vis[node] = 1;

    for (int e : adj[node]) {
        Edge &edge = edges[e];

        if (edge.cost < threshold || vis[edge.to])
            continue;

        ll val = dfs(edge.to, threshold, min(flow, edge.cost));
        if (val > 0) {
            edge.cost -= val;
            edges[e ^ 1].cost += val;
            return val;
        }
    }
    return 0;
}

bool simple_dfs(int node, int lenght) {

    if (node == n) {
        cout << lenght << "\n";
        return true;
    }

    vis[node] = 1;

    for (auto &to : adj2[node]) {
        Edge &edge = edges[to.first];

        if (to.second || edge.cost != 0 || vis[edge.to])
            continue;

        if (simple_dfs(edge.to, lenght + 1)) {
            path.push_back(edge.to);
            to.second = true;
            return true;
        }
    }
    return false;
}

ll find() {
    fill(vis.begin(), vis.end(), 0);
    return dfs(1, 1, 1);
}

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

    cin >> n >> m;
    adj = vector<vector<int>>(n + 1);
    adj2 = vector<vector<pair<int, bool>>>(n + 1);
    vis.assign(n + 1, 0);

    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        add_edge(a, b);
    }

    ll flow = -1;
    ll val = 1;

    while (val > 0) {
        flow += val;
        val = find();
    }
    cout << flow << "\n";

    fill(vis.begin(), vis.end(), 0);
    while (simple_dfs(1, 1)) {
        cout << 1 << " ";
        for (int i = path.size() - 1; i >= 0; i--) {
            cout << path[i] << " ";
        }
        path = vector<int>();
        cout << "\n";
        fill(vis.begin(), vis.end(), 0);
    }

    return 0;
}

Test details

Test 1

Verdict:

input
2 1
1 2

correct output
1
2
1 2 

user output
1

Test 2

Verdict: ACCEPTED

input
4 2
1 2
3 4

correct output
0

user output
0

Test 3

Verdict:

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
2
1 500 
2
1 500 
...

Test 4

Verdict:

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

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
12
1 10 27 38 35 24 39 23 6 16 13...

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
9
1 72 66 5 55 38 19 23 75 

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

Test 9

Verdict:

input
3 2
1 2
2 3

correct output
1
3
1 2 3 

user output
1
2
1 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
5
1 3 7 9 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 5 6 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

Test 13

Verdict:

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

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
8
1 87 215 378 159 80 337 500 
8
1 31 476 88 347 452 98 500 
...