Submission details
Task:Snakeless path
Sender:kookinnam
Submission time:2025-10-20 17:04:14 +0300
Language:Python3 (PyPy3)
Status:READY
Result:
Test results
testverdicttime
#10.06 sdetails
#20.06 sdetails
#30.06 sdetails
#40.06 sdetails
#50.06 sdetails
#6ACCEPTED0.05 sdetails
#70.06 sdetails
#80.07 sdetails
#9ACCEPTED0.05 sdetails
#100.05 sdetails
#11ACCEPTED0.05 sdetails
#12ACCEPTED0.05 sdetails
#130.05 sdetails
#14ACCEPTED0.05 sdetails
#15ACCEPTED0.05 sdetails
#160.06 sdetails
#170.07 sdetails
#18ACCEPTED0.05 sdetails
#190.06 sdetails
#200.05 sdetails
#210.05 sdetails
#220.05 sdetails
#230.05 sdetails
#24ACCEPTED0.05 sdetails
#25ACCEPTED0.05 sdetails
#260.06 sdetails
#270.06 sdetails
#280.05 sdetails
#290.06 sdetails
#300.06 sdetails
#310.06 sdetails
#320.06 sdetails
#330.06 sdetails
#340.06 sdetails
#35ACCEPTED0.06 sdetails
#36ACCEPTED0.06 sdetails
#370.07 sdetails
#38ACCEPTED0.05 sdetails
#390.07 sdetails
#400.07 sdetails
#410.06 sdetails
#420.06 sdetails
#430.06 sdetails
#44ACCEPTED0.06 sdetails
#45ACCEPTED0.06 sdetails
#46ACCEPTED0.06 sdetails
#470.07 sdetails
#48ACCEPTED0.06 sdetails
#490.07 sdetails
#500.14 sdetails
#510.14 sdetails
#520.14 sdetails
#530.14 sdetails
#54ACCEPTED0.14 sdetails
#55ACCEPTED0.13 sdetails
#56ACCEPTED0.14 sdetails
#570.08 sdetails
#580.14 sdetails
#590.09 sdetails
#600.16 sdetails
#610.08 sdetails
#620.08 sdetails
#63ACCEPTED0.14 sdetails
#640.14 sdetails
#65ACCEPTED0.14 sdetails
#66ACCEPTED0.13 sdetails
#67ACCEPTED0.13 sdetails
#68ACCEPTED0.14 sdetails
#690.08 sdetails
#70--details
#71--details
#72--details
#73--details
#74--details
#75--details
#76--details
#770.18 sdetails
#780.11 sdetails
#790.18 sdetails
#800.17 sdetails
#810.18 sdetails
#820.18 sdetails
#83--details
#84--details
#85--details
#86--details
#87--details
#880.12 sdetails
#890.18 sdetails

Code

from collections import deque


def find_snakeless_path(n, snake_edges):
    snake_set = set()
    for a, b in snake_edges:
        snake_set.add((a, b))
        snake_set.add((b, a))

    # Build adjacency list
    adj = [[] for _ in range(n + 1)]
    for u in range(1, n + 1):
        for v in range(1, n + 1):
            if u != v and (u, v) not in snake_set:
                adj[u].append(v)

    # BFS to find path from 1 to n
    queue = deque([1])
    visited = [False] * (n + 1)
    parent = [-1] * (n + 1)
    visited[1] = True

    while queue:
        node = queue.popleft()
        if node == n:
            break
        for nxt in adj[node]:
            if not visited[nxt]:
                visited[nxt] = True
                parent[nxt] = node
                queue.append(nxt)

    # unable to reach the end
    if not visited[n]:
        print(0)
        return

    # Reconstruct path
    path = []
    cur = n
    while cur != -1:
        path.append(cur)
        cur = parent[cur]
    path.reverse()

    print(len(path))
    print(" ".join(map(str, path)))


if __name__ == "__main__":
    n, m = map(int, input().split())
    snakes = []
    for i in range(n):
        start, end = map(int, input().split())
        snakes.append((start, end))

    find_snakeless_path(n, snakes)


# 5 5
# 1 2
# 1 3
# 1 5
# 2 5
# 3 5

Test details

Test 1

