結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー ikomaikoma
提出日時 2023-08-04 22:52:20
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,970 bytes
コンパイル時間 278 ms
コンパイル使用メモリ 82,132 KB
実行使用メモリ 113,904 KB
最終ジャッジ日時 2024-04-22 21:12:49
合計ジャッジ時間 4,975 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
55,000 KB
testcase_01 AC 43 ms
54,932 KB
testcase_02 AC 42 ms
54,728 KB
testcase_03 AC 42 ms
55,332 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 42 ms
54,456 KB
testcase_15 AC 42 ms
56,008 KB
testcase_16 AC 42 ms
54,872 KB
testcase_17 AC 42 ms
55,004 KB
testcase_18 AC 41 ms
55,564 KB
testcase_19 AC 107 ms
113,904 KB
testcase_20 AC 57 ms
71,456 KB
testcase_21 AC 69 ms
87,388 KB
testcase_22 AC 106 ms
113,588 KB
testcase_23 AC 104 ms
113,332 KB
testcase_24 AC 104 ms
105,820 KB
testcase_25 AC 71 ms
91,844 KB
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 #

import sys
input = sys.stdin.readline
from collections import defaultdict
class UnionFind:
    def __init__(self, n:int):
        self.n = n
        self.parents = [-1] * n
    def find(self, x:int):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    def union(self, x:int, y:int):
        x = self.find(x)
        y = self.find(y)
        if x == y: return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
    def size(self, x:int):
        return -self.parents[self.find(x)]
    def same(self, x:int, y:int):
        return self.find(x) == self.find(y)
    def group_count(self):
        return len(self.roots())
    def all_group_members(self):
        members = defaultdict(list)
        for i in range(self.n): members[self.find(i)].append(i)
        return members


N,M=map(int,input().split())
uf = UnionFind(N)
dif = [0]*N
loop = [0]*N
for _ in range(M):
    u,v=map(lambda x:int(x)-1,input().split())
    if u==v:
        loop[u]=1
        continue
    dif[u]+=1
    dif[v]-=1
    uf.union(u,v)
# グループごとに計算
members = uf.all_group_members()
ans_list = []
for member in members.values():
    if len(member)==1 and loop[member[0]]==0:continue
    x=sum([abs(dif[m]) for m in member])//2
    if x:
        x-=1
    ans_list.append(x)
ans_list.sort()
# 0は他と接続コスト必要
ans = sum(ans_list)
if ans==0:
    ans = len(ans_list)-1
else:
    zero = 0
    one = 0
    tmp=[]
    for a in ans_list:
        if a==0:
            zero+=1
        elif a==1:
            one += 1
        else:
            tmp.append(a)
    # 1は2以上とつなぐ
    tmp.sort()
    for a in tmp:
        d = min(a-1,one)
        one -= d
        if a-d==1:
            one+=1
    ans += one//2
    if one:
        zero+=1
    ans += zero-1


print(ans)
0