Submission details
Task:Distinct Routes
Sender:aalto26gm_017
Submission time:2026-10-19 17:29:32 +0300
Language:Python3 (PyPy3)
Status:READY
Result:
Test results
testverdicttime
#10.05 sdetails
#2ACCEPTED0.05 sdetails
#30.07 sdetails
#40.05 sdetails
#5ACCEPTED0.05 sdetails
#60.67 sdetails
#70.96 sdetails
#8--details
#90.05 sdetails
#10ACCEPTED0.05 sdetails
#11ACCEPTED0.05 sdetails
#12ACCEPTED0.05 sdetails
#13ACCEPTED0.05 sdetails
#14--details
#15--details

Code

import sys
from collections import deque, defaultdict, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_left, bisect_right
from itertools import permutations, combinations, product, accumulate, groupby
from functools import lru_cache, cmp_to_key, reduce
from math import gcd, isqrt, comb, inf, pi, sqrt, atan2, hypot


def main():
    data = sys.stdin.buffer.read().split(); idx = 0
    # n = int(data[idx]); idx += 1
    n, m = map(int, data[idx:idx + 2]); idx += 2
    # a = list(map(int, data[idx:idx + n])); idx += n
    # s = data[idx].decode(); idx += 1                  # strings

    adj = [[] for i in range(n+1)]
    for i in range(m):
        a, b = map(int, data[idx:idx + 2]); idx += 2
        adj[a].append((b,i))


    # working_paths_node =  [[] for i in range(n+1)]
    all_paths = []

    used = set()
    
    taken_path = []
    def dfs(node):

        taken_path.append(node)
        # print(f"{node=}. {taken_path=}")
        if node == n:
            all_paths.append((taken_path.copy(), used.copy()))
            taken_path.pop()
            return

        neighbours = adj[node]
        for newnode, path in neighbours:
            if path in used: continue
            used.add(path)
            dfs(newnode)
            used.remove(path)

        taken_path.pop()

    dfs(1)
    out = []
    # print(all_paths)


    # for combi in combinations():

    #     bool(set() & set(b))
    for i in range(len(all_paths)-1, -1, -1):
        found = False
        for combo in combinations(all_paths, i):
            # sets = [a[1] for a in combo]
            used = set()
            valid = True
            for lst, sett in combo:
                if bool(used & sett):
                    valid = False
                    break
                else:
                    used = used | sett
            if valid:
                found = True
                for lst, sett in combo:
                    out.append(lst)
                break
        if found: break



    # g = [data[idx + i].decode() for i in range(n)]; idx += n  # grid of n rows, no spaces, strings

    # input = sys.stdin.readline
    # n, m = map(int, input().split())
    # s = input().strip()  # strip removes the trailing newline
    # for line in sys.stdin:  # read until EOF



    print(len(out))
    for paths in out:
        print(len(paths))
        print(" ".join(map(str, paths)))
    # sys.stdout.write("\n".join(map(str, out)) + "\n")  # one value per line
    # print(*out)                                      # space separated on one line
    # print(f"{ans:.6f}")                              # float with 6 decimals
    # print("\n".join("".join(row) for row in g))      # grid of chars


main()

Test details

Test 1

Verdict:

input
2 1
1 2

correct output
1
2
1 2 

user output
0

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
497
3
1 2 500
3
1 3 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
0

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
(empty)

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
(empty)

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
(empty)

Test 9

Verdict:

input
3 2
1 2
2 3

correct output
1
3
1 2 3 

user output
0

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: 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
(empty)

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
(empty)