結果
問題 | No.2780 The Bottle Imp |
ユーザー | ねしん |
提出日時 | 2024-06-07 23:39:56 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,470 bytes |
コンパイル時間 | 290 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 303,360 KB |
最終ジャッジ日時 | 2024-06-08 10:39:28 |
合計ジャッジ時間 | 13,216 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 43 ms
54,528 KB |
testcase_01 | AC | 44 ms
54,528 KB |
testcase_02 | AC | 41 ms
55,040 KB |
testcase_03 | AC | 42 ms
54,912 KB |
testcase_04 | AC | 41 ms
55,168 KB |
testcase_05 | AC | 41 ms
55,168 KB |
testcase_06 | AC | 41 ms
54,912 KB |
testcase_07 | AC | 480 ms
119,808 KB |
testcase_08 | AC | 530 ms
122,624 KB |
testcase_09 | AC | 586 ms
133,632 KB |
testcase_10 | AC | 493 ms
131,968 KB |
testcase_11 | AC | 540 ms
133,376 KB |
testcase_12 | AC | 531 ms
151,424 KB |
testcase_13 | AC | 609 ms
144,000 KB |
testcase_14 | AC | 218 ms
100,352 KB |
testcase_15 | AC | 220 ms
99,072 KB |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | AC | 218 ms
100,224 KB |
testcase_19 | AC | 211 ms
100,992 KB |
testcase_20 | AC | 212 ms
100,352 KB |
testcase_21 | WA | - |
testcase_22 | AC | 324 ms
98,816 KB |
testcase_23 | AC | 257 ms
105,088 KB |
testcase_24 | AC | 484 ms
127,744 KB |
testcase_25 | AC | 585 ms
139,520 KB |
testcase_26 | AC | 369 ms
106,496 KB |
testcase_27 | AC | 169 ms
96,512 KB |
testcase_28 | AC | 172 ms
96,512 KB |
testcase_29 | WA | - |
testcase_30 | AC | 175 ms
98,176 KB |
testcase_31 | AC | 257 ms
108,032 KB |
testcase_32 | AC | 104 ms
84,992 KB |
testcase_33 | AC | 551 ms
253,568 KB |
testcase_34 | AC | 641 ms
303,360 KB |
testcase_35 | AC | 109 ms
83,200 KB |
testcase_36 | WA | - |
testcase_37 | WA | - |
testcase_38 | AC | 113 ms
83,200 KB |
testcase_39 | AC | 322 ms
144,768 KB |
testcase_40 | AC | 318 ms
145,024 KB |
testcase_41 | WA | - |
testcase_42 | AC | 45 ms
54,528 KB |
testcase_43 | WA | - |
ソースコード
import sys def csr(n, E): start = [0] * (n + 1) elist = [0] * len(E) for e in E: start[e[0] + 1] += 1 for i in range(1, n + 1): start[i] += start[i - 1] counter = start[:] for e0, e1 in E: elist[counter[e0]] = e1 counter[e0] += 1 return start, elist class _SCC_graph: def __init__(self, n): self._n = n self.edges = [] sys.setrecursionlimit(max(2*n, sys.getrecursionlimit())) def num_vertices(self): return self._n def add_edge(self, frm, to): self.edges.append([frm, to]) def scc_ids(self): start, elist = csr(self._n, self.edges) now_ord, group_num = 0, 0 visited = [] low = [0] * self._n ord_ = [-1] * self._n ids = [0] * self._n def dfs(v): nonlocal now_ord, group_num, visited, low, ord_, ids low[v] = ord_[v] = now_ord now_ord += 1 visited.append(v) for i in range(start[v], start[v+1]): to = elist[i] if ord_[to] == -1: dfs(to) low[v] = min(low[v], low[to]) else: low[v] = min(low[v], ord_[to]) if low[v] == ord_[v]: while True: u = visited.pop() ord_[u] = self._n ids[u] = group_num if u == v: break group_num += 1 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 def scc(self): group_num, ids = self.scc_ids() groups = [[] for _ in range(group_num)] for i in range(self._n): groups[ids[i]].append(i) return groups class SCC_graph: def __init__(self, n): self._n = n self._scc_graph = _SCC_graph(n) def add_edge(self, frm, to): assert 0 <= frm < self._n assert 0 <= to < self._n self._scc_graph.add_edge(frm, to) def scc(self): return self._scc_graph.scc() N=int(input()) scc_graph = SCC_graph(N) from collections import defaultdict class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def size(self, x): return -self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] def group_count(self): return len(self.roots()) def all_group_members(self): group_members = defaultdict(list) for member in range(self.n): group_members[self.find(member)].append(member) return group_members def __str__(self): return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items()) #https://note.nkmk.me/python-union-find/ UF=UnionFind(N) ANS=[] for i in range(N): A=list(map(int,input().split())) ANS.append(A) for j in range(1,len(A)): scc_graph.add_edge(i,A[j]-1) UF.union(i,A[j]-1) groups = scc_graph.scc() members=[-1]*(N) for i in range(len(groups)): for j in range(len(groups[i])): members[groups[i][j]]=i path=defaultdict(list) check=set() for i in range(N): for j in range(1,len(ANS[i])): if members[i]==members[ANS[i][j]-1]: continue if (members[i],members[ANS[i][j]-1]) not in check: check.add((members[i],members[ANS[i][j]-1])) path[members[i]].append(members[ANS[i][j]-1]) from collections import deque Q=deque() Q.append(0) check=set() check.add(0) while len(Q)>0: i=Q.popleft() for j in path[i]: if j not in check: check.add(j) Q.append(j) #print(check) #print(groups) #print(path) #print(members) if len(check)==len(groups): print("Yes") else: print("No")