結果

問題 No.1865 Make Cycle
ユーザー tassei903tassei903
提出日時 2022-03-11 12:36:50
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,957 bytes
コンパイル時間 1,164 ms
コンパイル使用メモリ 87,244 KB
実行使用メモリ 205,204 KB
最終ジャッジ日時 2023-10-13 16:43:18
合計ジャッジ時間 38,446 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,004 ms
205,204 KB
testcase_01 AC 1,167 ms
137,884 KB
testcase_02 AC 2,051 ms
173,144 KB
testcase_03 AC 1,092 ms
153,504 KB
testcase_04 AC 1,459 ms
177,288 KB
testcase_05 AC 1,472 ms
179,124 KB
testcase_06 AC 1,572 ms
163,600 KB
testcase_07 AC 1,582 ms
160,440 KB
testcase_08 AC 2,082 ms
184,216 KB
testcase_09 AC 1,381 ms
174,204 KB
testcase_10 AC 1,895 ms
182,384 KB
testcase_11 AC 1,628 ms
171,796 KB
testcase_12 AC 1,548 ms
158,968 KB
testcase_13 RE -
testcase_14 AC 1,386 ms
150,088 KB
testcase_15 AC 1,991 ms
179,128 KB
testcase_16 AC 2,037 ms
184,920 KB
testcase_17 AC 1,435 ms
155,244 KB
testcase_18 AC 1,380 ms
184,400 KB
testcase_19 AC 2,274 ms
205,036 KB
testcase_20 AC 75 ms
71,356 KB
testcase_21 AC 75 ms
71,388 KB
testcase_22 AC 73 ms
71,200 KB
testcase_23 AC 74 ms
71,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda :sys.stdin.readline()[:-1]
ni = lambda :int(input())
na = lambda :list(map(int,input().split()))
yes = lambda :print("yes");Yes = lambda :print("Yes");YES = lambda : print("YES")
no = lambda :print("no");No = lambda :print("No");NO = lambda : print("NO")
#######################################################################
# 強連結成分分解(SCC): グラフGに対するSCCを行う
# 入力: <N>: 頂点サイズ, <G>: 順方向の有向グラフ, <RG>: 逆方向の有向グラフ
# 出力: (<ラベル数>, <各頂点のラベル番号>)
def scc(N, G, RG, y):
    order = []
    used = [0]*N
    group = [None]*N
    def dfs(s):
        used[s] = 1
        for t,x in G[s]:
            if x > y:
                continue
            if not used[t]:
                dfs(t)
        order.append(s)
    def rdfs(s, col):
        group[s] = col
        used[s] = 1
        for t, x in RG[s]:
            if x > y:
                continue
            if not used[t]:
                rdfs(t, col)
    for i in range(N):
        if not used[i]:
            dfs(i)
    used = [0]*N
    label = 0
    for s in reversed(order):
        if not used[s]:
            rdfs(s, label)
            label += 1
    return label, group

# 縮約後のグラフを構築
def construct(N, G, label, group):
    G0 = [set() for i in range(label)]
    GP = [[] for i in range(label)]
    for v in range(N):
        lbs = group[v]
        for w in G[v]:
            lbt = group[w]
            if lbs == lbt:
                continue
            G0[lbs].add(lbt)
        GP[lbs].append(v)
    return G0, GP
n,q = na()
g = [[] for i in range(n)]
rg = [[] for i in range(n)]

for j in range(q):
    a,b = na()
    a-=1
    b-=1
    g[a].append((b,j))
    rg[b].append((a, j))




ok = q
ng = -1
while (ok-ng)>1:
    d = (ok+ng)//2
    if scc(n,g,rg,d)[0]<n:
        ok = d
    else:
        ng = d
if ok == q:
    print(-1)
else:
    print(ok+1)
0