結果

問題 No.2780 The Bottle Imp
ユーザー 👑 rin204rin204
提出日時 2024-06-07 21:35:59
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,832 bytes
コンパイル時間 358 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 113,260 KB
最終ジャッジ日時 2024-06-08 10:27:44
合計ジャッジ時間 7,685 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
52,224 KB
testcase_01 AC 37 ms
52,352 KB
testcase_02 AC 40 ms
52,480 KB
testcase_03 AC 40 ms
52,352 KB
testcase_04 AC 41 ms
52,224 KB
testcase_05 AC 37 ms
52,864 KB
testcase_06 AC 36 ms
52,352 KB
testcase_07 AC 209 ms
88,808 KB
testcase_08 AC 224 ms
89,644 KB
testcase_09 AC 214 ms
89,288 KB
testcase_10 AC 208 ms
89,452 KB
testcase_11 AC 212 ms
89,684 KB
testcase_12 AC 257 ms
99,276 KB
testcase_13 AC 244 ms
99,112 KB
testcase_14 AC 131 ms
89,464 KB
testcase_15 AC 134 ms
89,236 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 135 ms
89,544 KB
testcase_19 AC 130 ms
89,664 KB
testcase_20 AC 132 ms
89,108 KB
testcase_21 WA -
testcase_22 AC 179 ms
82,580 KB
testcase_23 AC 136 ms
85,528 KB
testcase_24 AC 202 ms
87,552 KB
testcase_25 AC 221 ms
93,708 KB
testcase_26 AC 196 ms
88,124 KB
testcase_27 AC 148 ms
92,672 KB
testcase_28 AC 148 ms
92,672 KB
testcase_29 AC 156 ms
87,296 KB
testcase_30 AC 142 ms
86,656 KB
testcase_31 AC 183 ms
96,088 KB
testcase_32 AC 99 ms
87,936 KB
testcase_33 AC 198 ms
112,324 KB
testcase_34 AC 200 ms
113,260 KB
testcase_35 AC 97 ms
81,280 KB
testcase_36 AC 37 ms
52,096 KB
testcase_37 AC 35 ms
52,224 KB
testcase_38 AC 97 ms
81,152 KB
testcase_39 AC 192 ms
103,300 KB
testcase_40 AC 189 ms
102,916 KB
testcase_41 AC 190 ms
103,084 KB
testcase_42 AC 38 ms
52,480 KB
testcase_43 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

class SCC:
    def __init__(self, n, edges=None):
        self.n = n
        if edges is None:
            self.edges = []
        else:
            self.edges = edges

    def add_edge(self, u, v):
        self.edges.append((u, v))

    def read_edges(self, m, indexed=1):
        for _ in range(m):
            u, v = map(int, input().split())
            u -= indexed
            v -= indexed
            self.add_edge(u, v)

    def build(self):
        start = [0] * (self.n + 1)
        elist = [0] * len(self.edges)
        for u, _ in self.edges:
            start[u + 1] += 1
        for i in range(1, self.n + 1):
            start[i] += start[i - 1]

        counter = start[:]
        for u, v in self.edges:
            elist[counter[u]] = v
            counter[u] += 1

        now_ord = 0
        group_num = 0
        visited = []
        low = [0] * self.n
        ord = [-1] * self.n
        ids = [0] * self.n
        bpos = [-1] * self.n

        def dfs(v):
            nonlocal now_ord, group_num

            visited.append(v)
            stack = [~v, v]
            while stack:
                pos = stack.pop()
                if pos >= 0:
                    if ord[pos] == -1:
                        low[pos] = ord[pos] = now_ord
                        now_ord += 1
                        visited.append(pos)
                        for i in range(start[pos], start[pos + 1]):
                            to = elist[i]
                            if ord[to] == -1:
                                stack.append(~to)
                                stack.append(to)
                                bpos[to] = pos
                            else:
                                low[pos] = min(low[pos], ord[to])
                else:
                    pos = ~pos
                    if low[pos] == ord[pos]:
                        while 1:
                            u = visited.pop()
                            ord[u] = self.n
                            ids[u] = group_num
                            if u == pos:
                                break
                        group_num += 1
                    if bpos[pos] != -1:
                        low[bpos[pos]] = min(low[bpos[pos]], low[pos])

        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

n = int(input())
G = SCC(n)
edges = [[] for _ in range(n)]
for i in range(n):
    m, *A = map(int, input().split())
    for a in A:
        a -= 1
        G.add_edge(i, a)
        edges[i].append(a)

l, ids = G.build()
ok = [False] * (l - 1)
for i in range(n):
    for j in edges[i]:
        if ids[i] + 1 == ids[j]:
            ok[ids[i]] = True

if all(ok):
    print("Yes")
else:
    print("No")
0