結果

問題 No.2403 "Eight" Bridges of Königsberg
コンテスト
ユーザー gr1msl3y
提出日時 2023-08-13 13:31:16
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
WA  
実行時間 -
コード長 796 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 216 ms
コンパイル使用メモリ 85,504 KB
実行使用メモリ 94,720 KB
最終ジャッジ日時 2026-05-15 13:35:59
合計ジャッジ時間 4,709 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 25 WA * 6
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

N, K = map(int, input().split())
g0 = [[] for _ in range(N+1)]
g1 = [[] for _ in range(N+1)]
for _ in range(K):
    a, b = map(int, input().split())
    g0[a].append(b)
    g1[b].append(a)

ans = 0
seen = [0]*(N+1)
ct0 = ct1 = 0
for i in range(1, N+1):
    if seen[i]:
        continue
    seen[i] = 1
    task = [i]
    for v in task:
        for u in g0[v]:
            if seen[u]:
                continue
            seen[u] = 1
            task.append(u)
        for u in g1[v]:
            if seen[u]:
                continue
            seen[u] = 1
            task.append(u)
    res = sum(abs(len(g0[v])-len(g1[v])) for v in task)//2
    if len(task) == 1:
        continue
    if res:
        ct1 += 1
        ans += res-1
    else:
        ct0 += 1
ans += max(0, ct0+ct1-1)
print(ans)
0