結果

問題 No.2780 The Bottle Imp
ユーザー 👑 timitimi
提出日時 2024-06-07 22:25:28
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,581 bytes
コンパイル時間 303 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 101,740 KB
最終ジャッジ日時 2024-06-08 10:33:43
合計ジャッジ時間 4,524 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
59,264 KB
testcase_01 AC 41 ms
53,888 KB
testcase_02 AC 41 ms
54,144 KB
testcase_03 AC 41 ms
54,528 KB
testcase_04 AC 41 ms
54,528 KB
testcase_05 AC 41 ms
53,760 KB
testcase_06 AC 43 ms
54,016 KB
testcase_07 AC 185 ms
95,840 KB
testcase_08 TLE -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import Counter, defaultdict, deque
import sys
input=sys.stdin.readline
N=int(input())
n=N
#n,m=map(int,input().split())
class SCC:
    def __init__(self,n):
        self.n = n
        self.edges = []
 
    def add_edge(self,a,b):
        self.edges.append((a,b))
 
    def scc(self):
        n = self.n
        counter = [0]*(n+1)
        elist = [0]*len(self.edges)
        for x,y in self.edges:
            counter[x+1] += 1
        for x in range(n):
            counter[x+1] += counter[x]
        start = counter[:]
        for x,y in self.edges:
            elist[counter[x]] = y
            counter[x] += 1
 
        k = 0
        low = [0]*n
        num = [-1]*n
        pre = [None]*n
        visited = []
        q = []
        sccs = []
        for x in range(n):
            if num[x] < 0:
                q.append(x)
                q.append(x)
                while q:
                    x = q.pop()
                    if num[x] < 0:
                        low[x] = num[x] = k
                        k += 1
                        visited.append(x)
                        for i in range(start[x],start[x+1]):
                            y = elist[i]
                            if num[y] < 0:
                                q.append(y)
                                q.append(y)
                                pre[y] = x
                            else:
                                low[x] = min(low[x],num[y])
                    else:
                        if low[x] == num[x]:
                            scc = []
                            while 1:
                                y = visited.pop()
                                num[y] = n
                                scc.append(y)
                                if x == y:
                                    break
                            sccs.append(scc)
                        y = pre[x]
                        if y is not None:
                            low[y] = min(low[y],low[x])
        return sccs[::-1]
scc = SCC(n)
C=[]
for i in range(N):
  A=list(map(int, input().split()))
  B=A[1:]
  for b in B:
    C.append((i,b-1))
    scc.add_edge(i,b-1)
    
ans = scc.scc()

d=len(ans)
D=[-1]*N 
for i in range(len(ans)):
  B=ans[i]
  for b in B:
    D[b]=i 

E=[[] for i in range(d)]
for x,y in C:
  x,y=D[x],D[y]
  if y!=0:
    E[x].append(y)

V=[-1]*d 
from collections import deque
d=deque()
d.append(0)
su=d
while d:
  now=d.popleft()
  V[now]=1 
  if len(E[now])!=0:
    nex=E[now][0]
    d.append(nex)

if len(set(V))==1:
  print('Yes')
else:
  print('No')
0