結果

問題 No.1390 Get together
ユーザー Kiri8128Kiri8128
提出日時 2021-02-12 21:26:25
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 424 ms / 2,000 ms
コード長 1,343 bytes
コンパイル時間 583 ms
コンパイル使用メモリ 87,324 KB
実行使用メモリ 124,924 KB
最終ジャッジ日時 2023-09-27 02:47:26
合計ジャッジ時間 9,730 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,168 KB
testcase_01 AC 71 ms
71,404 KB
testcase_02 AC 71 ms
71,224 KB
testcase_03 AC 111 ms
78,132 KB
testcase_04 AC 108 ms
77,940 KB
testcase_05 AC 112 ms
78,264 KB
testcase_06 AC 109 ms
78,076 KB
testcase_07 AC 107 ms
78,176 KB
testcase_08 AC 106 ms
78,328 KB
testcase_09 AC 116 ms
78,148 KB
testcase_10 AC 74 ms
71,044 KB
testcase_11 AC 73 ms
71,424 KB
testcase_12 AC 72 ms
71,308 KB
testcase_13 AC 71 ms
71,288 KB
testcase_14 AC 71 ms
70,880 KB
testcase_15 AC 71 ms
71,172 KB
testcase_16 AC 244 ms
113,048 KB
testcase_17 AC 319 ms
124,924 KB
testcase_18 AC 214 ms
106,728 KB
testcase_19 AC 413 ms
119,132 KB
testcase_20 AC 392 ms
121,328 KB
testcase_21 AC 391 ms
122,796 KB
testcase_22 AC 338 ms
116,352 KB
testcase_23 AC 341 ms
116,192 KB
testcase_24 AC 349 ms
115,884 KB
testcase_25 AC 424 ms
119,288 KB
testcase_26 AC 414 ms
118,704 KB
testcase_27 AC 422 ms
118,476 KB
testcase_28 AC 404 ms
119,340 KB
testcase_29 AC 417 ms
119,168 KB
testcase_30 AC 409 ms
119,116 KB
testcase_31 AC 421 ms
118,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda: sys.stdin.readline().rstrip()
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.PA = [-1] * n
    def root(self, a):
        L = []
        while self.PA[a] >= 0:
            L.append(a)
            a = self.PA[a]
        for l in L:
            self.PA[l] = a
        return a
    def unite(self, a, b):
        ra, rb = self.root(a), self.root(b)
        if ra != rb:
            if self.PA[rb] >= self.PA[ra]:
                self.PA[ra] += self.PA[rb]
                self.PA[rb] = ra
            else:
                self.PA[rb] += self.PA[ra]
                self.PA[ra] = rb
    def size(self, a):
        return -self.PA[self.root(a)]
    def groups(self):
        G = [[] for _ in range(self.n)]
        for i in range(self.n):
            G[self.root(i)].append(i)
        return [g for g in G if g]
    def group_size(self):
        G = [[] for _ in range(self.n)]
        for i in range(self.n):
            G[self.root(i)].append(i)
        return [len(g) for g in G if g]

N, M = map(int, input().split())
uf = UnionFind(M)
C = [[] for _ in range(N)]
for _ in range(N):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    C[b].append(a)

for c in C:
    if len(c) > 1:
        i = c[0]
        for j in c[1:]:
            uf.unite(i, j)
print(M - len(uf.groups()))
0