結果

問題 No.1865 Make Cycle
ユーザー titiatitia
提出日時 2022-03-06 02:32:58
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,540 ms / 3,000 ms
コード長 2,120 bytes
コンパイル時間 1,768 ms
コンパイル使用メモリ 86,528 KB
実行使用メモリ 291,988 KB
最終ジャッジ日時 2023-09-16 18:37:18
合計ジャッジ時間 33,237 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,433 ms
277,184 KB
testcase_01 AC 1,031 ms
270,724 KB
testcase_02 AC 1,533 ms
277,300 KB
testcase_03 AC 956 ms
272,576 KB
testcase_04 AC 1,551 ms
284,668 KB
testcase_05 AC 247 ms
109,924 KB
testcase_06 AC 1,611 ms
280,060 KB
testcase_07 AC 1,461 ms
279,576 KB
testcase_08 AC 1,957 ms
291,988 KB
testcase_09 AC 256 ms
109,800 KB
testcase_10 AC 1,822 ms
281,004 KB
testcase_11 AC 1,623 ms
286,688 KB
testcase_12 AC 1,406 ms
269,528 KB
testcase_13 AC 1,431 ms
269,752 KB
testcase_14 AC 1,329 ms
279,376 KB
testcase_15 AC 1,780 ms
282,004 KB
testcase_16 AC 2,005 ms
282,012 KB
testcase_17 AC 223 ms
102,780 KB
testcase_18 AC 264 ms
111,740 KB
testcase_19 AC 2,540 ms
289,616 KB
testcase_20 AC 67 ms
70,948 KB
testcase_21 AC 67 ms
71,068 KB
testcase_22 AC 68 ms
71,172 KB
testcase_23 AC 68 ms
71,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N,Q=map(int,input().split())
EDGE=[list(map(int,input().split())) for i in range(Q)]

for i in range(Q):
    EDGE[i][0]-=1
    EDGE[i][1]-=1


def calc(qind):
    E=[[] for i in range(N)]
    E_INV=[[] for i in range(N)]

    for i in range(qind):
        x,y=EDGE[i]
        E[x].append(y)
        E_INV[y].append(x)

    # DFSして帰り際にTOPに点を放り込んでいる。
    # NOWで現在地点、USEINDで、どこの辺まで既に見たか、を調べている。
    def Top_sort(E):
        Parent=[-1]*N
        USEIND=[0]*N
        TOP=[]

        for ROOT in range(N):
            if Parent[ROOT]!=-1:
                continue
            Parent[ROOT]=ROOT

            NOW=ROOT

            while NOW!=ROOT or USEIND[ROOT]!=len(E[ROOT]):

                if USEIND[NOW]==len(E[NOW]):
                    TOP.append(NOW)
                    NOW=Parent[NOW]
                elif E[NOW][USEIND[NOW]]==Parent[NOW]:
                    USEIND[NOW]+=1
                else:
                    NEXT=E[NOW][USEIND[NOW]]
                    USEIND[NOW]+=1
                    if Parent[NEXT]==-1:
                        Parent[NEXT]=NOW
                        NOW=NEXT
            TOP.append(ROOT)
            
        return TOP[::-1]


    USE=[0]*N
    SCC=[]

    # SCCを調べるための逆順DFS。
    # やっていることはhttps://manabitimes.jp/math/1250 などと同じ。
    def dfs2(x):
        Q=[x]
        USE[x]=1
        ANS=[]

        while Q:
            x=Q.pop()
            ANS.append(x)
            for to in E_INV[x]:
                if USE[to]==0:
                    USE[to]=1
                    Q.append(to)
        return ANS

    TOP_SORT=Top_sort(E)

    for x in TOP_SORT:
        if USE[x]==0:
            SCC.append(dfs2(x))

    for i in range(len(SCC)):
        if len(SCC[i])>1:
            return True

    return False

if calc(Q)==False:
    print(-1)
else:
    OK=Q
    NG=1

    while OK>NG+1:
        mid=(OK+NG)//2

        if calc(mid)==True:
            OK=mid
        else:
            NG=mid

    print(OK)
0