結果
問題 |
No.1865 Make Cycle
|
ユーザー |
![]() |
提出日時 | 2022-04-07 22:52:03 |
言語 | PyPy3 (7.3.15) |
結果 |
MLE
|
実行時間 | - |
コード長 | 2,685 bytes |
コンパイル時間 | 248 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 564,752 KB |
最終ジャッジ日時 | 2024-11-28 02:15:59 |
合計ジャッジ時間 | 60,245 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 13 TLE * 6 MLE * 1 |
ソースコード
import math from sys import stdin n, q, *indata = map(int, stdin.read().split()) offset = 0 m = int(math.floor(math.sqrt(q))) g = [[] for i in range(n+1)] repeat = (q-1) // m + 1 cyclecheck = -1 for i in range(repeat): g2 = [[] for i in range(n+1)] for j in range(m): ind = i * m + j if ind >= q: break else: s, t = indata[ind*2]-1,indata[ind*2+1]-1 g2[s].append(t) check = [0 for j in range(n)] for j in range(n): if check[j] == 0: que = [(j,0)] while que: now, inout = que.pop() if inout == 0: if check[now] == 0: que.append((now,1)) check[now] = 1 for k in g[now]: if check[k] == 1: cyclecheck = i break elif check[k] == 0: que.append((k,0)) for k in g2[now]: if check[k] == 1: cyclecheck = i break elif check[k] == 0: que.append((k,0)) if cyclecheck != -1: break else: check[now] = 2 if cyclecheck != -1: break if cyclecheck != -1: break else: for j in range(m): ind = i * m + j if ind >= q: break else: s, t = indata[ind*2]-1,indata[ind*2+1]-1 g[s].append(t) if cyclecheck == -1: print("{}".format(-1)) exit() for i in range(m): ind = cyclecheck * m + i if ind >= q: break else: s, t = indata[ind*2]-1,indata[ind*2+1]-1 g[s].append(t) check = [0 for j in range(n)] for j in range(n): if check[j] == 0: que = [(j,0)] while que: now, inout = que.pop() if inout == 0: if check[now] == 0: que.append((now,1)) check[now] = 1 for k in g[now]: if check[k] == 1: ans = cyclecheck * m + i + 1 print("{}".format(ans)) exit() elif check[k] == 0: que.append((k,0)) else: check[now] = 2