結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー gew1fw
提出日時 2025-06-12 14:41:49
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,102 bytes
コンパイル時間 364 ms
コンパイル使用メモリ 82,416 KB
実行使用メモリ 124,096 KB
最終ジャッジ日時 2025-06-12 14:42:05
合計ジャッジ時間 3,556 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 1 WA * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

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

    out_degree = [0] * (N + 1)
    in_degree = [0] * (N + 1)

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

    d = [0] * (N + 1)
    for i in range(1, N + 1):
        d[i] = out_degree[i] - in_degree[i]

    sum_d = sum(d)
    if sum_d != 0:
        print(-1)
        return

    non_zero = []
    for i in range(1, N + 1):
        if d[i] != 0:
            non_zero.append(d[i])

    cnt_nonzero = len(non_zero)
    if cnt_nonzero == 0:
        print(0)
        return
    elif cnt_nonzero == 2:
        a, b = non_zero
        if (a == 1 and b == -1) or (a == -1 and b == 1):
            print(0)
        else:
            print(-1)
        return
    else:
        S = sum(x for x in non_zero if x > 0)
        if S % 2 != 0:
            print(-1)
        else:
            print(S // 2)

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