結果
| 問題 |
No.2780 The Bottle Imp
|
| コンテスト | |
| ユーザー |
titia
|
| 提出日時 | 2024-06-07 22:19:49 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 2,576 bytes |
| コンパイル時間 | 700 ms |
| コンパイル使用メモリ | 81,920 KB |
| 実行使用メモリ | 123,284 KB |
| 最終ジャッジ日時 | 2024-12-27 14:00:24 |
| 合計ジャッジ時間 | 10,055 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 30 WA * 1 RE * 9 |
ソースコード
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))
#print(SCC)
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[i]
y=LIST[i-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
if flag==1:
print("Yes")
else:
print("No")
titia