結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
68,728 KB
testcase_01 AC 64 ms
68,308 KB
testcase_02 AC 63 ms
68,764 KB
testcase_03 AC 64 ms
69,208 KB
testcase_04 AC 64 ms
68,104 KB
testcase_05 AC 64 ms
68,976 KB
testcase_06 AC 63 ms
69,312 KB
testcase_07 AC 452 ms
114,784 KB
testcase_08 AC 485 ms
120,528 KB
testcase_09 AC 581 ms
130,436 KB
testcase_10 AC 502 ms
132,692 KB
testcase_11 AC 550 ms
134,664 KB
testcase_12 AC 510 ms
144,376 KB
testcase_13 AC 634 ms
144,376 KB
testcase_14 AC 241 ms
99,788 KB
testcase_15 AC 248 ms
98,380 KB
testcase_16 AC 247 ms
98,520 KB
testcase_17 AC 242 ms
99,120 KB
testcase_18 AC 227 ms
99,724 KB
testcase_19 AC 238 ms
101,328 KB
testcase_20 AC 232 ms
98,384 KB
testcase_21 AC 242 ms
100,224 KB
testcase_22 AC 320 ms
101,708 KB
testcase_23 AC 259 ms
100,464 KB
testcase_24 AC 492 ms
132,644 KB
testcase_25 AC 572 ms
132,664 KB
testcase_26 AC 340 ms
108,288 KB
testcase_27 AC 207 ms
95,580 KB
testcase_28 AC 209 ms
95,708 KB
testcase_29 WA -
testcase_30 AC 200 ms
99,064 KB
testcase_31 AC 326 ms
118,228 KB
testcase_32 AC 115 ms
85,220 KB
testcase_33 AC 624 ms
255,952 KB
testcase_34 AC 753 ms
288,868 KB
testcase_35 AC 145 ms
85,132 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 144 ms
85,268 KB
testcase_39 AC 393 ms
160,400 KB
testcase_40 AC 394 ms
160,568 KB
testcase_41 WA -
testcase_42 AC 65 ms
68,360 KB
testcase_43 AC 64 ms
68,716 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(i, aij)
		
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