結果

問題 No.1865 Make Cycle
ユーザー lam6er
提出日時 2025-03-31 17:47:22
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 928 bytes
コンパイル時間 203 ms
コンパイル使用メモリ 82,688 KB
実行使用メモリ 275,828 KB
最終ジャッジ日時 2025-03-31 17:48:34
合計ジャッジ時間 9,053 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 4
other TLE * 1 -- * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

def main():
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx]); idx +=1
    Q = int(input[idx]); idx +=1
    
    adj = [[] for _ in range(N+1)]  # 1-based
    
    for step in range(1, Q+1):
        A = int(input[idx]); idx +=1
        B = int(input[idx]); idx +=1
        
        # Check if B can reach A
        visited = [False] * (N +1)
        dq = deque()
        dq.append(B)
        visited[B] = True
        found = False
        while dq:
            u = dq.popleft()
            if u == A:
                found = True
                break
            for v in adj[u]:
                if not visited[v]:
                    visited[v] = True
                    dq.append(v)
        if found:
            print(step)
            return
        
        # Add the edge
        adj[A].append(B)
    
    print(-1)

if __name__ == "__main__":
    main()
0