結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー gew1fw
提出日時 2025-06-12 19:58:23
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,050 bytes
コンパイル時間 293 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 123,776 KB
最終ジャッジ日時 2025-06-12 20:01:20
合計ジャッジ時間 3,596 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 8 WA * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

def main():
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx])
    idx += 1
    M = int(input[idx])
    idx += 1

    balance = [0] * (N + 1)  # 1-based indexing

    for _ in range(M):
        u = int(input[idx])
        idx += 1
        v = int(input[idx])
        idx += 1
        balance[u] += 1
        balance[v] -= 1

    P = 0
    N_count = 0
    S_plus = 0
    for i in range(1, N+1):
        b = balance[i]
        if b > 0:
            P += 1
            S_plus += b
        elif b < 0:
            N_count += 1

    # Check if already satisfies Eulerian trail condition
    if (P == 0 and N_count == 0) or (P == 1 and N_count == 1 and S_plus == 1):
        print(0)
        return

    # Check if P and N are in the correct relationship
    if not ((P == N_count) or (P == N_count + 1) or (N_count == P + 1)):
        print(-1)
        return

    # Calculate E
    E = max(P-1, N_count-1)
    if (S_plus - E) in (0, 1):
        print(E)
    else:
        print(-1)

if __name__ == "__main__":
    main()
0