Submission details
Task:Distinct Routes
Sender:aalto26gm_009
Submission time:2026-10-19 17:45:47 +0300
Language:C++ (C++20)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.01 sdetails
#4ACCEPTED0.01 sdetails
#5ACCEPTED0.00 sdetails
#60.01 sdetails
#70.01 sdetails
#80.01 sdetails
#9ACCEPTED0.00 sdetails
#10ACCEPTED0.00 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.00 sdetails
#130.00 sdetails
#140.00 sdetails
#150.01 sdetails

Compiler report

input/code.cpp: In function 'long long int findPath(std::vector<std::vector<std::pair<long long int, long long int> > >&, std::vector<std::vector<long long int> >&, std::vector<bool>&, long long int, long long int, long long int)':
input/code.cpp:16:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |     for (long long i=0;i<g[node].size();i++){
      |                        ~^~~~~~~~~~~~~~~
input/code.cpp: In function 'bool checkPaths(std::vector<long long int>)':
input/code.cpp:53:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |         for (;i<path.size();i++){
      |               ~^~~~~~~~~~~~
input/code.cpp:58:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vect...

Code

#include <iostream>
#include <vector>
#include <climits>

using namespace std;

vector<vector<long long>> pathList(0, vector<long long>(0));

long long findPath(vector<vector<pair<long long,long long>>>& g, vector<vector<long long>> & rev, vector<bool>& visited, long long node, long long sink, long long flow){
    if (node==sink){
        return flow;
    }

    visited[node]=true;

    for (long long i=0;i<g[node].size();i++){
        long long next=g[node][i].first;
        long long cap=g[node][i].second;

        if (!visited[next] && cap>0){
            long long pushed = findPath(g, rev, visited, next, sink, min(flow, cap));

            if (pushed>0){
                g[node][i].second-=pushed;
                g[next][rev[node][i]].second+=pushed;
                return pushed;
            }
        }
    }
    return 0;
}

long long maxFlow(vector<vector<pair<long long,long long>>> & g, vector<vector<long long>>& rev, long long source, long long sink){
    long long totalFlow=0;

    while (true){
        vector<bool> visited(g.size(), false);
        long long pushed = findPath(g, rev, visited, source, sink, LLONG_MAX);
        if (pushed==0){
            break;
        }
        totalFlow+=pushed;
    }
    return totalFlow;
}

bool checkPaths(vector<long long> path){
    for (auto & p: pathList){
        if (p.size()!=path.size()) {
            continue;
        }
        int i=0;
        for (;i<path.size();i++){
            if (p[i]!=path[i]){
                break;
            }
        }
        if (i==path.size()){
            return false;
        }
    }
    return true;
}

bool findPaths(vector<vector<pair<long long, long long>>> & g, vector<long long>& path, vector<bool> & visited, long long node, long long start, long long sink, vector<bool> &visitedTmp){
    if (node==sink){
        if (checkPaths(path)) {

            for (long long i: path){
                visited[i]=true;
            }
            pathList.push_back(path);
            return true;
        } else {
            return false;
        }
    }
    if (visitedTmp[node]){
        return false;
    }
    if (visited[node]&& node!=start){
        return false;
    }
    visitedTmp[node]=true;
    path.push_back(node);
    for (auto next: g[node]){
        if (next.second==0 && !visited[next.first]){
            if (findPaths(g, path, visited, next.first, start, sink, visitedTmp)){
                return true;
            }
            
        }
    }
    visitedTmp[node]=false;
    path.pop_back();
    return false;
}

int main(){
    long long n, m;
    cin >> n >> m;
    //node, capacity
    vector<vector<pair<long long,long long>>> g(n+1, vector<pair<long long,long long>>(0));
    vector<vector<long long>> rev(n+1, vector<long long>(0));
    for (long long i=0;i<m;i++){
        long long from, to;
        cin >> from >> to;


        rev[from].push_back(g[to].size());
        rev[to].push_back(g[from].size());

        g[from].push_back({to,1});
        g[to].push_back({from,0});

    }

    long long paths=maxFlow(g, rev, 1, n);

    cout << paths << '\n';
    vector<bool> visited(n+1);
    for (long long i=0;i<paths;i++){
        vector<long long> path(0);
        vector<bool> visitedTmp(n+1);
        findPaths(g, path, visited, 1, 1, n, visitedTmp);
    }

    for (auto & p : pathList){
        cout << p.size()+1 << '\n';
        for (int i: p){
            cout << i << ' ';
        }
        cout << n << '\n';
    }
    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
...

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 ...

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
20
1 20 30 31 8 33 5 24 15 23 32 ...

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
3
1 29 75
67
1 70 46 66 42 21 17 58 37 74 3...

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
10
1 96 67 13 51 76 82 7 92 100
58
1 35 15 86 20 39 5 62 25 88 49...

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

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