結果
| 問題 | No.1865 Make Cycle |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-05-28 00:32:52 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
AC
|
| 実行時間 | 2,125 ms / 3,000 ms |
| コード長 | 960 bytes |
| 記録 | |
| コンパイル時間 | 411 ms |
| コンパイル使用メモリ | 85,056 KB |
| 実行使用メモリ | 318,080 KB |
| 最終ジャッジ日時 | 2026-05-28 00:33:26 |
| 合計ジャッジ時間 | 31,999 ms |
|
ジャッジサーバーID (参考情報) |
judge1_1 / judge3_1 |
| 純コード判定待ち |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 20 |
ソースコード
from collections import deque
def scc(x,y,n):
res=[-1]*n
p=0
a=[0]*n;b=[0]*n
for i in range(n):
if b[i]:
continue
b[i]=1
f=deque([(i,0)])
while f:
q,w=f.pop()
if len(x[q])>w:
f.append((q,w+1))
if b[x[q][w]]:
continue
f.append((x[q][w],0))
b[x[q][w]]=1
else:
a[p]=q;p+=1
for i in reversed(a):
if res[i]!=-1:
continue
f=deque([i]);res[i]=i
while f:
q=f.pop()
for j in y[q]:
if res[j]==-1:
f.append(j);res[j]=i
return res
n,q=map(int,input().split())
v=[tuple(map(lambda x:int(x)-1,input().split())) for i in range(q)]
a,b=0,q
while b-a>1:
m=(a+b)//2
x=[[] for i in range(n)]
y=[[] for i in range(n)]
for i in range(m+1):
x[v[i][0]].append(v[i][1])
y[v[i][1]].append(v[i][0])
c=scc(x,y,n)
for i in range(n):
if i!=c[i]:
b=m;break
else :
a=m
if b==q:
b=-2
print(b+1)