結果
問題 | No.2780 The Bottle Imp |
ユーザー | detteiuu |
提出日時 | 2024-06-07 22:50:28 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 4,055 bytes |
コンパイル時間 | 202 ms |
コンパイル使用メモリ | 82,456 KB |
実行使用メモリ | 162,832 KB |
最終ジャッジ日時 | 2024-06-08 10:36:10 |
合計ジャッジ時間 | 6,378 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 44 ms
54,912 KB |
testcase_01 | AC | 47 ms
55,296 KB |
testcase_02 | AC | 47 ms
55,296 KB |
testcase_03 | AC | 51 ms
55,168 KB |
testcase_04 | AC | 45 ms
55,296 KB |
testcase_05 | AC | 45 ms
54,912 KB |
testcase_06 | AC | 44 ms
54,912 KB |
testcase_07 | AC | 43 ms
54,912 KB |
testcase_08 | AC | 45 ms
54,912 KB |
testcase_09 | AC | 43 ms
54,912 KB |
testcase_10 | AC | 45 ms
55,040 KB |
testcase_11 | AC | 44 ms
54,656 KB |
testcase_12 | AC | 43 ms
54,784 KB |
testcase_13 | AC | 44 ms
54,912 KB |
testcase_14 | AC | 155 ms
89,268 KB |
testcase_15 | AC | 161 ms
88,676 KB |
testcase_16 | AC | 159 ms
88,772 KB |
testcase_17 | AC | 156 ms
89,284 KB |
testcase_18 | AC | 156 ms
89,276 KB |
testcase_19 | AC | 154 ms
89,536 KB |
testcase_20 | AC | 163 ms
89,152 KB |
testcase_21 | AC | 157 ms
89,280 KB |
testcase_22 | AC | 44 ms
54,912 KB |
testcase_23 | AC | 64 ms
68,608 KB |
testcase_24 | AC | 43 ms
55,180 KB |
testcase_25 | AC | 45 ms
55,040 KB |
testcase_26 | AC | 44 ms
54,656 KB |
testcase_27 | AC | 188 ms
93,468 KB |
testcase_28 | AC | 183 ms
93,480 KB |
testcase_29 | AC | 206 ms
90,716 KB |
testcase_30 | AC | 170 ms
91,480 KB |
testcase_31 | AC | 228 ms
93,864 KB |
testcase_32 | AC | 110 ms
88,660 KB |
testcase_33 | AC | 242 ms
113,796 KB |
testcase_34 | AC | 344 ms
162,832 KB |
testcase_35 | AC | 118 ms
81,476 KB |
testcase_36 | AC | 43 ms
55,296 KB |
testcase_37 | AC | 44 ms
55,296 KB |
testcase_38 | AC | 127 ms
81,280 KB |
testcase_39 | AC | 218 ms
97,536 KB |
testcase_40 | AC | 235 ms
103,600 KB |
testcase_41 | AC | 126 ms
82,772 KB |
testcase_42 | WA | - |
testcase_43 | AC | 44 ms
54,912 KB |
ソースコード
from collections import deque class UnionFind: def __init__(self,n): self.n = n self.parent_size = [-1]*n def leader(self,a): if self.parent_size[a] < 0: return a self.parent_size[a] = self.leader(self.parent_size[a]) return self.parent_size[a] def merge(self,a,b): x, y = self.leader(a), self.leader(b) if x == y: return if abs(self.parent_size[x]) < abs(self.parent_size[y]): x, y = y, x self.parent_size[x] += self.parent_size[y] self.parent_size[y] = x return def same(self,a,b): return self.leader(a) == self.leader(b) def size(self,a): return abs(self.parent_size[self.leader(a)]) def groups(self): result = [[] for _ in range(self.n)] for i in range(self.n): result[self.leader(i)].append(i) return [r for r in result if r != []] def scc(N,edges): M=len(edges) start=[0]*(N+1) elist=[0]*M for e in edges: start[e[0]+1]+=1 for i in range(1,N+1): start[i]+=start[i-1] counter=start[:] for e in edges: elist[counter[e[0]]]=e[1] counter[e[0]]+=1 visited=[] low=[0]*N Ord=[-1]*N ids=[0]*N NG=[0,0] def dfs(v): stack=[(v,-1,0),(v,-1,1)] while stack: v,bef,t=stack.pop() if t: if bef!=-1 and Ord[v]!=-1: low[bef]=min(low[bef],Ord[v]) stack.pop() continue low[v]=NG[0] Ord[v]=NG[0] NG[0]+=1 visited.append(v) for i in range(start[v],start[v+1]): to=elist[i] if Ord[to]==-1: stack.append((to,v,0)) stack.append((to,v,1)) else: low[v]=min(low[v],Ord[to]) else: if low[v]==Ord[v]: while(True): u=visited.pop() Ord[u]=N ids[u]=NG[1] if u==v: break NG[1]+=1 low[bef]=min(low[bef],low[v]) for i in range(N): if Ord[i]==-1: dfs(i) for i in range(N): ids[i]=NG[1]-1-ids[i] group_num=NG[1] counts=[0]*group_num for x in ids: counts[x]+=1 groups=[[] for i in range(group_num)] for i in range(N): groups[ids[i]].append(i) return groups from types import GeneratorType def bootstrap(f, stack=[]): def wrappedfunc(*args, **kwargs): if stack: return f(*args, **kwargs) to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) else: stack.pop() if not stack: break to = stack[-1].send(to) return to return wrappedfunc N = int(input()) edge = [] zero = 0 for i in range(N): MA = list(map(int, input().split())) M = MA[0] if M == 0: zero += 1 if zero >= 2: exit(print("No")) A = MA[1:] for j in range(M): edge.append((i, A[j]-1)) SCC = scc(N, edge) UF = UnionFind(N) for s in SCC: if len(s) >= 2: for i in range(len(s)-1): UF.merge(s[i], s[i+1]) G = [set() for _ in range(N)] for e in edge: u, v = e if not UF.same(u, v): G[UF.leader(u)].add(UF.leader(v)) for i in range(N): G[i] = list(G[i]) @bootstrap def dfs(n): visited[UF.leader(n)] = True for v in G[UF.leader(n)]: if not visited[UF.leader(v)]: yield dfs(UF.leader(v)) else: exit(print("No")) yield visited = [False]*N dfs(0) for i in range(N): if not visited[UF.leader(i)]: print("No") break else: print("Yes")