結果

問題 No.2780 The Bottle Imp
ユーザー hato336hato336
提出日時 2024-06-07 23:51:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 516 ms / 2,000 ms
コード長 4,962 bytes
コンパイル時間 222 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 179,180 KB
最終ジャッジ日時 2024-06-08 10:39:53
合計ジャッジ時間 13,808 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 133 ms
88,960 KB
testcase_01 AC 135 ms
88,960 KB
testcase_02 AC 139 ms
88,832 KB
testcase_03 AC 137 ms
89,088 KB
testcase_04 AC 135 ms
89,088 KB
testcase_05 AC 129 ms
88,960 KB
testcase_06 AC 131 ms
88,832 KB
testcase_07 AC 407 ms
124,796 KB
testcase_08 AC 409 ms
124,692 KB
testcase_09 AC 388 ms
123,992 KB
testcase_10 AC 440 ms
125,408 KB
testcase_11 AC 401 ms
123,940 KB
testcase_12 AC 516 ms
167,632 KB
testcase_13 AC 499 ms
166,820 KB
testcase_14 AC 239 ms
105,228 KB
testcase_15 AC 235 ms
105,256 KB
testcase_16 AC 232 ms
105,184 KB
testcase_17 AC 247 ms
105,148 KB
testcase_18 AC 238 ms
105,516 KB
testcase_19 AC 250 ms
105,380 KB
testcase_20 AC 235 ms
105,372 KB
testcase_21 AC 236 ms
105,240 KB
testcase_22 AC 324 ms
100,432 KB
testcase_23 AC 242 ms
101,792 KB
testcase_24 AC 382 ms
119,044 KB
testcase_25 AC 491 ms
144,532 KB
testcase_26 AC 338 ms
108,700 KB
testcase_27 AC 259 ms
109,696 KB
testcase_28 AC 251 ms
109,696 KB
testcase_29 AC 289 ms
116,556 KB
testcase_30 AC 269 ms
116,292 KB
testcase_31 AC 329 ms
121,328 KB
testcase_32 AC 181 ms
100,652 KB
testcase_33 AC 339 ms
141,248 KB
testcase_34 AC 405 ms
179,180 KB
testcase_35 AC 197 ms
96,128 KB
testcase_36 AC 131 ms
88,832 KB
testcase_37 AC 131 ms
89,088 KB
testcase_38 AC 205 ms
96,136 KB
testcase_39 AC 313 ms
123,312 KB
testcase_40 AC 299 ms
123,440 KB
testcase_41 AC 301 ms
123,308 KB
testcase_42 AC 130 ms
88,960 KB
testcase_43 AC 132 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)


for i in x:
    for j in range(len(i)-1):
        uf.union(i[j],i[j+1])
t = uf.all_group_members()
z = []
ed = [set() for i in range(n)]
for k in range(n):
    if k != uf.find(k):
        continue
    z.append(k)
    for i in t[k]:
        for j in edge[i]:
            if k==uf.find(j):
                continue
            ed[k].add(uf.find(j))
deg = [0 for i in range(n)]
for i in range(n):
    ed[i] = list(ed[i])
    for j in ed[i]:
        deg[j] += 1

from collections import deque
def topological_sort(node, input_edge):
    """トポロジカルソート

    有向グラフの順序を守るようにソートする
    閉路があるか判定も出来る
    計算量: O(E+V)

    Args:
        node (list): edge[i] = [a, b,...] iからa,b,...に辺が伸びている
        input_edge (list): input_edge[i] = iの入力辺の本数

    Returns:
        list or -1:閉路が存在しないとき
                      ソート済みのリスト
                   閉路が存在する時
                      -1   
    """
    N = len(input_edge)
    ans = [i for i in range(N) if input_edge[i] == 0]
    que = deque(ans)
    while que:
        q = que.popleft()
        for e in node[q]:
            input_edge[e] -= 1
            if input_edge[e] == 0:
                que.append(e)
                ans.append(e)
    if len(ans) == N:
        return ans
    return -1
tp = topological_sort(ed,deg)
dp = [0 for i in range(n)]
dp[uf.find(0)] = 1

for i in tp:
    for j in ed[i]:
        dp[j] = max(dp[j],dp[i]+1)
if len(z) == max(dp):
    print('Yes')
else:
    print('No')
0