Submission details
Task:Road network
Sender:PLS2020
Submission time:2020-10-03 14:53:41 +0300
Language:Python3 (CPython3)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.04 sdetails
#20.04 sdetails
#3ACCEPTED0.04 sdetails
#4ACCEPTED0.04 sdetails
#5ACCEPTED0.04 sdetails
#6--details
#7--details
#8--details
#9--details
#10--details
#11ACCEPTED0.04 sdetails

Code

from queue import Queue
from itertools import islice
import sys

n, m = [int(x) for x in input().split()]

routes = []
for i in range(0, n + 1):
    routes.append([])

for i in range(m):
    a, b = [int(x) for x in input().split()]
    routes[a].append(b)

solution = [0] * (n + 1)

# if we've reached this intersection from the currently
# processed node. The node that we care about is written into
# the array - if we havent reached it then it will be out of date -
# we can use this later on when finding an example of something that
# does not work
reached = [0] * (n + 1)

# Dynamic programming + search. Basically we go through
# each node and if we see a node for which we know that
# we can reach every other road
for i in range(1, n + 1):
    q = Queue()

    q.put(i)

    while not q.empty():
        n = q.get()
        reached[n] = i

        # We can reach every other node from
        # this node - mark this one in the solution
        # as well and continue
        # if solution[n] == 1:
        #     solution[i] = 1
        #     break

        # Not sure yet, lets push this node's children
        # on to the queue and continue
        for c in routes[n]:
            if reached[c] != i:
                q.put(c)

    if solution[i] == 1:
        continue

    # Reached the end of the queue. Did we reach
    # every node from this one?
    for r in range(1, n + 1):
        if reached[r] != i:
            print("NO")
            print(i, r)
            sys.exit(0)
    
    solution[i] = 1

print("YES")

Test details

Test 1

Verdict: ACCEPTED

input
10 20
8 1
9 5
6 10
6 1
...

correct output
NO
7 1

user output
NO
7 1

Test 2

Verdict:

input
10 20
2 10
10 6
8 5
3 8
...

correct output
NO
1 2

user output
YES

Test 3

Verdict: ACCEPTED

input
10 20
10 2
1 5
8 3
5 6
...

correct output
NO
9 1

user output
NO
9 1

Test 4

Verdict: ACCEPTED

input
10 20
9 6
10 4
7 2
10 5
...

correct output
NO
6 1

user output
NO
6 1

Test 5

Verdict: ACCEPTED

input
10 20
5 9
10 2
3 5
7 4
...

correct output
YES

user output
YES

Test 6

Verdict:

input
100000 200000
64780 62469
32706 84268
37795 14893
23995 68041
...

correct output
NO
40590 1

user output
(empty)

Test 7

Verdict:

input
100000 200000
74725 92399
25141 53472
70762 85785
47091 71621
...

correct output
NO
96983 1

user output
(empty)

Test 8

Verdict:

input
100000 200000
50342 88741
55031 42206
24989 54546
666 39964
...

correct output
NO
1 68638

user output
(empty)

Test 9

Verdict:

input
100000 200000
51243 54643
90493 3012
62110 9430
5809 45601
...

correct output
NO
48024 1

user output
(empty)

Test 10

Verdict:

input
100000 200000
5524 49109
87052 72192
46434 18442
67624 38661
...

correct output
YES

user output
(empty)

Test 11

Verdict: ACCEPTED

input
7 10
1 2
2 1
1 4
5 4
...

correct output
NO
2 3

user output
NO
1 3