結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー ntudantuda
提出日時 2023-08-05 12:56:07
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,210 bytes
コンパイル時間 149 ms
コンパイル使用メモリ 82,596 KB
実行使用メモリ 98,600 KB
最終ジャッジ日時 2024-04-23 10:08:54
合計ジャッジ時間 5,247 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
52,352 KB
testcase_01 AC 34 ms
52,480 KB
testcase_02 AC 35 ms
52,840 KB
testcase_03 AC 40 ms
52,480 KB
testcase_04 AC 196 ms
86,824 KB
testcase_05 AC 269 ms
95,508 KB
testcase_06 AC 300 ms
97,544 KB
testcase_07 AC 205 ms
89,472 KB
testcase_08 AC 240 ms
91,648 KB
testcase_09 AC 300 ms
98,600 KB
testcase_10 AC 283 ms
97,792 KB
testcase_11 AC 247 ms
93,176 KB
testcase_12 AC 298 ms
98,584 KB
testcase_13 AC 230 ms
91,904 KB
testcase_14 AC 36 ms
52,608 KB
testcase_15 AC 35 ms
52,480 KB
testcase_16 AC 34 ms
52,480 KB
testcase_17 AC 33 ms
52,096 KB
testcase_18 AC 34 ms
51,968 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

'''
YC2403
"Eight" Bridges of Königsberg
有向グラフに辺を追加して、各辺を1回ずつ通るようにする
最小追加変数は幾つか?
必要な条件
・連結であること
・出次数と入次数がすべて等しいか、差が+1の頂点と-1の頂点が1個ずつある


'''
def root(x):
    if P[x] < 0: return x
    P[x] = root(P[x])  # 経路圧縮
    return P[x]

def unite(x,y):
    x = root(x)
    y = root(y)
    if x == y: return
    if x > y: x,y = y,x
    P[x] += P[y]
    P[y] = x

def same(x,y):
    return root(x) == root(y)

def size(x):
    x = root(x)
    return -P[x]

def group_count():
    S = set()
    for i in range(N+1):
        if size(i) > 1:
            S.add(root(i))
        if size(i) == 1 and G[i] == True:
            S.add(i)

    return len(S)

N,M = map(int,input().split())
P = [-1] * (N + 1)

UV = [list(map(int,input().split())) for _ in range(M)]
F = [[0,0] for _ in range(N+1)]
G = [False] * (N+1)
for u,v in UV:
    F[u][0] += 1
    F[v][1] += 1
    G[u] = True
    G[v] = True
    unite(u,v)
tmp = group_count()
tmp2 = 0
for i in range(N+1):
    tmp2 += abs(F[i][0] - F[i][1])
tmp2 //= 2
if tmp2 > 0:
    tmp2 -= 1
print(tmp2 + tmp - 1)

0