Submission details
Task:Road network
Sender:megachainmail
Submission time:2020-10-03 16:08:22 +0300
Language:Python3 (CPython3)
Status:READY
Result:
Test results
testverdicttime
#10.04 sdetails
#20.04 sdetails
#30.04 sdetails
#40.04 sdetails
#5ACCEPTED0.04 sdetails
#6--details
#7--details
#8--details
#9--details
#10--details
#110.04 sdetails

Code

import queue

#Read input
n, m = [int(i) for i in input().split()]

#Read edge inputs and initialize edges
connections = {i: [] for i in range(1,n+1)}
#print(connections)

for i in range(m):
    a,b = [int(i) for i in input().split()]
    #print(a,b)
    connections[a].append(b)
    #connections[b].append(a)

#print(connections)

#Search with Breadth-first-search
def bfs(start):

    #Initialize a queue for BFS with starting vertex
    left = queue.Queue()
    left.put(start)

    found = [False] * n
    #print(found)
    found[start-1] = True
    num_found = 1

    #Continue until no unvisited neighbors left
    while not left.empty():
        
        #Take first vertex from the "waiting line"
        current = left.get()

        #print("CURRENT:  {}, found: {}".format(current, found))
        
        #Go trough every neighbor
        for neighbor in connections[current]:
            #If was not found
            if not found[neighbor-1]:
                left.put(neighbor) #Add neighbor to visit-list
                found[neighbor-1] = True #Mark as found
                num_found += 1

    #Check if something not found
    for i in range(n):
        if not found[i-1]:
            return "NO\n{} {}".format(start, i)
    return "YES"
    
#Print answer:
b = False
for i in range(n):
    r = bfs(n)
    if r != "YES":
        print(r)
        b = True
        break
if not b:
    print("YES")

Test details

Test 1

Verdict:

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

correct output
NO
7 1

user output
YES

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:

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

correct output
NO
9 1

user output
YES

Test 4

Verdict:

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

correct output
NO
6 1

user output
YES

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:

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

correct output
NO
2 3

user output
YES