結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,976 KB
testcase_01 AC 41 ms
52,608 KB
testcase_02 AC 39 ms
52,352 KB
testcase_03 AC 41 ms
53,120 KB
testcase_04 AC 40 ms
52,608 KB
testcase_05 AC 40 ms
52,352 KB
testcase_06 AC 40 ms
52,608 KB
testcase_07 AC 316 ms
98,676 KB
testcase_08 AC 317 ms
97,104 KB
testcase_09 AC 345 ms
97,200 KB
testcase_10 AC 325 ms
99,656 KB
testcase_11 AC 329 ms
98,648 KB
testcase_12 AC 485 ms
137,828 KB
testcase_13 AC 488 ms
138,684 KB
testcase_14 AC 165 ms
85,864 KB
testcase_15 AC 161 ms
86,756 KB
testcase_16 AC 152 ms
86,876 KB
testcase_17 AC 183 ms
86,656 KB
testcase_18 AC 149 ms
86,836 KB
testcase_19 AC 163 ms
87,176 KB
testcase_20 AC 185 ms
86,740 KB
testcase_21 AC 151 ms
86,712 KB
testcase_22 AC 232 ms
82,680 KB
testcase_23 AC 164 ms
83,668 KB
testcase_24 AC 285 ms
93,428 KB
testcase_25 AC 432 ms
114,624 KB
testcase_26 AC 264 ms
89,352 KB
testcase_27 AC 181 ms
96,236 KB
testcase_28 AC 177 ms
96,236 KB
testcase_29 AC 214 ms
96,352 KB
testcase_30 AC 190 ms
104,412 KB
testcase_31 AC 273 ms
109,996 KB
testcase_32 AC 90 ms
84,716 KB
testcase_33 AC 289 ms
126,616 KB
testcase_34 AC 315 ms
133,320 KB
testcase_35 AC 112 ms
78,268 KB
testcase_36 WA -
testcase_37 AC 50 ms
52,608 KB
testcase_38 AC 115 ms
78,704 KB
testcase_39 AC 227 ms
111,244 KB
testcase_40 AC 256 ms
111,248 KB
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 39 ms
52,608 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)
in_deg = [0]*nN
for nv in range(nN):
   for na in nG[nv]:
      in_deg[na] += 1

if in_deg[ns] == 0 and in_deg.count(1) == nN-1:
   ans = True
else:
   ans = False

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