結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー 👑 rin204rin204
提出日時 2023-08-04 22:26:49
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,375 bytes
コンパイル時間 283 ms
コンパイル使用メモリ 82,128 KB
実行使用メモリ 81,712 KB
最終ジャッジ日時 2024-04-22 20:44:20
合計ジャッジ時間 5,499 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,480 KB
testcase_01 AC 39 ms
52,480 KB
testcase_02 AC 39 ms
52,096 KB
testcase_03 AC 41 ms
52,472 KB
testcase_04 AC 177 ms
77,340 KB
testcase_05 AC 250 ms
77,184 KB
testcase_06 AC 266 ms
77,696 KB
testcase_07 AC 196 ms
77,280 KB
testcase_08 AC 213 ms
77,220 KB
testcase_09 AC 279 ms
77,348 KB
testcase_10 AC 245 ms
77,044 KB
testcase_11 AC 220 ms
77,064 KB
testcase_12 AC 265 ms
77,640 KB
testcase_13 AC 216 ms
77,232 KB
testcase_14 AC 38 ms
52,736 KB
testcase_15 AC 38 ms
52,736 KB
testcase_16 AC 38 ms
52,096 KB
testcase_17 WA -
testcase_18 AC 37 ms
52,864 KB
testcase_19 AC 59 ms
67,840 KB
testcase_20 AC 55 ms
63,616 KB
testcase_21 AC 54 ms
65,536 KB
testcase_22 AC 57 ms
67,456 KB
testcase_23 AC 56 ms
66,176 KB
testcase_24 AC 65 ms
69,760 KB
testcase_25 AC 55 ms
64,896 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 96 ms
80,768 KB
testcase_29 WA -
testcase_30 AC 66 ms
70,784 KB
testcase_31 AC 98 ms
81,712 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 103 ms
77,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

    def find(self, x):
        if self.par[x] < 0:
            return x
        lst = []
        while self.par[x] >= 0:
            lst.append(x)
            x = self.par[x]
        for y in lst:
            self.par[y] = x
        return x

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False

        if self.par[x] > self.par[y]:
            x, y = y, x

        self.par[x] += self.par[y]
        self.par[y] = x
        self.group_ -= 1
        return True

    def size(self, x):
        return -self.par[self.find(x)]

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

    @property
    def group(self):
        return self.group_


n, m = map(int, input().split())
in_ = [0] * n
out_ = [0] * n
UF = UnionFind(n)

for _ in range(m):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    in_[v] += 1
    out_[u] += 1
    UF.unite(u, v)

tot = 0
for i, o in zip(in_, out_):
    tot += max(0, i - o)

C = [False] * n
for i in range(n):
    if in_[i] != out_[i]:
        C[UF.find(i)] = True

z = 0
for i in range(n):
    if UF.find(i) == i and not C[i] and UF.size(i) > 1:
        z += 1
        C[i] = True

print(max(0, tot - 1) + min(z, UF.group - 1))
0