結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー mattu34mattu34
提出日時 2023-09-08 01:51:26
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,895 bytes
コンパイル時間 771 ms
コンパイル使用メモリ 86,792 KB
実行使用メモリ 97,336 KB
最終ジャッジ日時 2023-09-08 01:51:36
合計ジャッジ時間 9,769 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 86 ms
76,196 KB
testcase_01 AC 87 ms
71,524 KB
testcase_02 AC 86 ms
71,720 KB
testcase_03 AC 115 ms
71,704 KB
testcase_04 AC 212 ms
79,572 KB
testcase_05 AC 263 ms
79,380 KB
testcase_06 AC 282 ms
80,712 KB
testcase_07 AC 219 ms
78,736 KB
testcase_08 AC 257 ms
79,736 KB
testcase_09 AC 309 ms
80,124 KB
testcase_10 AC 269 ms
78,944 KB
testcase_11 AC 255 ms
79,392 KB
testcase_12 AC 291 ms
80,120 KB
testcase_13 AC 270 ms
79,388 KB
testcase_14 AC 91 ms
71,392 KB
testcase_15 AC 95 ms
71,876 KB
testcase_16 AC 91 ms
71,768 KB
testcase_17 AC 88 ms
71,812 KB
testcase_18 AC 90 ms
71,704 KB
testcase_19 TLE -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

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.roots()
ans = 0
for g in group:
    cnt = 0
    ok = False
    for member in uf.members(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