Verdict:

input
2 1
1 2

correct output
0

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 2

Verdict:

input
3 1
1 3

correct output
3
1 2 3 

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 3

Verdict:

input
3 1
1 3

correct output
3
1 2 3 

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 4

Verdict:

input
3 1
1 3

correct output
3
1 2 3 

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 5

Verdict:

input
4 1
1 4

correct output
3
1 3 4 

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 6

Verdict: ACCEPTED

input
4 5
1 2
1 3
1 4
2 3
...

correct output
0

user output
0

Test 7

Verdict:

input
4 3
1 2
1 3
1 4

correct output
0

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 8

Verdict:

input
4 2
1 3
2 3

correct output
2
1 4 

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 9

Verdict: ACCEPTED

input
4 4
1 2
1 4
2 3
3 4

correct output
0

user output
0

Test 10

Verdict:

input
5 6
1 2
1 4
1 5
2 5
...

correct output
5
1 3 2 4 5 

user output
3
1 3 5

Test 11

Verdict: ACCEPTED

input
5 5
1 2
1 3
1 5
2 5
...

correct output
3
1 4 5 

user output
3
1 4 5

Test 12

Verdict: ACCEPTED

input
5 5
1 3
1 4
1 5
3 5
...

correct output
3
1 2 5 

user output
3
1 2 5

Test 13

Verdict:

input
5 6
1 3
1 4
1 5
2 4
...

correct output
5
1 2 3 4 5 

user output
4
1 2 3 5

Test 14

Verdict: ACCEPTED

input
5 9
1 2
1 3
1 4
1 5
...

correct output
0

user output
0

Test 15

Verdict: ACCEPTED

input
5 7
1 2
1 3
1 4
1 5
...

correct output
0

user output
0

Test 16

Verdict:

input
5 1
1 5

correct output
3
1 4 5 

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 17

Verdict:

input
5 4
1 2
1 3
1 4
1 5

correct output
0

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 18

Verdict: ACCEPTED

input
5 9
1 2
1 3
1 4
1 5
...

correct output
0

user output
0

Test 19

Verdict:

input
5 4
1 2
1 3
1 4
1 5

correct output
0

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 20

Verdict:

input
10 16
1 2
1 3
1 4
1 5
...

correct output
6
1 6 9 8 7 10 

user output
3
1 6 10

Test 21

Verdict:

input
10 16
1 2
1 3
1 4
1 5
...

correct output
5
1 9 8 7 10 

user output
3
1 9 10

Test 22

Verdict:

input
10 16
1 2
1 4
1 5
1 6
...

correct output
10
1 3 9 8 7 6 5 4 2 10 

user output
4
1 3 4 10

Test 23

Verdict:

input
10 16
1 3
1 4
1 5
1 6
...

correct output
6
1 2 9 8 7 10 

user output
4
1 2 3 10

Test 24

Verdict: ACCEPTED

input
10 39
1 2
1 3
1 4
1 5
...

correct output
0

user output
0

Test 25

Verdict: ACCEPTED

input
10 17
1 2
1 3
1 4
1 5
...

correct output
0

user output
0

Test 26

Verdict:

input
10 1
1 10

correct output
3
1 9 10 

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 27

Verdict:

input
10 9
1 2
1 3
1 4
1 5
...

correct output
0

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 28

Verdict:

input
10 40
1 2
1 3
1 4
1 5
...

correct output
0

user output
3
1 8 10

Test 29

Verdict:

input
10 9
1 2
1 3
1 4
1 5
...

correct output
0

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 30

Verdict:

input
100 196
1 2
1 3
1 4
1 5
...

correct output
31
1 60 99 98 97 96 95 94 93 92 9...

user output
3
1 60 100

Test 31

Verdict:

input
100 196
1 2
1 3
1 4
1 5
...

correct output
30
1 99 98 97 96 95 94 93 92 91 9...

user output
3
1 99 100

Test 32

Verdict:

input
100 196
1 2
1 3
1 4
1 5
...

correct output
98
1 20 99 98 97 96 95 94 93 92 9...

user output
3
1 20 100

Test 33

Verdict:

input
100 196
1 2
1 3
1 4
1 5
...

