結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー MottchanMottchan
提出日時 2023-08-04 23:24:13
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,962 bytes
コンパイル時間 513 ms
コンパイル使用メモリ 82,120 KB
実行使用メモリ 123,988 KB
最終ジャッジ日時 2024-04-22 21:47:57
合計ジャッジ時間 8,378 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
54,984 KB
testcase_01 AC 42 ms
55,832 KB
testcase_02 AC 42 ms
55,104 KB
testcase_03 AC 41 ms
55,564 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 AC 42 ms
54,764 KB
testcase_16 RE -
testcase_17 WA -
testcase_18 WA -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        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):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def group(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return ''.join(f'{r}: {m}' for r, m in self.group().items())

n,m = map(int,input().split())
uf = UnionFind(n)
graph = [[] for i in range(n+1)]
e=[]
dout = defaultdict(int)
din = defaultdict(int)
for i in range(m):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)
    e.append([a,b])
    uf.union(a,b)
    dout[b]+=1
    din[a]+=1

cnt1=[[]*n]
cnt2=-1

for i,l in uf.group().items():
    if len(l)==1:continue
    cnt2+=1
    for j in l:
        if dout[j] == din[j]:
            continue
        else:
            cnt1[i].append(abs(dout[j] - din[j]))
ans=0
for l in cnt1:
    s=sum(l)
    if s ==0:
        continue
    else:
        ans+=s//2-1

if ans==0:
    print(0+cnt2)
else:
    print(ans+cnt2)
0