結果
問題 | No.2780 The Bottle Imp |
ユーザー | titia |
提出日時 | 2024-06-07 22:56:31 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 374 ms / 2,000 ms |
コード長 | 2,694 bytes |
コンパイル時間 | 219 ms |
コンパイル使用メモリ | 82,456 KB |
実行使用メモリ | 123,504 KB |
最終ジャッジ日時 | 2024-06-08 10:36:48 |
合計ジャッジ時間 | 9,277 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 38 ms
52,736 KB |
testcase_01 | AC | 39 ms
52,608 KB |
testcase_02 | AC | 37 ms
52,352 KB |
testcase_03 | AC | 38 ms
52,864 KB |
testcase_04 | AC | 38 ms
52,608 KB |
testcase_05 | AC | 37 ms
52,736 KB |
testcase_06 | AC | 38 ms
52,992 KB |
testcase_07 | AC | 281 ms
99,448 KB |
testcase_08 | AC | 262 ms
97,660 KB |
testcase_09 | AC | 280 ms
99,200 KB |
testcase_10 | AC | 271 ms
97,408 KB |
testcase_11 | AC | 283 ms
99,712 KB |
testcase_12 | AC | 327 ms
119,760 KB |
testcase_13 | AC | 330 ms
123,104 KB |
testcase_14 | AC | 141 ms
81,988 KB |
testcase_15 | AC | 141 ms
81,792 KB |
testcase_16 | AC | 147 ms
81,652 KB |
testcase_17 | AC | 135 ms
81,920 KB |
testcase_18 | AC | 136 ms
81,536 KB |
testcase_19 | AC | 140 ms
81,536 KB |
testcase_20 | AC | 139 ms
82,048 KB |
testcase_21 | AC | 137 ms
81,664 KB |
testcase_22 | AC | 177 ms
83,968 KB |
testcase_23 | AC | 147 ms
81,664 KB |
testcase_24 | AC | 242 ms
93,416 KB |
testcase_25 | AC | 374 ms
113,104 KB |
testcase_26 | AC | 254 ms
88,908 KB |
testcase_27 | AC | 168 ms
95,488 KB |
testcase_28 | AC | 167 ms
95,488 KB |
testcase_29 | AC | 178 ms
98,816 KB |
testcase_30 | AC | 132 ms
89,472 KB |
testcase_31 | AC | 251 ms
109,184 KB |
testcase_32 | AC | 98 ms
78,184 KB |
testcase_33 | AC | 233 ms
111,312 KB |
testcase_34 | AC | 245 ms
123,504 KB |
testcase_35 | AC | 108 ms
78,208 KB |
testcase_36 | AC | 38 ms
52,736 KB |
testcase_37 | AC | 37 ms
52,096 KB |
testcase_38 | AC | 109 ms
78,564 KB |
testcase_39 | AC | 197 ms
105,088 KB |
testcase_40 | AC | 200 ms
105,216 KB |
testcase_41 | AC | 200 ms
105,312 KB |
testcase_42 | AC | 38 ms
52,352 KB |
testcase_43 | AC | 36 ms
52,736 KB |
ソースコード
import sys input = sys.stdin.readline N=int(input()) E=[[] for i in range(N)] E_INV=[[] for i in range(N)] for i in range(N): A=list(map(int,input().split())) for a in A[1:]: E[i].append(a-1) E_INV[a-1].append(i) # (一般グラフの)トポロジカルソート、SCC # DFSして帰り際にTOPに点を放り込んでいる。 # NOWで現在地点、USEINDで、どこの辺まで既に見たか、を調べている。 def Top_sort(E): Parent=[-1]*N USEIND=[0]*N TOP=[] for ROOT in range(N): if Parent[ROOT]!=-1: continue Parent[ROOT]=ROOT NOW=ROOT while NOW!=ROOT or USEIND[ROOT]!=len(E[ROOT]): if USEIND[NOW]==len(E[NOW]): TOP.append(NOW) NOW=Parent[NOW] elif E[NOW][USEIND[NOW]]==Parent[NOW]: USEIND[NOW]+=1 else: NEXT=E[NOW][USEIND[NOW]] USEIND[NOW]+=1 if Parent[NEXT]==-1: Parent[NEXT]=NOW NOW=NEXT TOP.append(ROOT) return TOP[::-1] USE=[0]*N SCC=[] # SCCを調べるための逆順DFS。 # やっていることはhttps://manabitimes.jp/math/1250 などと同じ。 def dfs2(x): Q=[x] USE[x]=1 ANS=[] while Q: x=Q.pop() ANS.append(x) for to in E_INV[x]: if USE[to]==0: USE[to]=1 Q.append(to) return ANS TOP_SORT=Top_sort(E) for x in TOP_SORT: if USE[x]==0: SCC.append(dfs2(x)) E2=[set() for i in range(N)] # UnionFind Group = [i for i in range(N+1)] # グループ分け Nodes = [1]*(N+1) # 各グループのノードの数 def find(x): while Group[x] != x: x=Group[x] return x def Union(x,y): if find(x) != find(y): if Nodes[find(x)] < Nodes[find(y)]: Nodes[find(y)] += Nodes[find(x)] Nodes[find(x)] = 0 Group[find(x)] = find(y) else: Nodes[find(x)] += Nodes[find(y)] Nodes[find(y)] = 0 Group[find(y)] = find(x) for i in range(len(SCC)): LIST=SCC[i] for j in range(1,len(LIST)): x=LIST[j] y=LIST[j-1] Union(x,y) for i in range(N): for to in E[i]: E2[find(i)].add(find(to)) flag=1 for i in range(1,len(SCC)): x=SCC[i-1][0] y=SCC[i][0] if find(y) in E2[find(x)]: pass else: flag=0 start=SCC[0][0] if find(start)==find(0): pass else: if find(start) in E2[find(0)]: pass else: flag=0 if flag==1: print("Yes") else: print("No")