結果

問題 No.2780 The Bottle Imp
ユーザー anan
提出日時 2024-06-07 23:38:00
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,859 bytes
コンパイル時間 566 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 241,792 KB
最終ジャッジ日時 2024-06-08 10:39:16
合計ジャッジ時間 12,843 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
54,656 KB
testcase_01 AC 46 ms
54,656 KB
testcase_02 WA -
testcase_03 AC 44 ms
54,656 KB
testcase_04 AC 44 ms
54,272 KB
testcase_05 AC 44 ms
54,272 KB
testcase_06 AC 44 ms
54,656 KB
testcase_07 AC 442 ms
99,968 KB
testcase_08 AC 487 ms
100,352 KB
testcase_09 AC 527 ms
114,688 KB
testcase_10 AC 453 ms
111,104 KB
testcase_11 AC 538 ms
116,352 KB
testcase_12 AC 448 ms
98,432 KB
testcase_13 AC 547 ms
105,472 KB
testcase_14 AC 226 ms
98,688 KB
testcase_15 AC 233 ms
98,176 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 220 ms
98,176 KB
testcase_19 AC 213 ms
98,816 KB
testcase_20 AC 221 ms
98,048 KB
testcase_21 WA -
testcase_22 AC 319 ms
94,720 KB
testcase_23 AC 247 ms
97,280 KB
testcase_24 AC 451 ms
112,384 KB
testcase_25 AC 542 ms
107,008 KB
testcase_26 AC 342 ms
100,224 KB
testcase_27 AC 165 ms
91,264 KB
testcase_28 AC 165 ms
91,648 KB
testcase_29 AC 264 ms
114,432 KB
testcase_30 AC 149 ms
84,864 KB
testcase_31 WA -
testcase_32 AC 104 ms
85,120 KB
testcase_33 AC 552 ms
241,792 KB
testcase_34 WA -
testcase_35 AC 110 ms
82,560 KB
testcase_36 AC 42 ms
54,784 KB
testcase_37 AC 43 ms
54,272 KB
testcase_38 WA -
testcase_39 AC 304 ms
134,528 KB
testcase_40 AC 309 ms
134,656 KB
testcase_41 AC 306 ms
134,272 KB
testcase_42 WA -
testcase_43 AC 44 ms
54,528 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)
for i in range(N):
	A=list(map(int,input().split()))
	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()
if len(groups)==1:
	print("Yes")
elif len(groups)==2 and UF.size(0)==N:
	print("Yes")
else:
	print("No")
0