結果

問題 No.1390 Get together
ユーザー shotoyooshotoyoo
提出日時 2021-02-12 21:25:27
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 427 ms / 2,000 ms
コード長 1,243 bytes
コンパイル時間 403 ms
コンパイル使用メモリ 87,148 KB
実行使用メモリ 100,352 KB
最終ジャッジ日時 2023-09-27 02:44:39
合計ジャッジ時間 9,167 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,304 KB
testcase_01 AC 73 ms
71,364 KB
testcase_02 AC 73 ms
71,240 KB
testcase_03 AC 118 ms
78,544 KB
testcase_04 AC 114 ms
78,312 KB
testcase_05 AC 117 ms
78,212 KB
testcase_06 AC 109 ms
78,320 KB
testcase_07 AC 108 ms
78,076 KB
testcase_08 AC 110 ms
78,384 KB
testcase_09 AC 118 ms
78,284 KB
testcase_10 AC 70 ms
71,444 KB
testcase_11 AC 71 ms
71,516 KB
testcase_12 AC 70 ms
71,260 KB
testcase_13 AC 72 ms
71,500 KB
testcase_14 AC 73 ms
71,556 KB
testcase_15 AC 72 ms
71,132 KB
testcase_16 AC 233 ms
94,900 KB
testcase_17 AC 287 ms
100,352 KB
testcase_18 AC 216 ms
95,608 KB
testcase_19 AC 362 ms
98,372 KB
testcase_20 AC 406 ms
98,796 KB
testcase_21 AC 427 ms
98,952 KB
testcase_22 AC 325 ms
97,952 KB
testcase_23 AC 325 ms
98,260 KB
testcase_24 AC 309 ms
98,040 KB
testcase_25 AC 385 ms
98,752 KB
testcase_26 AC 364 ms
98,856 KB
testcase_27 AC 348 ms
98,620 KB
testcase_28 AC 373 ms
98,996 KB
testcase_29 AC 367 ms
98,740 KB
testcase_30 AC 369 ms
98,784 KB
testcase_31 AC 355 ms
98,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda : sys.stdin.readline().rstrip()

sys.setrecursionlimit(2*10**5+10)
write = lambda x: sys.stdout.write(x+"\n")

class UF:
    # unionfind
    def __init__(self, n):
        self.n = n
        self.parent = list(range(n))
        self.size = [1] * n
    def root(self, i):
        inter = set()
        while self.parent[i]!=i:
            inter.add(i)
            i = self.parent[i]
        r = i
        for i in inter:
            self.parent[i] = r
        return r
    def connect(self, i, j):
        # 繋いだかどうかを返す
        ri = self.root(i)
        rj = self.root(j)
        if ri==rj:
            return False
        if self.size[ri]<self.size[rj]:
            self.parent[ri] = rj
            self.size[rj] += self.size[ri]
        else:
            self.parent[rj] = ri
            self.size[ri] += self.size[rj]
        return True

n,m = list(map(int, input().split()))
cs = [[] for _ in range(n)]
for i in range(n):
    b,c = map(int, input().split())
    b -= 1
    c -= 1
    cs[c].append(b)
#     cs[b].append(c)
uf = UF(m)
ans = 0
for c in range(n):
    if not cs[c]:
        continue
    b0 = cs[c][0]
    for b in cs[c]:
        if uf.connect(b,b0):
            ans += 1
print(ans)
0