結果

問題 No.2780 The Bottle Imp
ユーザー dp_ijkdp_ijk
提出日時 2024-06-18 00:42:23
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,437 bytes
コンパイル時間 602 ms
コンパイル使用メモリ 82,272 KB
実行使用メモリ 151,264 KB
最終ジャッジ日時 2024-06-18 00:42:35
合計ジャッジ時間 11,505 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,280 KB
testcase_01 AC 42 ms
56,264 KB
testcase_02 AC 43 ms
55,868 KB
testcase_03 AC 45 ms
54,256 KB
testcase_04 AC 43 ms
55,268 KB
testcase_05 AC 43 ms
54,356 KB
testcase_06 AC 44 ms
56,068 KB
testcase_07 AC 319 ms
98,352 KB
testcase_08 AC 370 ms
104,036 KB
testcase_09 AC 343 ms
103,960 KB
testcase_10 AC 322 ms
99,656 KB
testcase_11 AC 307 ms
98,388 KB
testcase_12 AC 496 ms
138,444 KB
testcase_13 AC 506 ms
139,316 KB
testcase_14 AC 168 ms
85,928 KB
testcase_15 AC 161 ms
86,544 KB
testcase_16 AC 161 ms
86,792 KB
testcase_17 AC 160 ms
86,344 KB
testcase_18 AC 154 ms
86,708 KB
testcase_19 AC 166 ms
86,896 KB
testcase_20 AC 160 ms
86,888 KB
testcase_21 AC 151 ms
86,352 KB
testcase_22 AC 208 ms
82,676 KB
testcase_23 AC 175 ms
83,592 KB
testcase_24 AC 291 ms
94,016 KB
testcase_25 AC 435 ms
115,392 KB
testcase_26 AC 233 ms
89,300 KB
testcase_27 AC 186 ms
96,156 KB
testcase_28 AC 183 ms
95,904 KB
testcase_29 WA -
testcase_30 AC 197 ms
104,252 KB
testcase_31 AC 256 ms
110,036 KB
testcase_32 AC 96 ms
84,648 KB
testcase_33 AC 269 ms
126,648 KB
testcase_34 AC 364 ms
151,264 KB
testcase_35 AC 111 ms
78,588 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 116 ms
78,480 KB
testcase_39 AC 237 ms
111,176 KB
testcase_40 AC 238 ms
111,044 KB
testcase_41 WA -
testcase_42 AC 42 ms
54,656 KB
testcase_43 AC 42 ms
55,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class SCCGraph:
   def __init__(self, G):
      self.N = N = len(G)
      self.G = G
      self.H = [[] for _ in range(N)]
      for v in range(N):
         for a in G[v]:
            self.H[a].append(v)

      called = [False]*N
      closed = [False]*N
      preO = []
      postO = []

      def dfs(s):
         Q = [~s, s]
         while Q:
            v = Q.pop()
            if v >= 0:
               if called[v]:
                  continue
               called[v] = True
               preO.append(v)
               for a in reversed(self.G[v]):
                  if called[a]:
                     continue
                  Q.append(~a)
                  Q.append(a)
            else:
               v = ~v
               if closed[v]:
                  continue
               closed[v] = True
               postO.append(v)

      for s in range(N):
         if called[s]:
            continue
         dfs(s)

      found = [False]*N
      self.SCCs = SCCs = []
      self.atlas = atlas = [-1]*N
      cur = 0
      for s in reversed(postO):
         if found[s]:
            continue
         SCC = []
         Q = [s]
         while Q:
            v = Q.pop()
            if found[v]:
               continue
            found[v] = True
            SCC.append(v)
            for a in self.H[v]:
               Q.append(a)
         SCCs.append(SCC)
         for v in SCC:
            atlas[v] = cur
         cur += 1

      SCCs.reverse()
      for v in range(N):
         atlas[v] = cur-1-atlas[v]
      self.DAG: list|None = None

   def contract(self):
      dag = [set() for _ in range(len(self.SCCs))]
      for v in range(self.N):
         for a in self.G[v]:
            if self.atlas[v] == self.atlas[a]:
               continue
            dag[self.atlas[v]].add(self.atlas[a])
      return [list(d) for d in dag]



INF = float("INF")
N = int(input())
G = [[] for _ in range(N)]
for v in range(N):
   M, *A = map(int, input().split())
   G[v] = [a-1 for a in A]

sccG = SCCGraph(G)
nG = sccG.contract()


def bfs(G, s):
   from collections import deque
   N = len(G)
   dist = [INF]*N
   Q = deque([s])
   dist[s] = 0
   while Q:
      v = Q.popleft()
      for a in G[v]:
         if dist[a] != INF:
            continue
         Q.append(a)
         dist[a] = dist[v] + 1
   return dist


dist = bfs(nG, s=sccG.atlas[0])
ans = all(d != INF for d in dist)

if ans is True:
   print("Yes")
else:
   print("No")
0