結果
問題 | No.2780 The Bottle Imp |
ユーザー |
![]() |
提出日時 | 2024-06-11 09:51:08 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,903 bytes |
コンパイル時間 | 237 ms |
コンパイル使用メモリ | 13,184 KB |
実行使用メモリ | 46,716 KB |
最終ジャッジ日時 | 2024-06-11 09:51:14 |
合計ジャッジ時間 | 5,542 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 3 TLE * 1 -- * 36 |
ソースコード
# SCC→強連結成分をノードとみなしてDFSで訪問可能判定?from collections import defaultdict#from atcoder.scc import SCCGraph#### https://github.com/not522/ac-library-pythonimport sysimport typingclass CSR:def __init__(self, n: int, edges: typing.List[typing.Tuple[int, int]]) -> None:self.start = [0] * (n + 1)self.elist = [0] * len(edges)for e in edges:self.start[e[0] + 1] += 1for i in range(1, n + 1):self.start[i] += self.start[i - 1]counter = self.start.copy()for e in edges:self.elist[counter[e[0]]] = e[1]counter[e[0]] += 1class SCCGraph:'''Reference:R. Tarjan,Depth-First Search and Linear Graph Algorithms'''def __init__(self, n: int) -> None:self._n = nself._edges: typing.List[typing.Tuple[int, int]] = []def num_vertices(self) -> int:return self._ndef add_edge(self, from_vertex: int, to_vertex: int) -> None:self._edges.append((from_vertex, to_vertex))def scc_ids(self) -> typing.Tuple[int, typing.List[int]]:g = CSR(self._n, self._edges)now_ord = 0group_num = 0visited = []low = [0] * self._norder = [-1] * self._nids = [0] * self._nsys.setrecursionlimit(max(self._n + 1000, sys.getrecursionlimit()))def dfs(v: int) -> None:nonlocal now_ordnonlocal group_numnonlocal visitednonlocal lownonlocal ordernonlocal idslow[v] = now_ordorder[v] = now_ordnow_ord += 1visited.append(v)for i in range(g.start[v], g.start[v + 1]):to = g.elist[i]if order[to] == -1:dfs(to)low[v] = min(low[v], low[to])else:low[v] = min(low[v], order[to])if low[v] == order[v]:while True:u = visited[-1]visited.pop()order[u] = self._nids[u] = group_numif u == v:breakgroup_num += 1for i in range(self._n):if order[i] == -1:dfs(i)for i in range(self._n):ids[i] = group_num - 1 - ids[i]return group_num, idsdef scc(self) -> typing.List[typing.List[int]]:ids = self.scc_ids()group_num = ids[0]counts = [0] * group_numfor x in ids[1]:counts[x] += 1groups: typing.List[typing.List[int]] = [[] for _ in range(group_num)]for i in range(self._n):groups[ids[1][i]].append(i)return groups###N = int(input())sccgraph = SCCGraph(N)graph = defaultdict(list)for i in range(1,N+1):A = list(map(int, input().split()))[1:]for a in A:sccgraph.add_edge(i-1, a-1)graph[i-1].append(a-1)# 強連結成分を取得groups = sccgraph.scc()# 強連結成分を1つのノードとみなして新しいグラフを構築scc_mapping = [-1] * Nfor i, group in enumerate(groups):for a in group:scc_mapping[a] = iscc_graph = defaultdict(list)for i, group in enumerate(groups):for a in group:a_next = [scc_mapping[n] for n in graph[a]]scc_graph[i] = list(set(scc_graph[i]+a_next))def dfs(start):todo = [start,]seen = [False] * len(groups)while todo:tgt = todo.pop()if seen[tgt]:continueseen[tgt] = Truefor next in scc_graph[tgt]:todo.append(next)return seenconnected = dfs(0)if(all(connected)):print("Yes")else:print("No")