結果

問題 No.2780 The Bottle Imp
ユーザー 👑 KA37RIKA37RI
提出日時 2024-06-07 23:18:45
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,402 bytes
コンパイル時間 491 ms
コンパイル使用メモリ 82,276 KB
実行使用メモリ 255,624 KB
最終ジャッジ日時 2024-06-08 10:38:19
合計ジャッジ時間 14,035 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
67,456 KB
testcase_01 AC 68 ms
67,456 KB
testcase_02 AC 69 ms
67,840 KB
testcase_03 AC 68 ms
68,096 KB
testcase_04 AC 69 ms
67,712 KB
testcase_05 AC 71 ms
67,712 KB
testcase_06 AC 70 ms
67,584 KB
testcase_07 AC 482 ms
116,264 KB
testcase_08 AC 459 ms
115,520 KB
testcase_09 AC 442 ms
117,448 KB
testcase_10 AC 540 ms
126,316 KB
testcase_11 AC 431 ms
116,408 KB
testcase_12 AC 603 ms
136,776 KB
testcase_13 AC 577 ms
135,404 KB
testcase_14 AC 237 ms
97,792 KB
testcase_15 AC 215 ms
98,452 KB
testcase_16 AC 231 ms
98,528 KB
testcase_17 AC 241 ms
98,104 KB
testcase_18 AC 232 ms
98,432 KB
testcase_19 AC 232 ms
98,236 KB
testcase_20 AC 214 ms
98,988 KB
testcase_21 AC 223 ms
98,924 KB
testcase_22 AC 288 ms
95,616 KB
testcase_23 AC 247 ms
98,156 KB
testcase_24 AC 495 ms
119,540 KB
testcase_25 AC 587 ms
138,916 KB
testcase_26 AC 420 ms
121,448 KB
testcase_27 AC 203 ms
95,552 KB
testcase_28 AC 206 ms
95,332 KB
testcase_29 WA -
testcase_30 AC 202 ms
100,736 KB
testcase_31 AC 303 ms
112,860 KB
testcase_32 AC 121 ms
85,104 KB
testcase_33 AC 628 ms
255,624 KB
testcase_34 AC 327 ms
142,000 KB
testcase_35 AC 139 ms
85,068 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 151 ms
85,024 KB
testcase_39 AC 427 ms
169,924 KB
testcase_40 AC 432 ms
169,412 KB
testcase_41 WA -
testcase_42 AC 69 ms
67,712 KB
testcase_43 AC 67 ms
67,456 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())
a = [[int(x) - 1 for x in input().split()] for _ in range(n)]

graph = SCCGraph(n)

for i in range(n):
	for aij in a[i][1:]:
		graph.add_edge(aij, i)
		
scc = graph.scc()

dag = [set() for _ in range(len(scc))]
parent = {}

for i in range(len(scc)):
	for scc_j in scc[i]:
		parent[scc_j] = i
		
print(scc, file=sys.stderr)
print(parent, file=sys.stderr)

for i in range(n):
	for aij in a[i][1:]:
		u = parent[i]
		v = parent[aij]
		dag[u].add(v)
		
start = parent[0]
visited = [False] * len(scc)
visited[start] = True
stack = [start]

while stack:
	cur = stack.pop()
	for nxt in dag[cur]:
		if not visited[nxt]:
			visited[nxt] = True
			stack.append(nxt)
			
print("Yes" if all(visited) else "No")
0