結果

問題 No.2780 The Bottle Imp
ユーザー ねしんねしん
提出日時 2024-06-07 23:38:59
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 4,470 bytes
コンパイル時間 261 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 303,104 KB
最終ジャッジ日時 2024-06-08 10:39:31
合計ジャッジ時間 13,288 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
55,168 KB
testcase_01 AC 45 ms
55,168 KB
testcase_02 AC 42 ms
55,168 KB
testcase_03 AC 43 ms
54,784 KB
testcase_04 AC 43 ms
54,784 KB
testcase_05 AC 44 ms
54,912 KB
testcase_06 AC 43 ms
55,168 KB
testcase_07 AC 463 ms
119,808 KB
testcase_08 AC 518 ms
122,624 KB
testcase_09 AC 563 ms
133,504 KB
testcase_10 AC 515 ms
131,840 KB
testcase_11 AC 536 ms
133,376 KB
testcase_12 AC 542 ms
151,168 KB
testcase_13 AC 626 ms
144,144 KB
testcase_14 AC 225 ms
100,608 KB
testcase_15 AC 238 ms
99,456 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 220 ms
100,352 KB
testcase_19 AC 212 ms
100,608 KB
testcase_20 AC 221 ms
100,608 KB
testcase_21 WA -
testcase_22 AC 330 ms
98,816 KB
testcase_23 AC 252 ms
105,472 KB
testcase_24 AC 480 ms
128,028 KB
testcase_25 AC 603 ms
139,776 KB
testcase_26 AC 359 ms
106,624 KB
testcase_27 AC 170 ms
96,864 KB
testcase_28 AC 175 ms
96,640 KB
testcase_29 WA -
testcase_30 AC 172 ms
98,176 KB
testcase_31 AC 254 ms
108,288 KB
testcase_32 AC 109 ms
85,248 KB
testcase_33 AC 547 ms
253,696 KB
testcase_34 AC 658 ms
303,104 KB
testcase_35 AC 110 ms
82,944 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 110 ms
82,944 KB
testcase_39 AC 327 ms
145,152 KB
testcase_40 AC 321 ms
145,024 KB
testcase_41 WA -
testcase_42 AC 43 ms
55,168 KB
testcase_43 WA -
権限があれば一括ダウンロードができます

ソースコード

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(0)
check=set()
check.add(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