Submission #44290942


Source Code Expand

from collections import defaultdict
import heapq
import queue


class Node:
    def __init__(self, num):
        self.num = num
        self.weak = []
        self.strong = []

    def __repr__(self):
        return f"Node({self.num}, weak: {self.weak}, strong: {self.strong})"


N, M = list(map(int, input().split(' ')))
strongest = []
A = []
B = []
for i in range(M):
    a, b = list(map(int, input().split(' ')))
    A.append(a)
    B.append(b)

nodes = {}
weaks = set()
for weak, strong in zip(B, A):
    if weak not in nodes:
        nodes[weak] = Node(weak)
    if strong not in nodes:
        nodes[strong] = Node(strong)
    nodes[weak].strong.append(strong)
    weaks.add(weak)

# print(weaks)
# print("nodes", nodes)

strongest = set()
for i in range(1, N + 1):
    if i in weaks:
        continue
    if i not in nodes:
        print(-1)
        exit()
    queue = [nodes[i]]
    while True:
        node = queue.pop(0)
        if not node.strong:
            strongest.add(node.num)
            break
        for strong in node.strong:
            queue.append(nodes[strong])

if len(strongest) == 1:
    print(list(strongest)[0])
else:
    print(-1)

Submission Info

Submission Time
Task B - Who is Saikyo?
User cocodrips
Language PyPy3 (7.3.0)
Score 300
Code Size 1220 Byte
Status AC
Exec Time 145 ms
Memory 77040 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 300 / 300
Status
AC × 3
AC × 26
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 02_min_00.txt, 02_min_01.txt, 02_min_02.txt, 03_corner_00.txt, 03_corner_01.txt, 03_corner_02.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 145 ms 74620 KiB
00_sample_01.txt AC 79 ms 74372 KiB
00_sample_02.txt AC 80 ms 74580 KiB
01_random_00.txt AC 101 ms 75184 KiB
01_random_01.txt AC 79 ms 74708 KiB
01_random_02.txt AC 80 ms 74492 KiB
01_random_03.txt AC 81 ms 74512 KiB
01_random_04.txt AC 116 ms 76824 KiB
01_random_05.txt AC 80 ms 74776 KiB
01_random_06.txt AC 91 ms 75204 KiB
01_random_07.txt AC 86 ms 75208 KiB
01_random_08.txt AC 81 ms 74484 KiB
01_random_09.txt AC 77 ms 74488 KiB
01_random_10.txt AC 80 ms 74756 KiB
01_random_11.txt AC 100 ms 75512 KiB
01_random_12.txt AC 94 ms 74984 KiB
01_random_13.txt AC 86 ms 74544 KiB
01_random_14.txt AC 115 ms 77040 KiB
01_random_15.txt AC 79 ms 74672 KiB
01_random_16.txt AC 82 ms 74824 KiB
02_min_00.txt AC 77 ms 74760 KiB
02_min_01.txt AC 80 ms 74712 KiB
02_min_02.txt AC 82 ms 74424 KiB
03_corner_00.txt AC 79 ms 74672 KiB
03_corner_01.txt AC 80 ms 74712 KiB
03_corner_02.txt AC 79 ms 74760 KiB