結果

問題 No.2780 The Bottle Imp
ユーザー 寝癖寝癖
提出日時 2024-06-07 22:31:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 655 ms / 2,000 ms
コード長 3,854 bytes
コンパイル時間 278 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 271,320 KB
最終ジャッジ日時 2024-06-08 10:34:29
合計ジャッジ時間 13,191 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
67,584 KB
testcase_01 AC 68 ms
67,456 KB
testcase_02 AC 64 ms
67,968 KB
testcase_03 AC 63 ms
67,712 KB
testcase_04 AC 65 ms
67,584 KB
testcase_05 AC 64 ms
67,456 KB
testcase_06 AC 63 ms
67,712 KB
testcase_07 AC 388 ms
105,856 KB
testcase_08 AC 442 ms
108,620 KB
testcase_09 AC 532 ms
122,676 KB
testcase_10 AC 447 ms
121,148 KB
testcase_11 AC 481 ms
117,888 KB
testcase_12 AC 373 ms
119,132 KB
testcase_13 AC 498 ms
121,344 KB
testcase_14 AC 210 ms
97,708 KB
testcase_15 AC 220 ms
98,100 KB
testcase_16 AC 215 ms
98,092 KB
testcase_17 AC 211 ms
97,844 KB
testcase_18 AC 205 ms
96,304 KB
testcase_19 AC 212 ms
98,752 KB
testcase_20 AC 221 ms
97,992 KB
testcase_21 AC 221 ms
99,364 KB
testcase_22 AC 319 ms
99,072 KB
testcase_23 AC 237 ms
98,048 KB
testcase_24 AC 460 ms
118,668 KB
testcase_25 AC 501 ms
124,476 KB
testcase_26 AC 324 ms
105,648 KB
testcase_27 AC 187 ms
92,156 KB
testcase_28 AC 184 ms
92,664 KB
testcase_29 AC 326 ms
121,984 KB
testcase_30 AC 191 ms
93,568 KB
testcase_31 AC 283 ms
102,884 KB
testcase_32 AC 121 ms
87,936 KB
testcase_33 AC 594 ms
240,668 KB
testcase_34 AC 655 ms
271,320 KB
testcase_35 AC 125 ms
85,248 KB
testcase_36 AC 62 ms
67,712 KB
testcase_37 AC 66 ms
67,456 KB
testcase_38 AC 125 ms
85,376 KB
testcase_39 AC 344 ms
139,764 KB
testcase_40 AC 351 ms
140,152 KB
testcase_41 AC 343 ms
139,888 KB
testcase_42 AC 64 ms
67,968 KB
testcase_43 AC 64 ms
67,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import typing


class CSR:
    def __init__(
            self, n: int, edges: typing.List[typing.Tuple[int, int]]) -> None:
        self.start = [0] * (n + 1)
        self.elist = [0] * len(edges)

        for e in edges:
            self.start[e[0] + 1] += 1

        for i in range(1, n + 1):
            self.start[i] += self.start[i - 1]

        counter = self.start.copy()
        for e in edges:
            self.elist[counter[e[0]]] = e[1]
            counter[e[0]] += 1


class _SCCGraph:
    '''
    Reference:
    R. Tarjan,
    Depth-First Search and Linear Graph Algorithms
    '''

    def __init__(self, n: int) -> None:
        self._n = n
        self._edges: typing.List[typing.Tuple[int, int]] = []

    def num_vertices(self) -> int:
        return self._n

    def add_edge(self, from_vertex: int, to_vertex: int) -> None:
        self._edges.append((from_vertex, to_vertex))

    def scc_ids(self) -> typing.Tuple[int, typing.List[int]]:
        g = CSR(self._n, self._edges)
        now_ord = 0
        group_num = 0
        visited = []
        low = [0] * self._n
        order = [-1] * self._n
        ids = [0] * self._n

        sys.setrecursionlimit(max(self._n + 1000, sys.getrecursionlimit()))

        def dfs(v: int) -> None:
            nonlocal now_ord
            nonlocal group_num
            nonlocal visited
            nonlocal low
            nonlocal order
            nonlocal ids

            low[v] = now_ord
            order[v] = now_ord
            now_ord += 1
            visited.append(v)
            for i in range(g.start[v], g.start[v + 1]):
                to = g.elist[i]
                if order[to] == -1:
                    dfs(to)
                    low[v] = min(low[v], low[to])
                else:
                    low[v] = min(low[v], order[to])

            if low[v] == order[v]:
                while True:
                    u = visited[-1]
                    visited.pop()
                    order[u] = self._n
                    ids[u] = group_num
                    if u == v:
                        break
                group_num += 1

        for i in range(self._n):
            if order[i] == -1:
                dfs(i)

        for i in range(self._n):
            ids[i] = group_num - 1 - ids[i]

        return group_num, ids

    def scc(self) -> typing.List[typing.List[int]]:
        ids = self.scc_ids()
        group_num = ids[0]
        counts = [0] * group_num
        for x in ids[1]:
            counts[x] += 1
        groups: typing.List[typing.List[int]] = [[] for _ in range(group_num)]
        for i in range(self._n):
            groups[ids[1][i]].append(i)

        return groups


class SCCGraph:
    def __init__(self, n: int = 0) -> None:
        self._internal = _SCCGraph(n)

    def add_edge(self, from_vertex: int, to_vertex: int) -> None:
        n = self._internal.num_vertices()
        assert 0 <= from_vertex < n
        assert 0 <= to_vertex < n
        self._internal.add_edge(from_vertex, to_vertex)

    def scc(self) -> typing.List[typing.List[int]]:
        return self._internal.scc()


N = int(input())
M, A = [], []
scc = SCCGraph(N)
for i in range(N):
    m, *a = map(lambda x: int(x)-1, input().split())
    M.append(m)
    A.append(a)
    for j in a:
        scc.add_edge(i, j)

scc = scc.scc()

label = [-1]*N
for i, l in enumerate(scc):
    for j in l:
        label[j] = i

to = [set() for _ in range(len(scc))]
for i in range(N):
    for j in A[i]:
        if label[i] != label[j]:
            to[label[i]].add(label[j])

from collections import deque
q = deque([label[0]])
dist = [-1] * len(scc)
dist[label[0]] = 0

while q:
    v = q.popleft()
    for u in to[v]:
        if dist[u] < dist[v] + 1:
            dist[u] = dist[v] + 1
            q.append(u)

print('Yes' if max(dist) == len(scc)-1 else 'No')
0