結果

問題 No.2780 The Bottle Imp
ユーザー 👑 timitimi
提出日時 2024-06-07 23:04:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 257 ms / 2,000 ms
コード長 2,626 bytes
コンパイル時間 213 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 121,264 KB
最終ジャッジ日時 2024-06-08 10:37:21
合計ジャッジ時間 8,394 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
54,272 KB
testcase_01 AC 54 ms
54,272 KB
testcase_02 AC 47 ms
54,272 KB
testcase_03 AC 47 ms
54,272 KB
testcase_04 AC 47 ms
54,400 KB
testcase_05 AC 47 ms
54,144 KB
testcase_06 AC 47 ms
54,016 KB
testcase_07 AC 212 ms
94,812 KB
testcase_08 AC 218 ms
93,800 KB
testcase_09 AC 197 ms
92,332 KB
testcase_10 AC 197 ms
91,912 KB
testcase_11 AC 196 ms
92,068 KB
testcase_12 AC 257 ms
114,068 KB
testcase_13 AC 257 ms
113,804 KB
testcase_14 AC 144 ms
94,976 KB
testcase_15 AC 148 ms
92,960 KB
testcase_16 AC 150 ms
93,864 KB
testcase_17 AC 148 ms
94,592 KB
testcase_18 AC 147 ms
94,464 KB
testcase_19 AC 148 ms
95,048 KB
testcase_20 AC 146 ms
95,440 KB
testcase_21 AC 151 ms
94,720 KB
testcase_22 AC 170 ms
82,560 KB
testcase_23 AC 146 ms
89,088 KB
testcase_24 AC 187 ms
89,768 KB
testcase_25 AC 244 ms
104,004 KB
testcase_26 AC 189 ms
89,076 KB
testcase_27 AC 145 ms
95,348 KB
testcase_28 AC 145 ms
95,536 KB
testcase_29 AC 153 ms
88,960 KB
testcase_30 AC 133 ms
91,776 KB
testcase_31 AC 169 ms
92,416 KB
testcase_32 AC 117 ms
92,032 KB
testcase_33 AC 177 ms
112,000 KB
testcase_34 AC 208 ms
121,264 KB
testcase_35 AC 115 ms
83,712 KB
testcase_36 AC 48 ms
53,760 KB
testcase_37 AC 47 ms
53,760 KB
testcase_38 AC 113 ms
83,584 KB
testcase_39 AC 173 ms
104,960 KB
testcase_40 AC 177 ms
105,344 KB
testcase_41 AC 173 ms
105,088 KB
testcase_42 AC 48 ms
54,144 KB
testcase_43 AC 48 ms
54,016 KB
権限があれば一括ダウンロードができます

ソースコード

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 
s=0
for i in range(len(ans)):
  B=ans[i]
  for b in B:
    D[b]=i 
    if b==0:
      s=i

DD={}
for x,y in C:
  x,y=D[x],D[y]
  if x==y:
    continue
  if x not in DD:
    DD[x]=[y]

V=[-1]*d 
from collections import deque
d=deque()
d.append(s)
while d:
  now=d.popleft()
  V[now]=1 
  if now in DD:
    nex=DD[now][0]
    if V[nex]==-1:
      d.append(nex)

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