結果

問題 No.2914 正閉路検出
ユーザー tassei903tassei903
提出日時 2024-10-04 21:54:18
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 3,307 bytes
コンパイル時間 128 ms
コンパイル使用メモリ 82,240 KB
実行使用メモリ 849,464 KB
最終ジャッジ日時 2024-10-04 21:54:30
合計ジャッジ時間 6,620 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
53,624 KB
testcase_01 AC 36 ms
53,960 KB
testcase_02 AC 32 ms
53,384 KB
testcase_03 AC 34 ms
53,684 KB
testcase_04 AC 47 ms
53,220 KB
testcase_05 AC 32 ms
53,236 KB
testcase_06 AC 33 ms
53,020 KB
testcase_07 AC 32 ms
53,240 KB
testcase_08 AC 33 ms
53,664 KB
testcase_09 AC 34 ms
53,116 KB
testcase_10 AC 36 ms
54,440 KB
testcase_11 AC 35 ms
53,416 KB
testcase_12 AC 37 ms
53,056 KB
testcase_13 AC 34 ms
54,740 KB
testcase_14 AC 34 ms
54,780 KB
testcase_15 AC 34 ms
54,364 KB
testcase_16 AC 34 ms
53,568 KB
testcase_17 AC 34 ms
54,440 KB
testcase_18 AC 34 ms
54,780 KB
testcase_19 MLE -
testcase_20 MLE -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
権限があれば一括ダウンロードができます

ソースコード

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")
#######################################################################
def find_cycle(N, M, G):
    vis = [0] * N
    used = [0] * M
    par_v = [None] * N
    par_e = [None] * N

    for s in range(N):
        if vis[s]: continue
        stack = [(s, -1, -1)]
        while stack:
            v, p, e = stack.pop()
            if e != -1 and used[e]: continue
            if vis[v]:
                par_v[v] = p
                if e != -1:
                    par_e[v] = e
                return v, p, e, par_v, par_e
            vis[v] = 1
            if e != -1:
                par_e[v] = e
                used[e] = 1
            par_v[v] = p
            for a, e in G[v]:
                if used[e]: continue
                stack.append((a, v, e))

    return -1, -1, -1, par_v, par_e

def cycle_detection(N, M, G):
    v, p, e, par_v, par_e = find_cycle(N, M, G)
    if p == -1:
        return [], []
    else:
        cycle_v = [p]
        cycle_e = [e]
        while v != p:
            e = par_e[p]
            p = par_v[p]
            cycle_v.append(p)
            cycle_e.append(e)
        return cycle_v[::-1], cycle_e[::-1]
    

def find_cycle2(N, M, G):
    vis = [0] * N
    used = [0] * M
    par_v = [None] * N
    par_e = [None] * N
    potential = [0] * N

    for s in range(N):
        if vis[s]: continue
        stack = [(s, -1, -1, 0)]
        while stack:
            v, p, e, pot = stack.pop()
            if e != -1 and used[e]: continue
            if vis[v] and pot != potential[v]:
                par_v[v] = p
                if e != -1:
                    par_e[v] = e
                return v, p, e, par_v, par_e
            vis[v] = 1
            potential[v] = pot
            if e != -1:
                par_e[v] = e
                used[e] = 1
            par_v[v] = p
            for a, e, w in G[v]:
                if used[e]: continue
                stack.append((a, v, e, pot + w))

    return -1, -1, -1, par_v, par_e


def cycle_detection2(N, M, G):
    v, p, e, par_v, par_e = find_cycle2(N, M, G)
    #print(v, p, e, par_v, par_e)
    if p == -1:
        return [], []
    else:
        cycle_v = [p]
        cycle_e = [e]
        while v != p:
            e = par_e[p]
            p = par_v[p]
            cycle_v.append(p)
            cycle_e.append(e)
        return cycle_v[::-1], cycle_e[::-1]

n, m = na()

g = [[] for i in range(n)]
E = []
for i in range(m):
    a, b, w = na()
    g[a-1].append((b-1, i, w))
    g[b-1].append((a-1, i, -w))
    E.append((a-1, b-1, w))

cycle_v, cycle_e = cycle_detection2(n, m, g)
#print(cycle_v, cycle_e)
if not cycle_v:
    print(-1)
else:
    print(len(cycle_v))
    ans = 0
    c = 0
    for i in cycle_e:
        if E[i][0] == c:
            ans += E[i][2]
            c = E[i][1]
        else:
            ans -= E[i][2]
            c = E[i][0]
    #print(ans)
    if ans > 0:
        print(cycle_v[0]+1)
        print(*[i+1 for i in cycle_e])
    else:
        print(cycle_v[0]+1)
        print(*[i+1 for i in cycle_e[::-1]])
0