結果

問題 No.2780 The Bottle Imp
ユーザー hato336hato336
提出日時 2024-06-07 21:53:45
言語 PyPy3
(7.3.15)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 3,907 bytes
コンパイル時間 366 ms
コンパイル使用メモリ 82,540 KB
実行使用メモリ 138,048 KB
最終ジャッジ日時 2024-06-08 10:29:36
合計ジャッジ時間 13,336 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 141 ms
88,960 KB
testcase_01 AC 138 ms
89,088 KB
testcase_02 AC 144 ms
89,216 KB
testcase_03 AC 138 ms
89,404 KB
testcase_04 AC 138 ms
89,080 KB
testcase_05 AC 140 ms
89,088 KB
testcase_06 AC 142 ms
89,088 KB
testcase_07 AC 351 ms
103,760 KB
testcase_08 AC 352 ms
103,940 KB
testcase_09 AC 329 ms
105,652 KB
testcase_10 AC 371 ms
107,444 KB
testcase_11 AC 347 ms
103,088 KB
testcase_12 AC 398 ms
121,924 KB
testcase_13 AC 377 ms
112,996 KB
testcase_14 AC 270 ms
104,944 KB
testcase_15 AC 263 ms
104,944 KB
testcase_16 AC 268 ms
104,892 KB
testcase_17 AC 261 ms
104,992 KB
testcase_18 AC 265 ms
105,244 KB
testcase_19 AC 267 ms
105,116 KB
testcase_20 AC 261 ms
104,992 KB
testcase_21 AC 258 ms
105,084 KB
testcase_22 AC 300 ms
96,560 KB
testcase_23 AC 257 ms
101,188 KB
testcase_24 AC 348 ms
104,048 KB
testcase_25 AC 385 ms
108,148 KB
testcase_26 AC 313 ms
102,080 KB
testcase_27 AC 276 ms
109,760 KB
testcase_28 AC 280 ms
109,728 KB
testcase_29 AC 275 ms
103,392 KB
testcase_30 AC 248 ms
99,540 KB
testcase_31 AC 335 ms
111,360 KB
testcase_32 AC 203 ms
101,388 KB
testcase_33 AC 355 ms
138,048 KB
testcase_34 AC 349 ms
136,392 KB
testcase_35 AC 213 ms
96,008 KB
testcase_36 AC 139 ms
88,952 KB
testcase_37 AC 138 ms
89,080 KB
testcase_38 AC 214 ms
95,992 KB
testcase_39 AC 320 ms
123,396 KB
testcase_40 AC 317 ms
123,392 KB
testcase_41 AC 308 ms
123,404 KB
testcase_42 WA -
testcase_43 AC 139 ms
89,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections,sys,math,functools,operator,itertools,bisect,heapq,decimal,string,time,random
#sys.setrecursionlimit(10**9)
#sys.set_int_max_str_digits(0)
#input = sys.stdin.readline

#
#alist = []
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        group_members = collections.defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return ''.join(f'{r}: {m}' for r, m in self.all_group_members().items())
def scc(N,edges):
    M=len(edges)
    start=[0]*(N+1)
    elist=[0]*M
    for e in edges:
        start[e[0]+1]+=1
    for i in range(1,N+1):
        start[i]+=start[i-1]
    counter=start[:]
    for e in edges:
        elist[counter[e[0]]]=e[1]
        counter[e[0]]+=1
    visited=[]
    low=[0]*N
    Ord=[-1]*N
    ids=[0]*N
    NG=[0,0]
    def dfs(v):
        stack=[(v,-1,0),(v,-1,1)]
        while stack:
            v,bef,t=stack.pop()
            if t:
                if bef!=-1 and Ord[v]!=-1:
                    low[bef]=min(low[bef],Ord[v])
                    stack.pop()
                    continue
                low[v]=NG[0]
                Ord[v]=NG[0]
                NG[0]+=1
                visited.append(v)
                for i in range(start[v],start[v+1]):
                    to=elist[i]
                    if Ord[to]==-1:
                        stack.append((to,v,0))
                        stack.append((to,v,1))
                    else:
                        low[v]=min(low[v],Ord[to])
            else:
                if low[v]==Ord[v]:
                    while(True):
                        u=visited.pop()
                        Ord[u]=N
                        ids[u]=NG[1]
                        if u==v:
                            break
                    NG[1]+=1
                low[bef]=min(low[bef],low[v])
    for i in range(N):
        if Ord[i]==-1:
            dfs(i)
    for i in range(N):
        ids[i]=NG[1]-1-ids[i]
    group_num=NG[1]
    counts=[0]*group_num
    for x in ids:
        counts[x]+=1
    groups=[[] for i in range(group_num)]
    for i in range(N):
        groups[ids[i]].append(i)
    return groups
n = int(input())
uf = UnionFind(n)
e = []
edge = [[] for i in range(n)]
for i in range(n):
    a = list(map(int,input().split()))
    for j in a[1:]:
        edge[i].append(j-1)
        e.append((i,j-1))
x = scc(n,e)

ans = set()
for i in x:
    for j in range(len(i)-1):
        uf.union(i[j],i[j+1])
for i in uf.members(0):
    for j in edge[i]:
        ans.add(uf.find(j))
ans.discard(uf.find(0))
if len(ans) >= 2:
    exit(print('No'))


start = 0
d = collections.deque()
dist = [10**18 for i in range(n)]
d.append(start)
dist[start] = 0
while d:
    now = d.popleft()
    for i in edge[now]:
        if dist[i] > dist[now] + 1:
            dist[i] = dist[now] + 1
            d.append(i)
print('Yes' if all(dist[i] < 10**18 for i in range(n)) else 'No')
0