結果

問題 No.2780 The Bottle Imp
ユーザー イルカイルカ
提出日時 2024-06-11 10:27:20
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 4,827 bytes
コンパイル時間 333 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 278,844 KB
最終ジャッジ日時 2024-06-11 10:27:33
合計ジャッジ時間 11,533 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
72,960 KB
testcase_01 AC 68 ms
67,840 KB
testcase_02 AC 80 ms
67,584 KB
testcase_03 AC 77 ms
67,584 KB
testcase_04 AC 79 ms
67,840 KB
testcase_05 AC 78 ms
67,968 KB
testcase_06 AC 76 ms
67,456 KB
testcase_07 AC 1,259 ms
261,180 KB
testcase_08 AC 1,260 ms
261,172 KB
testcase_09 AC 1,169 ms
260,608 KB
testcase_10 AC 1,222 ms
261,780 KB
testcase_11 AC 1,220 ms
260,744 KB
testcase_12 TLE -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
権限があれば一括ダウンロードができます

ソースコード

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


          stack = []
          for start in range(self._n):
              if order[start] != -1:
                  continue
              
              stack.append((start, 'visit'))
              parent = [-1] * self._n  # 親を記録するリスト

              while stack:
                  v, action = stack.pop()

                  if action == 'visit':
                      if order[v] == -1:
                          low[v] = now_ord
                          order[v] = now_ord
                          now_ord += 1
                          visited.append(v)
                          stack.append((v, 'process'))

                          for i in range(g.start[v], g.start[v + 1]):
                              to = g.elist[i]
                              if order[to] == -1:
                                  parent[to] = v
                                  stack.append((to, 'visit'))
                              else:
                                  low[v] = min(low[v], order[to])
                  elif action == 'process':
                      if parent[v] != -1:
                          low[parent[v]] = min(low[parent[v]], low[v])

                      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

          return ids

        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) 
groups = sccgraph.scc()
if 0 not in groups[0]:
    print("No")
    exit()
scc_mapping = [-1] * N
for i, group in enumerate(groups):
    for a in group:
        scc_mapping[a] = i
scc_graph = defaultdict(list)
for i, group in enumerate(groups):
    seen = set()
    for a in group:
        for n in graph[a]:
            mapped = scc_mapping[n]
            if mapped != i and mapped not in seen:
                seen.add(mapped)
                scc_graph[i].append(mapped)

def dfs(start):
    todo = [start,]
    seen = [False] * len(groups)

    while todo:
        tgt = todo.pop()
        if seen[tgt]:
            continue
        seen[tgt] = True

        for next in scc_graph[tgt]:
            todo.append(next)
    
    return seen

connected = dfs(0)      

if(all(connected)):
    print("Yes")
else:
    print("No")
  
0