結果

問題 No.2780 The Bottle Imp
ユーザー イルカイルカ
提出日時 2024-06-11 11:02:09
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 4,105 bytes
コンパイル時間 389 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 273,076 KB
最終ジャッジ日時 2024-06-11 11:02:32
合計ジャッジ時間 13,145 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
67,584 KB
testcase_01 AC 73 ms
67,456 KB
testcase_02 AC 72 ms
67,712 KB
testcase_03 AC 73 ms
67,456 KB
testcase_04 WA -
testcase_05 AC 73 ms
67,584 KB
testcase_06 AC 73 ms
67,840 KB
testcase_07 AC 180 ms
87,936 KB
testcase_08 AC 458 ms
103,488 KB
testcase_09 AC 508 ms
113,792 KB
testcase_10 AC 443 ms
113,192 KB
testcase_11 AC 215 ms
88,308 KB
testcase_12 AC 228 ms
95,488 KB
testcase_13 AC 494 ms
104,636 KB
testcase_14 AC 237 ms
95,860 KB
testcase_15 AC 267 ms
96,256 KB
testcase_16 AC 241 ms
96,684 KB
testcase_17 AC 232 ms
96,384 KB
testcase_18 AC 226 ms
95,620 KB
testcase_19 AC 227 ms
96,704 KB
testcase_20 AC 256 ms
96,116 KB
testcase_21 AC 225 ms
96,548 KB
testcase_22 AC 316 ms
97,152 KB
testcase_23 AC 252 ms
99,328 KB
testcase_24 AC 475 ms
114,104 KB
testcase_25 AC 499 ms
114,784 KB
testcase_26 AC 185 ms
87,936 KB
testcase_27 AC 215 ms
94,592 KB
testcase_28 AC 208 ms
94,464 KB
testcase_29 AC 322 ms
121,008 KB
testcase_30 AC 192 ms
88,820 KB
testcase_31 AC 338 ms
107,716 KB
testcase_32 AC 148 ms
87,552 KB
testcase_33 AC 657 ms
247,248 KB
testcase_34 AC 735 ms
273,076 KB
testcase_35 AC 137 ms
85,760 KB
testcase_36 AC 72 ms
67,456 KB
testcase_37 AC 72 ms
67,840 KB
testcase_38 AC 143 ms
85,888 KB
testcase_39 AC 387 ms
153,872 KB
testcase_40 AC 428 ms
154,036 KB
testcase_41 AC 396 ms
154,288 KB
testcase_42 AC 76 ms
67,712 KB
testcase_43 AC 73 ms
67,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# SCC→強連結成分をノードとみなしてDFSで訪問可能判定?

from collections import defaultdict
#from atcoder.scc import SCCGraph

###
# https://github.com/not522/ac-library-python
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
###


N = int(input())
sccgraph = SCCGraph(N)
graph = defaultdict(list)
for i in range(1,N+1):
    A = list(map(int, input().split()))[1:]
    for a in A:
        sccgraph.add_edge(i-1, a-1)
        graph[i-1].append(a-1) 
if len(graph[0])==0:
	print("No")
	exit()
groups = sccgraph.scc()
if 0 not in groups[0]:
    print("No")
    exit()
num_groups = len(groups)
node_to_group = [0] * N

# 各ノードがどの強連結成分に属するかを記録する
for group_index, nodes in enumerate(groups):
    for node in nodes:
        node_to_group[node] = group_index

group_connections = [set() for _ in range(num_groups)]

# グラフ内のノード間の接続を強連結成分間の接続に変換する
for node in range(N):
    current_group = node_to_group[node]
    for neighbor in graph[node]:
        neighbor_group = node_to_group[neighbor]
        if neighbor_group == current_group:
            continue
        group_connections[current_group].add(neighbor_group)

# 各強連結成分が次の強連結成分と直接接続されているかを確認する
for group in range(num_groups - 1):
    if group + 1 not in group_connections[group]:
        print("No")
        exit()
print("Yes")

0