結果

問題 No.2780 The Bottle Imp
ユーザー 寝癖寝癖
提出日時 2024-06-07 22:22:12
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,795 bytes
コンパイル時間 137 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 269,820 KB
最終ジャッジ日時 2024-06-08 10:33:04
合計ジャッジ時間 11,877 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
67,584 KB
testcase_01 AC 65 ms
67,840 KB
testcase_02 AC 70 ms
67,456 KB
testcase_03 AC 63 ms
67,584 KB
testcase_04 AC 65 ms
67,584 KB
testcase_05 AC 62 ms
67,456 KB
testcase_06 AC 66 ms
67,584 KB
testcase_07 AC 354 ms
105,600 KB
testcase_08 AC 387 ms
107,468 KB
testcase_09 AC 484 ms
122,172 KB
testcase_10 AC 381 ms
120,368 KB
testcase_11 AC 437 ms
117,504 KB
testcase_12 AC 363 ms
118,128 KB
testcase_13 AC 449 ms
120,068 KB
testcase_14 AC 200 ms
97,960 KB
testcase_15 AC 207 ms
97,992 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 198 ms
95,920 KB
testcase_19 AC 219 ms
98,616 KB
testcase_20 AC 202 ms
97,352 KB
testcase_21 WA -
testcase_22 AC 277 ms
98,688 KB
testcase_23 AC 215 ms
97,536 KB
testcase_24 AC 397 ms
117,488 KB
testcase_25 AC 442 ms
123,184 KB
testcase_26 AC 297 ms
105,752 KB
testcase_27 AC 173 ms
92,540 KB
testcase_28 AC 172 ms
92,164 KB
testcase_29 AC 292 ms
121,600 KB
testcase_30 AC 173 ms
93,184 KB
testcase_31 AC 253 ms
102,628 KB
testcase_32 AC 114 ms
87,680 KB
testcase_33 AC 555 ms
240,756 KB
testcase_34 AC 609 ms
269,820 KB
testcase_35 AC 123 ms
85,248 KB
testcase_36 AC 67 ms
67,328 KB
testcase_37 AC 63 ms
67,712 KB
testcase_38 AC 121 ms
84,992 KB
testcase_39 AC 319 ms
139,896 KB
testcase_40 AC 322 ms
139,764 KB
testcase_41 AC 325 ms
140,276 KB
testcase_42 WA -
testcase_43 WA -
権限があれば一括ダウンロードができます

ソースコード

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])

cnt = 0
now = 0
while True:
    if len(to[now]) > 1:
        print('No')
        exit()
    if len(to[now]) == 1:
        now = to[now].pop()
        cnt += 1
    else:
        break

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