結果

問題 No.1390 Get together
ユーザー LignanLignan
提出日時 2021-06-19 07:42:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 474 ms / 2,000 ms
コード長 1,649 bytes
コンパイル時間 1,115 ms
コンパイル使用メモリ 86,980 KB
実行使用メモリ 112,652 KB
最終ジャッジ日時 2023-09-05 00:41:56
合計ジャッジ時間 12,894 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
71,652 KB
testcase_01 AC 90 ms
71,648 KB
testcase_02 AC 91 ms
71,580 KB
testcase_03 AC 151 ms
81,416 KB
testcase_04 AC 146 ms
81,340 KB
testcase_05 AC 155 ms
81,736 KB
testcase_06 AC 147 ms
81,544 KB
testcase_07 AC 147 ms
81,840 KB
testcase_08 AC 145 ms
81,484 KB
testcase_09 AC 154 ms
81,612 KB
testcase_10 AC 92 ms
71,304 KB
testcase_11 AC 93 ms
71,900 KB
testcase_12 AC 92 ms
71,660 KB
testcase_13 AC 92 ms
71,328 KB
testcase_14 AC 93 ms
71,656 KB
testcase_15 AC 93 ms
71,492 KB
testcase_16 AC 264 ms
111,884 KB
testcase_17 AC 366 ms
110,392 KB
testcase_18 AC 251 ms
107,432 KB
testcase_19 AC 445 ms
109,964 KB
testcase_20 AC 447 ms
110,008 KB
testcase_21 AC 464 ms
110,764 KB
testcase_22 AC 375 ms
112,652 KB
testcase_23 AC 373 ms
112,152 KB
testcase_24 AC 362 ms
112,140 KB
testcase_25 AC 458 ms
110,200 KB
testcase_26 AC 455 ms
110,336 KB
testcase_27 AC 448 ms
109,888 KB
testcase_28 AC 446 ms
110,008 KB
testcase_29 AC 439 ms
110,288 KB
testcase_30 AC 474 ms
110,596 KB
testcase_31 AC 460 ms
110,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n,m= map(int,input().split())

lst=[[] for i in range(n)]
for i in range(n):
    b,c=map(int,input().split())
    b-=1
    c-=1
    lst[c].append(b)

from collections import defaultdict

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())
    
slime=UnionFind(m)
cnt1=slime.group_count()

for l in lst:
    for i in range(len(l)):
        if not slime.same(l[0],l[i]):
            slime.union(l[0],l[i])
cnt2=slime.group_count()

print(cnt1-cnt2)
0