結果

問題 No.1865 Make Cycle
ユーザー tassei903tassei903
提出日時 2022-03-06 16:44:15
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,957 bytes
コンパイル時間 400 ms
コンパイル使用メモリ 87,020 KB
実行使用メモリ 205,188 KB
最終ジャッジ日時 2023-09-28 07:59:27
合計ジャッジ時間 36,520 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,948 ms
205,188 KB
testcase_01 AC 1,091 ms
137,840 KB
testcase_02 AC 1,994 ms
172,656 KB
testcase_03 AC 1,064 ms
153,624 KB
testcase_04 AC 1,428 ms
177,492 KB
testcase_05 AC 1,447 ms
178,872 KB
testcase_06 AC 1,575 ms
163,388 KB
testcase_07 AC 1,547 ms
160,524 KB
testcase_08 AC 2,048 ms
184,356 KB
testcase_09 AC 1,329 ms
174,212 KB
testcase_10 AC 1,783 ms
182,472 KB
testcase_11 AC 1,575 ms
171,788 KB
testcase_12 AC 1,479 ms
159,060 KB
testcase_13 RE -
testcase_14 AC 1,197 ms
150,268 KB
testcase_15 AC 1,926 ms
179,252 KB
testcase_16 AC 1,908 ms
184,720 KB
testcase_17 AC 1,345 ms
155,208 KB
testcase_18 AC 1,329 ms
184,380 KB
testcase_19 AC 2,180 ms
204,940 KB
testcase_20 AC 71 ms
71,156 KB
testcase_21 AC 70 ms
71,640 KB
testcase_22 AC 69 ms
71,428 KB
testcase_23 AC 71 ms
71,456 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