結果

問題 No.2780 The Bottle Imp
ユーザー dp_ijkdp_ijk
提出日時 2024-06-18 00:53:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 524 ms / 2,000 ms
コード長 2,355 bytes
コンパイル時間 347 ms
コンパイル使用メモリ 81,904 KB
実行使用メモリ 150,016 KB
最終ジャッジ日時 2024-06-18 00:53:15
合計ジャッジ時間 10,855 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
52,392 KB
testcase_01 AC 40 ms
53,768 KB
testcase_02 AC 40 ms
52,828 KB
testcase_03 AC 41 ms
54,500 KB
testcase_04 AC 44 ms
53,168 KB
testcase_05 AC 44 ms
53,180 KB
testcase_06 AC 40 ms
52,948 KB
testcase_07 AC 316 ms
98,280 KB
testcase_08 AC 323 ms
96,796 KB
testcase_09 AC 324 ms
97,204 KB
testcase_10 AC 309 ms
99,284 KB
testcase_11 AC 322 ms
98,648 KB
testcase_12 AC 524 ms
137,192 KB
testcase_13 AC 460 ms
133,196 KB
testcase_14 AC 172 ms
85,620 KB
testcase_15 AC 163 ms
86,876 KB
testcase_16 AC 155 ms
86,616 KB
testcase_17 AC 154 ms
86,644 KB
testcase_18 AC 152 ms
86,832 KB
testcase_19 AC 163 ms
87,040 KB
testcase_20 AC 169 ms
86,816 KB
testcase_21 AC 153 ms
86,644 KB
testcase_22 AC 203 ms
82,668 KB
testcase_23 AC 166 ms
83,524 KB
testcase_24 AC 295 ms
93,472 KB
testcase_25 AC 411 ms
115,332 KB
testcase_26 AC 238 ms
89,296 KB
testcase_27 AC 181 ms
96,012 KB
testcase_28 AC 188 ms
96,268 KB
testcase_29 AC 216 ms
96,884 KB
testcase_30 AC 196 ms
104,088 KB
testcase_31 AC 261 ms
110,020 KB
testcase_32 AC 93 ms
84,844 KB
testcase_33 AC 270 ms
126,156 KB
testcase_34 AC 361 ms
150,016 KB
testcase_35 AC 114 ms
78,272 KB
testcase_36 AC 40 ms
52,948 KB
testcase_37 AC 39 ms
53,376 KB
testcase_38 AC 124 ms
78,696 KB
testcase_39 AC 237 ms
111,636 KB
testcase_40 AC 235 ms
111,112 KB
testcase_41 AC 235 ms
111,636 KB
testcase_42 AC 40 ms
53,856 KB
testcase_43 AC 41 ms
54,184 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()
ns = sccG.atlas[0]
nN = len(nG)
found = [False]*nN
Q = [ns]
found[ns] = True
while True:
   v = Q.pop()
   for a in nG[v]:
      if found[a]:
         continue
      else:
         Q.append(a)
         found[a] = True
         break
   else:
      break

ans = all(found)

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