結果

問題 No.2780 The Bottle Imp
ユーザー ねしんねしん
提出日時 2024-06-07 23:50:54
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 4,488 bytes
コンパイル時間 305 ms
コンパイル使用メモリ 82,840 KB
実行使用メモリ 303,360 KB
最終ジャッジ日時 2024-06-08 10:40:06
合計ジャッジ時間 15,913 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
54,528 KB
testcase_01 AC 50 ms
55,168 KB
testcase_02 AC 49 ms
55,168 KB
testcase_03 AC 51 ms
54,912 KB
testcase_04 AC 51 ms
54,784 KB
testcase_05 AC 50 ms
54,784 KB
testcase_06 AC 49 ms
55,168 KB
testcase_07 AC 578 ms
119,680 KB
testcase_08 AC 651 ms
122,496 KB
testcase_09 AC 705 ms
133,248 KB
testcase_10 AC 610 ms
131,712 KB
testcase_11 AC 680 ms
133,376 KB
testcase_12 AC 675 ms
151,168 KB
testcase_13 AC 788 ms
144,000 KB
testcase_14 AC 259 ms
100,096 KB
testcase_15 AC 268 ms
98,944 KB
testcase_16 AC 259 ms
100,608 KB
testcase_17 AC 257 ms
100,480 KB
testcase_18 AC 261 ms
99,968 KB
testcase_19 AC 256 ms
101,120 KB
testcase_20 AC 254 ms
100,352 KB
testcase_21 AC 267 ms
100,352 KB
testcase_22 AC 409 ms
98,816 KB
testcase_23 AC 312 ms
105,216 KB
testcase_24 AC 594 ms
128,128 KB
testcase_25 AC 760 ms
143,360 KB
testcase_26 AC 433 ms
105,984 KB
testcase_27 AC 208 ms
96,768 KB
testcase_28 AC 208 ms
96,256 KB
testcase_29 WA -
testcase_30 AC 208 ms
97,920 KB
testcase_31 AC 324 ms
108,672 KB
testcase_32 AC 124 ms
84,992 KB
testcase_33 AC 652 ms
253,312 KB
testcase_34 AC 754 ms
303,360 KB
testcase_35 AC 134 ms
83,328 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 134 ms
83,452 KB
testcase_39 AC 389 ms
144,768 KB
testcase_40 AC 379 ms
145,024 KB
testcase_41 WA -
testcase_42 AC 51 ms
55,168 KB
testcase_43 AC 49 ms
54,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys


def csr(n, E):
    start = [0] * (n + 1)
    elist = [0] * len(E)
    for e in E:
        start[e[0] + 1] += 1
    for i in range(1, n + 1):
        start[i] += start[i - 1]
    counter = start[:]
    for e0, e1 in E:
        elist[counter[e0]] = e1
        counter[e0] += 1
    return start, elist

class _SCC_graph:
    def __init__(self, n):
        self._n = n
        self.edges = []
        sys.setrecursionlimit(max(2*n, sys.getrecursionlimit()))

    def num_vertices(self):
        return self._n

    def add_edge(self, frm, to):
        self.edges.append([frm, to])

    def scc_ids(self):
        start, elist = csr(self._n, self.edges)
        now_ord, group_num = 0, 0
        visited = []
        low = [0] * self._n
        ord_ = [-1] * self._n
        ids = [0] * self._n
        def dfs(v):
            nonlocal now_ord, group_num, visited, low, ord_, ids
            low[v] = ord_[v] = now_ord
            now_ord += 1
            visited.append(v)
            for i in range(start[v], start[v+1]):
                to = elist[i]
                if ord_[to] == -1:
                    dfs(to)
                    low[v] = min(low[v], low[to])
                else:
                    low[v] = min(low[v], ord_[to])
            if low[v] == ord_[v]:
                while True:
                    u = visited.pop()
                    ord_[u] = self._n
                    ids[u] = group_num
                    if u == v: break
                group_num += 1
        
        for i in range(self._n):
            if ord_[i] == -1: dfs(i)
        for i in range(self._n):
            ids[i] = group_num - 1 - ids[i]
        
        return group_num, ids


    def scc(self):
        group_num, ids = self.scc_ids()
        groups = [[] for _ in range(group_num)]
        for i in range(self._n):
            groups[ids[i]].append(i)
        return groups

class SCC_graph:
    def __init__(self, n):
        self._n = n
        self._scc_graph = _SCC_graph(n)
    
    def add_edge(self, frm, to):
        assert 0 <= frm < self._n
        assert 0 <= to < self._n
        self._scc_graph.add_edge(frm, to)
    
    def scc(self):
        return self._scc_graph.scc()
      
N=int(input())
scc_graph = SCC_graph(N)
from collections import defaultdict

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 = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())
#https://note.nkmk.me/python-union-find/
UF=UnionFind(N)
ANS=[]
for i in range(N):
	A=list(map(int,input().split()))
	ANS.append(A)
	for j in range(1,len(A)):
		scc_graph.add_edge(i,A[j]-1)
		UF.union(i,A[j]-1)
groups = scc_graph.scc()
members=[-1]*(N)
for i in range(len(groups)):
  for j in range(len(groups[i])):
    members[groups[i][j]]=i
path=defaultdict(list)
check=set()
for i in range(N):
	for j in range(1,len(ANS[i])):
		if members[i]==members[ANS[i][j]-1]:
			continue
		if (members[i],members[ANS[i][j]-1]) not in check:
			check.add((members[i],members[ANS[i][j]-1]))
			path[members[i]].append(members[ANS[i][j]-1])

from collections import deque
Q=deque()
Q.append(members[0])
check=set()
check.add(members[0])
while len(Q)>0:
	i=Q.popleft()
	for j in path[i]:
		if j not in check:
			check.add(j)
			Q.append(j)
#print(check)
#print(groups)
#print(path)
#print(members)
if len(check)==len(groups):
	print("Yes")
else:
	print("No")
0