correct output
32
1 8 99 98 97 96 95 94 93 92 91...

user output
3
1 8 100

Test 34

Verdict:

input
100 4910
1 2
1 3
1 4
1 5
...

correct output
0

user output
3
1 12 100

Test 35

Verdict: ACCEPTED

input
100 197
1 2
1 3
1 4
1 5
...

correct output
0

user output
0

Test 36

Verdict: ACCEPTED

input
100 248
1 8
1 29
1 53
1 61
...

correct output
3
1 99 100 

user output
3
1 2 100

Test 37

Verdict:

input
100 99
1 2
1 3
1 4
1 5
...

correct output
0

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 38

Verdict: ACCEPTED

input
100 4888
1 2
1 3
1 4
1 5
...

correct output
0

user output
0

Test 39

Verdict:

input
100 99
1 2
1 3
1 4
1 5
...

correct output
0

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 40

Verdict:

input
200 396
1 2
1 3
1 4
1 5
...

correct output
60
1 119 199 198 197 196 195 194 ...

user output
3
1 119 200

Test 41

Verdict:

input
200 396
1 2
1 3
1 4
1 5
...

correct output
58
1 199 198 197 196 195 194 193 ...

user output
3
1 199 200

Test 42

Verdict:

input
200 396
1 2
1 3
1 4
1 5
...

correct output
195
1 38 199 198 197 196 195 194 1...

user output
3
1 38 200

Test 43

Verdict:

input
200 396
1 2
1 3
1 4
1 5
...

correct output
61
1 16 199 198 197 196 195 194 1...

user output
3
1 16 200

Test 44

Verdict: ACCEPTED

input
200 19807
1 2
1 3
1 4
1 5
...

correct output
0

user output
0

Test 45

Verdict: ACCEPTED

input
200 397
1 2
1 3
1 4
1 5
...

correct output
0

user output
0

Test 46

Verdict: ACCEPTED

input
200 994
1 8
1 29
1 53
1 61
...

correct output
3
1 199 200 

user output
3
1 2 200

Test 47

Verdict:

input
200 199
1 2
1 3
1 4
1 5
...

correct output
0

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 48

Verdict: ACCEPTED

input
200 19792
1 2
1 3
1 4
1 5
...

correct output
0

user output
0

Test 49

Verdict:

input
200 199
1 2
1 3
1 4
1 5
...

correct output
0

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 50

Verdict:

input
1000 1996
1 2
1 3
1 4
1 5
...

correct output
288
1 593 999 998 997 996 995 994 ...

user output
3
1 593 1000

Test 51

Verdict:

input
1000 1996
1 2
1 3
1 4
1 5
...

correct output
282
1 997 999 998 996 995 994 993 ...

user output
3
1 997 1000

Test 52

Verdict:

input
1000 1996
1 2
1 3
1 4
1 5
...

correct output
975
1 186 999 998 997 996 995 994 ...

user output
3
1 186 1000

Test 53

Verdict:

input
1000 1996
1 2
1 3
1 4
1 5
...

correct output
295
1 72 999 998 997 996 995 994 9...

user output
3
1 72 1000

Test 54

Verdict: ACCEPTED

input
1000 299999
1 2
1 3
1 4
1 5
...

correct output
3
1 691 1000 

user output
3
1 691 1000

Test 55

Verdict: ACCEPTED

input
1000 1997
1 2
1 3
1 4
1 5
...

correct output
0

user output
0

Test 56

Verdict: ACCEPTED

input
1000 25751
1 8
1 29
1 53
1 61
...

correct output
4
1 999 998 1000 

user output
3
1 2 1000

Test 57

Verdict:

input
1000 999
1 2
1 3
1 4
1 5
...

correct output
0

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 58

Verdict:

input
1000 299999
1 2
1 3
1 4
1 5
...

correct output
3
1 832 1000 

user output
3
1 224 1000

Test 59

Verdict:

input
1000 999
1 2
1 3
1 4
1 5
...

correct output
0

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 60

Verdict:

input
1000 299999
1 2
1 3
1 4
1 5
...

correct output
3
1 999 1000 

user output
3
1 9 1000

Test 61

Verdict:

input
1000 999
1 1000
2 1000
3 1000
4 1000
...

