結果
問題 | No.2780 The Bottle Imp |
ユーザー |
![]() |
提出日時 | 2024-08-28 06:33:09 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 607 ms / 2,000 ms |
コード長 | 1,625 bytes |
コンパイル時間 | 549 ms |
コンパイル使用メモリ | 82,320 KB |
実行使用メモリ | 284,968 KB |
最終ジャッジ日時 | 2024-08-28 06:33:25 |
合計ジャッジ時間 | 13,216 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 40 |
ソースコード
from collections import defaultdict import sys sys.setrecursionlimit(10 ** 6) # n : 頂点数 # 辺 : {u: [v...]} # return : 強連結成分のリスト (トポロジカルソート順) def scc(n, adj) -> list: def dfs(node, used, acc): used[node] = True for to in adj[node]: if used[to]: continue dfs(to, used, acc) acc.append(node) def r_dfs(node, used, g): used[node] = True g.append(node) for to in radj[node]: if used[to]: continue r_dfs(to, used, g) radj = defaultdict(list) # 逆辺 for k, vs in adj.items(): for v in vs: radj[v].append(k) acc = [] used = [False] * n for i in range(n): if used[i]: continue dfs(i, used, acc) cc = [] used = [False] * n for node in reversed(acc): if used[node]: continue g = [] r_dfs(node, used, g) cc.append(g) return cc N = int(input()) adj = defaultdict(list) for i in range(N): M, *A = list(map(int, input().split())) for a in A: adj[i].append(a-1) ss = scc(N, adj) group_inds = [0] * N for i, cc in enumerate(ss): for x in cc: group_inds[x] = i # SCC の先頭に 0 が含まれない if 0 not in ss[0]: print('No') exit() # SCC の各グループから次の SCC へ遷移できるか for i in range(len(ss)-1): ok = False for x in ss[i]: for y in adj[x]: if group_inds[y] == i+1: ok = True break if not ok: print('No') exit() print('Yes')