結果

問題 No.2780 The Bottle Imp
ユーザー rlangevinrlangevin
提出日時 2024-06-07 23:37:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 461 ms / 2,000 ms
コード長 2,646 bytes
コンパイル時間 443 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 140,632 KB
最終ジャッジ日時 2024-12-27 14:39:35
合計ジャッジ時間 13,029 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
52,096 KB
testcase_01 AC 56 ms
51,840 KB
testcase_02 AC 53 ms
52,096 KB
testcase_03 AC 53 ms
52,480 KB
testcase_04 AC 51 ms
52,096 KB
testcase_05 AC 50 ms
52,224 KB
testcase_06 AC 55 ms
52,224 KB
testcase_07 AC 307 ms
94,356 KB
testcase_08 AC 339 ms
95,088 KB
testcase_09 AC 309 ms
95,168 KB
testcase_10 AC 358 ms
94,788 KB
testcase_11 AC 290 ms
94,236 KB
testcase_12 AC 391 ms
105,668 KB
testcase_13 AC 403 ms
106,148 KB
testcase_14 AC 293 ms
97,196 KB
testcase_15 AC 285 ms
97,244 KB
testcase_16 AC 254 ms
94,368 KB
testcase_17 AC 253 ms
93,292 KB
testcase_18 AC 274 ms
97,252 KB
testcase_19 AC 293 ms
96,996 KB
testcase_20 AC 268 ms
96,864 KB
testcase_21 AC 241 ms
93,284 KB
testcase_22 AC 224 ms
83,316 KB
testcase_23 AC 236 ms
90,596 KB
testcase_24 AC 296 ms
92,272 KB
testcase_25 AC 371 ms
99,444 KB
testcase_26 AC 252 ms
90,540 KB
testcase_27 AC 259 ms
105,540 KB
testcase_28 AC 248 ms
105,532 KB
testcase_29 AC 292 ms
95,560 KB
testcase_30 AC 189 ms
87,936 KB
testcase_31 AC 377 ms
111,700 KB
testcase_32 AC 335 ms
92,192 KB
testcase_33 AC 384 ms
128,740 KB
testcase_34 AC 461 ms
140,632 KB
testcase_35 AC 180 ms
86,784 KB
testcase_36 AC 52 ms
51,840 KB
testcase_37 AC 52 ms
52,096 KB
testcase_38 AC 180 ms
86,784 KB
testcase_39 AC 342 ms
114,576 KB
testcase_40 AC 352 ms
114,720 KB
testcase_41 AC 355 ms
114,328 KB
testcase_42 AC 52 ms
52,096 KB
testcase_43 AC 54 ms
52,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class DirectedGraph():
    def __init__(self, N):
        self.N = N
        self.G = [[] for i in range(N)]
        self.rG = [[] for i in range(N)]
        self.order = []
        self.used1 = [0] * N
        self.used2 = [0] * N
        self.group = [-1] * N
        self.label = 0
        self.seen = [0] * N
        self.Edge = set()

    def add_edge(self, u, v):
        #多重辺は排除する
        if (u, v) not in self.Edge:
            self.G[u].append(v)
            self.rG[v].append(u)
            self.Edge.add((u, v))

    def dfs(self, s):
        stack = [~s, s]
        while stack:
            u = stack.pop()
            if u >= 0:
                if self.used1[u]:
                    continue
                self.used1[u] = 1
                for v in self.G[u]:
                    if self.used1[v]:
                        continue
                    stack.append(~v)
                    stack.append(v)
            else:
                u = ~u
                if self.seen[u]:
                    continue
                self.seen[u]= 1
                self.order.append(u)

    def rdfs(self, s, num):
        stack = [s]
        while stack:
            u = stack.pop()
            if u >= 0:
                self.used2[u] = 1
                self.group[u] = num
                for v in self.rG[u]:
                    if self.used2[v]:
                        continue
                    stack.append(v)

    def scc(self):
        for i in range(self.N):
            if self.used1[i]:
                continue
            if i != 0:
                print("No")
                exit()
            self.dfs(i)
        for s in reversed(self.order):
            if self.used2[s]:
                continue
            self.rdfs(s, self.label)
            self.label += 1
        return self.label, self.group

    def construct(self):
        nG = [set() for _ in range(self.label)]
        mem = [[] for i in range(self.label)]
        for s in range(self.N):
            now = self.group[s]
            for u in self.G[s]:
                if now == self.group[u]:
                    continue
                nG[now].add(self.group[u])
            mem[now].append(s)
        return nG, mem

N = int(input())
G = DirectedGraph(N)
for i in range(N):
    A = list(map(int, input().split()))
    M = A.pop(0)
    for j in range(M):
        G.add_edge(i, A[j]-1)
        
G.scc()
nG, mem = G.construct()
if 0 not in mem[0]:
    print("No")
    exit()
N = len(nG)
dp = [-100] * N
dp[0] = 0
for i in range(N):
    for v in nG[i]:
        dp[v] = max(dp[v], dp[i] + 1)
print("Yes") if max(dp) == N-1 else print("No")
0