correct output
0

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 62

Verdict:

input
1000 999
1 1000
2 1000
3 1000
4 1000
...

correct output
0

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 63

Verdict: ACCEPTED

input
1000 195765
1 2
1 3
1 4
1 5
...

correct output
4
1 999 997 1000 

user output
3
1 6 1000

Test 64

Verdict:

input
1000 1996
1 2
1 3
1 4
1 5
...

correct output
229
1 922 999 998 997 996 995 994 ...

user output
3
1 922 1000

Test 65

Verdict: ACCEPTED

input
1000 92979
1 6
1 8
1 12
1 18
...

correct output
2
1 1000 

user output
2
1 1000

Test 66

Verdict: ACCEPTED

input
1000 1997
1 2
1 3
1 4
1 5
...

correct output
0

user output
0

Test 67

Verdict: ACCEPTED

input
1000 1997
1 2
1 3
1 4
1 5
...

correct output
0

user output
0

Test 68

Verdict: ACCEPTED

input
1000 299999
1 2
1 3
1 4
1 5
...

correct output
3
1 885 1000 

user output
3
1 885 1000

Test 69

Verdict:

input
1000 999
1 2
1 3
1 4
1 5
...

correct output
0

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 70

Verdict:

input
100000 199996
1 2
1 3
1 4
1 5
...

correct output
28483
1 59286 99999 99998 99997 9999...

user output
(empty)

Test 71

Verdict:

input
100000 199996
1 2
1 3
1 4
1 5
...

correct output
27969
1 99719 99999 99998 99997 9999...

user output
(empty)

Test 72

Verdict:

input
100000 199996
1 2
1 3
1 4
1 5
...

correct output
97408
1 18510 99999 99998 99997 9999...

user output
(empty)

Test 73

Verdict:

input
100000 199996
1 2
1 3
1 4
1 5
...

correct output
29187
1 7074 99999 99998 99997 99996...

user output
(empty)

Test 74

Verdict:

input
100000 270197
1 861
1 12080
1 39541
1 39686
...

correct output
3
1 99999 100000 

user output
(empty)

Test 75

Verdict:

input
100000 199997
1 2
1 3
1 4
1 5
...

correct output
0

user output
(empty)

Test 76

Verdict:

input
100000 284253
1 23553
1 48406
1 56616
1 56899
...

correct output
2
1 100000 

user output
(empty)

Test 77

Verdict:

input
100000 99999
1 2
1 3
1 4
1 5
...

correct output
0

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 78

Verdict:

input
100000 3335
1 100000
11 26761
12 80933
41 44903
...

correct output
3
1 99999 100000 

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 79

Verdict:

input
100000 99999
1 2
1 3
1 4
1 5
...

correct output
0

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 80

Verdict:

input
100000 89632
1 76350
1 97733
1 100000
2 16314
...

correct output
3
1 99999 100000 

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 81

Verdict:

input
100000 99999
1 100000
2 100000
3 100000
4 100000
...

correct output
0

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 82

Verdict:

input
100000 99999
1 100000
2 100000
3 100000
4 100000
...

correct output
0

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 83

Verdict:

input
100000 182210
1 17827
1 55463
1 98875
1 100000
...

correct output
3
1 99999 100000 

user output
(empty)

Test 84

Verdict:

input
100000 199996
1 2
1 3
1 4
1 5
...

correct output
22685
1 92190 99999 99998 99997 9999...

user output
(empty)

Test 85

Verdict:

input
100000 244084
1 33037
1 48376
1 94522
1 100000
...

correct output
3
1 99999 100000 

user output
(empty)

Test 86

Verdict:

input
100000 199997
1 2
1 3
1 4
1 5
...

correct output
0

user output
(empty)

Test 87

Verdict:

input
100000 199997
1 2
1 3
1 4
1 5
...

correct output
0

user output
(empty)

Test 88

Verdict:

input
100000 22805
1 100000
2 29973
7 38479
7 77260
...

correct output
3
1 99999 100000 

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError

Test 89

Verdict:

input
100000 99999
1 2
1 3
1 4
1 5
...

correct output
0

user output
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 54, in <module>
    start, end = map(int, input().split())
EOFError