結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー mattu34mattu34
提出日時 2023-09-08 01:56:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 347 ms / 2,000 ms
コード長 1,946 bytes
コンパイル時間 602 ms
コンパイル使用メモリ 87,044 KB
実行使用メモリ 107,584 KB
最終ジャッジ日時 2023-09-08 01:57:00
合計ジャッジ時間 8,546 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
71,736 KB
testcase_01 AC 90 ms
71,888 KB
testcase_02 AC 90 ms
71,676 KB
testcase_03 AC 90 ms
71,536 KB
testcase_04 AC 220 ms
79,568 KB
testcase_05 AC 271 ms
79,408 KB
testcase_06 AC 347 ms
80,660 KB
testcase_07 AC 221 ms
79,092 KB
testcase_08 AC 250 ms
79,504 KB
testcase_09 AC 296 ms
80,092 KB
testcase_10 AC 275 ms
79,340 KB
testcase_11 AC 267 ms
80,180 KB
testcase_12 AC 301 ms
80,192 KB
testcase_13 AC 279 ms
79,332 KB
testcase_14 AC 94 ms
71,560 KB
testcase_15 AC 94 ms
71,584 KB
testcase_16 AC 100 ms
71,568 KB
testcase_17 AC 94 ms
71,932 KB
testcase_18 AC 94 ms
71,588 KB
testcase_19 AC 142 ms
107,584 KB
testcase_20 AC 110 ms
81,640 KB
testcase_21 AC 121 ms
92,716 KB
testcase_22 AC 136 ms
104,232 KB
testcase_23 AC 137 ms
101,452 KB
testcase_24 AC 166 ms
98,468 KB
testcase_25 AC 126 ms
96,656 KB
testcase_26 AC 99 ms
72,268 KB
testcase_27 AC 185 ms
104,164 KB
testcase_28 AC 176 ms
99,028 KB
testcase_29 AC 145 ms
84,116 KB
testcase_30 AC 133 ms
89,092 KB
testcase_31 AC 187 ms
105,068 KB
testcase_32 AC 133 ms
78,476 KB
testcase_33 AC 125 ms
85,816 KB
testcase_34 AC 140 ms
81,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import *
import heapq
import bisect
import itertools


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 all_group_members(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 "\n".join(f"{r}: {m}" for r, m in self.all_group_members().items())


INF = float("inf")
MOD = 998244353
mod = 998244353

N, M = map(int, input().split())
indeg = [0] * N
outdeg = [0] * N
uf = UnionFind(N)
for _ in range(M):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    uf.union(u, v)
    indeg[v] += 1
    outdeg[u] += 1
group = uf.all_group_members()
# print(group)
ans = 0
for p, g in group.items():
    cnt = 0
    ok = False
    for member in g:
        cnt += abs(indeg[member] - outdeg[member])
        if outdeg[member] > 0:
            ok = True
    if not ok:
        continue
    cnt //= 2
    ans += max(cnt - 1, 0) + 1
print(ans - 1)
0