結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー bug maker / ばぐめいかー@Python学習者bug maker / ばぐめいかー@Python学習者
提出日時 2023-08-05 12:18:03
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,484 bytes
コンパイル時間 125 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 48,384 KB
最終ジャッジ日時 2024-04-23 09:27:59
合計ジャッジ時間 5,970 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
10,752 KB
testcase_01 AC 27 ms
10,624 KB
testcase_02 AC 25 ms
10,880 KB
testcase_03 AC 26 ms
10,624 KB
testcase_04 AC 181 ms
27,520 KB
testcase_05 AC 346 ms
43,008 KB
testcase_06 AC 364 ms
45,056 KB
testcase_07 AC 235 ms
32,000 KB
testcase_08 AC 264 ms
35,200 KB
testcase_09 AC 405 ms
47,744 KB
testcase_10 AC 407 ms
47,488 KB
testcase_11 AC 295 ms
37,888 KB
testcase_12 AC 416 ms
48,384 KB
testcase_13 AC 279 ms
35,456 KB
testcase_14 AC 26 ms
10,880 KB
testcase_15 AC 26 ms
10,752 KB
testcase_16 AC 26 ms
10,752 KB
testcase_17 AC 26 ms
10,752 KB
testcase_18 AC 27 ms
10,880 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

def main():
    N, M = map(int, input().split())
    
    l = [list(map(int, input().split())) for l in range(M)]

    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 roots(self):
            return [i for i, x in enumerate(self.parents) if x < 0]
        def group_count(self):
            return len(self.roots())

    lout = [0] * N
    lin = [0] * N
    lans = [0] * N

    uf = UnionFind(N)
    
    for i in range(M):
        uf.union(l[i][0] - 1, l[i][1] - 1)
        lout[l[i][0] - 1] += 1
        lin[l[i][1] - 1] += 1
    
    ecpt = 0
    
    for i in range(N):
        lans[i] = lout[i] - lin[i]
        if lout[i] == lin[i] == 0:
            ecpt += 1
        
    rc = uf.group_count()

    if set(lans) == {0}:
        print(rc - 1 - ecpt)
    else:
        print(sum([i for i in lans if i > 0]) - 1 + (rc - 1) - ecpt)

if __name__ == '__main__':
    main()
0