結果
問題 | No.2780 The Bottle Imp |
ユーザー | hato336 |
提出日時 | 2024-06-07 23:35:47 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,053 bytes |
コンパイル時間 | 442 ms |
コンパイル使用メモリ | 82,448 KB |
実行使用メモリ | 168,940 KB |
最終ジャッジ日時 | 2024-06-08 10:39:03 |
合計ジャッジ時間 | 14,775 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 137 ms
88,704 KB |
testcase_01 | AC | 144 ms
89,088 KB |
testcase_02 | AC | 140 ms
88,704 KB |
testcase_03 | AC | 149 ms
89,216 KB |
testcase_04 | AC | 149 ms
89,008 KB |
testcase_05 | AC | 137 ms
89,088 KB |
testcase_06 | AC | 147 ms
89,088 KB |
testcase_07 | AC | 420 ms
120,440 KB |
testcase_08 | AC | 418 ms
119,848 KB |
testcase_09 | AC | 394 ms
115,960 KB |
testcase_10 | AC | 436 ms
117,340 KB |
testcase_11 | AC | 394 ms
115,312 KB |
testcase_12 | AC | 502 ms
149,492 KB |
testcase_13 | AC | 502 ms
148,432 KB |
testcase_14 | AC | 274 ms
105,052 KB |
testcase_15 | AC | 275 ms
105,344 KB |
testcase_16 | AC | 272 ms
105,376 KB |
testcase_17 | AC | 274 ms
105,628 KB |
testcase_18 | AC | 277 ms
105,364 KB |
testcase_19 | AC | 271 ms
105,224 KB |
testcase_20 | AC | 277 ms
105,224 KB |
testcase_21 | AC | 271 ms
105,192 KB |
testcase_22 | AC | 335 ms
97,868 KB |
testcase_23 | AC | 267 ms
101,564 KB |
testcase_24 | AC | 405 ms
114,048 KB |
testcase_25 | AC | 490 ms
130,684 KB |
testcase_26 | AC | 351 ms
103,772 KB |
testcase_27 | AC | 287 ms
109,952 KB |
testcase_28 | AC | 301 ms
109,952 KB |
testcase_29 | AC | 293 ms
111,080 KB |
testcase_30 | AC | 292 ms
116,864 KB |
testcase_31 | AC | 363 ms
116,480 KB |
testcase_32 | AC | 211 ms
101,948 KB |
testcase_33 | AC | 374 ms
141,020 KB |
testcase_34 | AC | 437 ms
168,940 KB |
testcase_35 | AC | 233 ms
96,080 KB |
testcase_36 | AC | 145 ms
88,832 KB |
testcase_37 | AC | 152 ms
89,088 KB |
testcase_38 | AC | 236 ms
96,040 KB |
testcase_39 | AC | 343 ms
126,000 KB |
testcase_40 | AC | 347 ms
125,624 KB |
testcase_41 | AC | 321 ms
125,748 KB |
testcase_42 | WA | - |
testcase_43 | AC | 139 ms
88,832 KB |
ソースコード
import collections,sys,math,functools,operator,itertools,bisect,heapq,decimal,string,time,random #sys.setrecursionlimit(10**9) #sys.set_int_max_str_digits(0) #input = sys.stdin.readline # #alist = [] 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 = collections.defaultdict(list) for member in range(self.n): group_members[self.find(member)].append(member) return group_members def __str__(self): return ''.join(f'{r}: {m}' for r, m in self.all_group_members().items()) 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 n = int(input()) uf = UnionFind(n) e = [] edge = [[] for i in range(n)] for i in range(n): a = list(map(int,input().split())) for j in a[1:]: edge[i].append(j-1) e.append((i,j-1)) x = scc(n,e) ans = [set() for i in range(n)] for i in x: for j in range(len(i)-1): uf.union(i[j],i[j+1]) t = uf.all_group_members() for k in range(n): if k != uf.find(k): continue for i in t[k]: for j in edge[i]: ans[k].add(uf.find(j)) ans[k].discard(uf.find(k)) if any(len(ans[i]) >= 2 for i in range(n)): exit(print('No')) start = 0 d = collections.deque() dist = [10**18 for i in range(n)] d.append(start) dist[start] = 0 while d: now = d.popleft() for i in edge[now]: if dist[i] > dist[now] + 1: dist[i] = dist[now] + 1 d.append(i) print('Yes' if all(dist[i] < 10**18 for i in range(n)) else 'No')