結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2023-08-04 22:48:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 258 ms / 2,000 ms
コード長 1,984 bytes
コンパイル時間 277 ms
コンパイル使用メモリ 87,244 KB
実行使用メモリ 82,572 KB
最終ジャッジ日時 2023-08-17 18:40:27
合計ジャッジ時間 6,452 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,164 KB
testcase_01 AC 74 ms
71,364 KB
testcase_02 AC 76 ms
71,496 KB
testcase_03 AC 74 ms
71,384 KB
testcase_04 AC 176 ms
77,968 KB
testcase_05 AC 241 ms
78,976 KB
testcase_06 AC 258 ms
78,932 KB
testcase_07 AC 183 ms
78,072 KB
testcase_08 AC 210 ms
78,440 KB
testcase_09 AC 254 ms
78,352 KB
testcase_10 AC 234 ms
78,396 KB
testcase_11 AC 222 ms
78,652 KB
testcase_12 AC 253 ms
78,288 KB
testcase_13 AC 218 ms
78,260 KB
testcase_14 AC 73 ms
71,496 KB
testcase_15 AC 71 ms
71,412 KB
testcase_16 AC 72 ms
71,152 KB
testcase_17 AC 72 ms
71,136 KB
testcase_18 AC 75 ms
71,540 KB
testcase_19 AC 92 ms
81,420 KB
testcase_20 AC 83 ms
77,236 KB
testcase_21 AC 88 ms
78,732 KB
testcase_22 AC 90 ms
80,768 KB
testcase_23 AC 87 ms
80,472 KB
testcase_24 AC 93 ms
79,900 KB
testcase_25 AC 86 ms
79,608 KB
testcase_26 AC 77 ms
71,592 KB
testcase_27 AC 121 ms
82,572 KB
testcase_28 AC 108 ms
81,084 KB
testcase_29 AC 106 ms
78,188 KB
testcase_30 AC 94 ms
78,308 KB
testcase_31 AC 114 ms
82,552 KB
testcase_32 AC 105 ms
76,644 KB
testcase_33 AC 94 ms
77,928 KB
testcase_34 AC 113 ms
78,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""

https://yukicoder.me/problems/no/2403

オイラー路を作れってこと
全体を連結にする

まず、全体が連結じゃないと駄目なんだよな…

in , out が打ち消し合って居ない場合…

連結成分ごとに
in/outの欲しい個数を書いておくか…

後は?
連結成分内で、in/outの溢れ個数は同じ?
だな

とりあえずくっ付ける必要はある
i/o を1つずつ消費してくっ付けられる

a,b -> a+b-1

になる

1,1 -> 1 なので…?
0以外は適当にくっ付けてokっぽい

適当にくっ付けた後、
0 をくっ付ける必要がある。


1,1 0,0

-> 1,1 になる

"""

import sys
from sys import stdin

def uf_find(n,p):

    ufl = []

    while p[n] != n:
        ufl.append(n)
        n = p[n]

    for i in ufl:
        p[i] = n

    return n


def uf_union(a,b,p,rank):

    ap = uf_find(a,p)
    bp = uf_find(b,p)

    if ap == bp:
        return True
    else:

        if rank[ap] > rank[bp]:
            p[bp] = ap
        elif rank[ap] < rank[bp]:
            p[ap] = bp
        else:
            p[bp] = ap
            rank[ap] += 1

        return False

N,M = map(int,stdin.readline().split())

D = [0] * N
p = [i for i in range(N)]
rank = [0] * N

exist = [False] * N
for i in range(M):

    u,v = map(int,stdin.readline().split())
    u -= 1
    v -= 1

    uf_union(u,v,p,rank)
    D[u] += 1
    D[v] -= 1
    exist[u] = exist[v] = True

dic = {}
#print (D)

for i in range(N):

    np = uf_find(i,p)
    if (np not in dic) and exist[np]:
        dic[np] = 0

    if D[i] > 0:
        dic[np] += D[i]

lis = []
for i in dic:
    lis.append(dic[i])

lis.sort()

print (lis,file=sys.stderr)

ans = 0
while len(lis) >= 2:

    r = lis.pop()
    l = lis.pop()

    if l == 0 and r == 0:
        ans += 1
        lis.append(1)
    elif l == 0:
        ans += 1
        lis.append(r)
    else:
        ans += 1
        lis.append(l+r-1)

ans += max(0,lis[0]-1)

print (ans)
0