結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー lam6er
提出日時 2025-03-20 20:58:04
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,897 bytes
コンパイル時間 161 ms
コンパイル使用メモリ 82,472 KB
実行使用メモリ 124,384 KB
最終ジャッジ日時 2025-03-20 20:58:32
合計ジャッジ時間 3,404 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 21 WA * 10
権限があれば一括ダウンロードができます

ソースコード

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]

    cur_odd = 0
    for i in range(1, N + 1):
        if d[i] % 2 != 0:
            cur_odd += 1

    if cur_odd % 2 != 0:
        print(-1)
        return

    sum_plus = 0
    for v in d:
        if v > 0:
            sum_plus += v

    case1_possible = True
    for i in range(1, N + 1):
        if d[i] % 2 != 0:
            case1_possible = False
            break
    if case1_possible:
        if sum_plus % 2 == 0:
            K1 = sum_plus
        else:
            case1_possible = False
    else:
        case1_possible = False

    has_s = any(d[i] >= 1 for i in range(1, N + 1))
    has_t = any(d[i] <= -1 for i in range(1, N + 1))

    if has_s and has_t:
        K2 = sum_plus - 1
    else:
        min_a = -1 if has_s else 0
        min_b = float('inf')
        for i in range(1, N + 1):
            if d[i] <= -1:
                current_b = 0
            elif d[i] < 0:
                current_b = d[i] + 1
            else:
                current_b = 1
            if current_b < min_b:
                min_b = current_b
        K2 = sum_plus + (min_a + min_b)

    possible = []
    if case1_possible:
        possible.append(K1)
    possible.append(K2)

    valid = False
    if case1_possible or (has_s and has_t) or (min_b != float('inf')):
        valid = True

    if not valid:
        print(-1)
    else:
        print(min(possible) if possible else -1)

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