結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,352 KB
testcase_01 AC 38 ms
52,224 KB
testcase_02 AC 40 ms
52,608 KB
testcase_03 AC 40 ms
52,480 KB
testcase_04 AC 39 ms
52,608 KB
testcase_05 AC 37 ms
52,352 KB
testcase_06 AC 39 ms
52,608 KB
testcase_07 AC 244 ms
89,596 KB
testcase_08 AC 269 ms
89,676 KB
testcase_09 AC 260 ms
89,500 KB
testcase_10 AC 246 ms
89,856 KB
testcase_11 AC 243 ms
89,600 KB
testcase_12 AC 287 ms
99,256 KB
testcase_13 AC 296 ms
99,588 KB
testcase_14 AC 156 ms
90,924 KB
testcase_15 AC 160 ms
90,744 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 154 ms
90,684 KB
testcase_19 AC 159 ms
90,964 KB
testcase_20 AC 160 ms
91,292 KB
testcase_21 WA -
testcase_22 AC 198 ms
83,348 KB
testcase_23 AC 162 ms
87,596 KB
testcase_24 AC 239 ms
87,096 KB
testcase_25 AC 264 ms
94,100 KB
testcase_26 AC 242 ms
89,112 KB
testcase_27 AC 154 ms
94,036 KB
testcase_28 AC 158 ms
94,044 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 212 ms
96,700 KB
testcase_32 AC 98 ms
87,936 KB
testcase_33 AC 216 ms
113,640 KB
testcase_34 AC 216 ms
113,388 KB
testcase_35 AC 105 ms
81,408 KB
testcase_36 AC 39 ms
52,864 KB
testcase_37 WA -
testcase_38 AC 107 ms
81,408 KB
testcase_39 AC 199 ms
102,912 KB
testcase_40 AC 197 ms
102,960 KB
testcase_41 AC 198 ms
102,924 KB
testcase_42 AC 39 ms
52,352 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)
redges = [[] 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)
        redges[a].append(i)

_, ids = G.build()
u = ids.index(max(ids))

used = [False] * n
used[u] = True
st = [u]
while st:
    v = st.pop()
    for nv in redges[v]:
        if not used[nv]:
            used[nv] = True
            st.append(nv)

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