結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー rlangevinrlangevin
提出日時 2023-08-06 17:55:15
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,282 bytes
コンパイル時間 151 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 78,080 KB
最終ジャッジ日時 2024-04-24 12:47:35
合計ジャッジ時間 4,627 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
52,224 KB
testcase_01 AC 42 ms
52,352 KB
testcase_02 AC 41 ms
51,968 KB
testcase_03 AC 42 ms
52,480 KB
testcase_04 AC 155 ms
76,672 KB
testcase_05 AC 199 ms
77,184 KB
testcase_06 AC 196 ms
77,696 KB
testcase_07 AC 154 ms
76,672 KB
testcase_08 AC 186 ms
77,824 KB
testcase_09 AC 203 ms
78,080 KB
testcase_10 AC 175 ms
77,312 KB
testcase_11 AC 182 ms
77,056 KB
testcase_12 AC 209 ms
77,440 KB
testcase_13 AC 207 ms
77,800 KB
testcase_14 AC 42 ms
52,352 KB
testcase_15 AC 42 ms
51,968 KB
testcase_16 AC 38 ms
52,480 KB
testcase_17 AC 38 ms
51,968 KB
testcase_18 AC 38 ms
52,352 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
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

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

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

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]



N, M = map(int, input().split())
S, T = [0] * N, [0] * N
U = UnionFind(N)
for i in range(M):
    u, v = map(int, input().split())
    u, v = u - 1, v - 1
    U.union(u, v)
    S[u] += 1
    T[v] += 1
    
ans = 0
SS = set()
for i in range(N):
    ans += max(0, S[i] - T[i])
    if S[i] == T[i] == 0:
        continue
    SS.add(U.find(i))
if ans == 0:
    print(len(SS) - 1)
else:
    print(max(0, ans - 1 + len(SS) - 1))
0