結果

問題 No.1865 Make Cycle
ユーザー first_vilfirst_vil
提出日時 2022-03-04 22:34:05
言語 PyPy3
(7.3.15)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,578 bytes
コンパイル時間 286 ms
コンパイル使用メモリ 86,964 KB
実行使用メモリ 110,220 KB
最終ジャッジ日時 2023-08-16 02:47:20
合計ジャッジ時間 12,399 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 343 ms
100,540 KB
testcase_01 AC 293 ms
97,096 KB
testcase_02 AC 381 ms
104,440 KB
testcase_03 AC 292 ms
102,788 KB
testcase_04 AC 377 ms
108,152 KB
testcase_05 AC 414 ms
108,556 KB
testcase_06 AC 378 ms
101,296 KB
testcase_07 AC 345 ms
101,320 KB
testcase_08 AC 345 ms
105,756 KB
testcase_09 RE -
testcase_10 AC 444 ms
109,504 KB
testcase_11 AC 413 ms
107,692 KB
testcase_12 AC 322 ms
102,144 KB
testcase_13 AC 329 ms
100,748 KB
testcase_14 AC 352 ms
101,320 KB
testcase_15 AC 412 ms
104,260 KB
testcase_16 AC 428 ms
104,108 KB
testcase_17 AC 363 ms
100,596 KB
testcase_18 AC 366 ms
110,220 KB
testcase_19 AC 494 ms
109,368 KB
testcase_20 AC 64 ms
71,372 KB
testcase_21 AC 65 ms
71,180 KB
testcase_22 AC 65 ms
71,128 KB
testcase_23 AC 62 ms
71,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

int1 = lambda x: int(x) - 1

# input = lambda: sys.stdin.buffer.readline()
input = lambda: sys.stdin.readline().rstrip()
ii = lambda: int(input())
i1 = lambda: int1(input())
mi = lambda: map(int, input().split())
mi1 = lambda: map(int1, input().split())
li = lambda: list(mi())
li1 = lambda: list(mi1())
lli = lambda n: [li() for _ in range(n)]

INF = float("inf")
mod = int(1e9 + 7)
# mod = 998244353

n, q = mi()
g = [list() for i in range(n)]
for i in range(q):
    a, b = mi1()
    g[a].append((b, i))

ok = -1
ng = q
while ng - ok > 1:
    mid = ok + ng >> 1
    flag = False

    # check
    f = [-1] * n
    idx = [0] * n
    st = []
    for i in range(n):
        if f[i] != -1:
            continue
        st.append(i)
        while st:
            cur = st.pop()
            if idx[cur] == 0:
                f[cur] = i
            while idx[cur] < len(g[cur]) and g[cur][idx[cur]][1] > mid:
                idx[cur] += 1
            if idx[cur] == len(g[cur]):
                f[cur] = -2
            else:
                to = g[cur][idx[cur]][0]
                if f[to] == i:
                    flag = True
                    st = []
                    break
                elif f[to] == -1:
                    st.append(cur)
                    st.append(to)
                    idx[cur] += 1
                elif f[to] == -2:
                    st.append(cur)
                    idx[cur] += 1
        if flag:
            break
    #

    if flag:
        ng = mid
    else:
        ok = mid
if ng == q:
    print(-1)
else:
    print(ng + 1)
0