結果

問題 No.1865 Make Cycle
ユーザー puznekopuzneko
提出日時 2022-04-07 22:52:03
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,685 bytes
コンパイル時間 329 ms
コンパイル使用メモリ 87,280 KB
実行使用メモリ 271,436 KB
最終ジャッジ日時 2023-08-18 18:57:43
合計ジャッジ時間 10,340 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,874 ms
268,860 KB
testcase_01 AC 2,375 ms
215,700 KB
testcase_02 AC 2,914 ms
270,480 KB
testcase_03 AC 591 ms
122,536 KB
testcase_04 AC 1,911 ms
271,436 KB
testcase_05 TLE -
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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
0