結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
67,840 KB
testcase_01 AC 68 ms
67,456 KB
testcase_02 AC 68 ms
67,456 KB
testcase_03 AC 65 ms
67,584 KB
testcase_04 AC 68 ms
67,584 KB
testcase_05 AC 67 ms
67,712 KB
testcase_06 AC 72 ms
67,456 KB
testcase_07 AC 380 ms
104,832 KB
testcase_08 AC 415 ms
107,856 KB
testcase_09 AC 493 ms
122,300 KB
testcase_10 AC 410 ms
120,372 KB
testcase_11 AC 478 ms
126,848 KB
testcase_12 AC 390 ms
133,744 KB
testcase_13 AC 526 ms
134,912 KB
testcase_14 AC 198 ms
97,708 KB
testcase_15 AC 211 ms
98,008 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 190 ms
95,924 KB
testcase_19 AC 195 ms
98,760 KB
testcase_20 AC 210 ms
97,748 KB
testcase_21 WA -
testcase_22 AC 288 ms
98,560 KB
testcase_23 AC 212 ms
97,408 KB
testcase_24 AC 432 ms
117,260 KB
testcase_25 AC 466 ms
123,184 KB
testcase_26 AC 315 ms
105,644 KB
testcase_27 AC 174 ms
92,552 KB
testcase_28 AC 175 ms
92,680 KB
testcase_29 AC 291 ms
121,984 KB
testcase_30 WA -
testcase_31 AC 253 ms
102,356 KB
testcase_32 AC 116 ms
87,808 KB
testcase_33 AC 554 ms
240,752 KB
testcase_34 AC 631 ms
269,916 KB
testcase_35 AC 120 ms
85,376 KB
testcase_36 AC 64 ms
67,456 KB
testcase_37 AC 63 ms
67,584 KB
testcase_38 AC 123 ms
85,376 KB
testcase_39 AC 330 ms
139,900 KB
testcase_40 AC 323 ms
139,896 KB
testcase_41 AC 326 ms
140,032 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_one = sum(len(x) == 1 for x in to)
cnt_zero = sum(len(x) == 0 for x in to)

if cnt_one == len(scc) - 1 and cnt_zero == 1:
    print("Yes")
else:
    print("No")
0