結果

問題 No.1390 Get together
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2021-05-11 13:24:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 469 ms / 2,000 ms
コード長 1,013 bytes
コンパイル時間 1,067 ms
コンパイル使用メモリ 81,824 KB
実行使用メモリ 118,000 KB
最終ジャッジ日時 2023-10-21 17:31:21
合計ジャッジ時間 10,319 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,580 KB
testcase_01 AC 37 ms
53,580 KB
testcase_02 AC 36 ms
53,580 KB
testcase_03 AC 86 ms
76,912 KB
testcase_04 AC 90 ms
76,884 KB
testcase_05 AC 91 ms
76,960 KB
testcase_06 AC 88 ms
76,820 KB
testcase_07 AC 88 ms
76,780 KB
testcase_08 AC 85 ms
76,640 KB
testcase_09 AC 94 ms
77,132 KB
testcase_10 AC 37 ms
53,580 KB
testcase_11 AC 37 ms
53,580 KB
testcase_12 AC 37 ms
53,580 KB
testcase_13 AC 37 ms
53,580 KB
testcase_14 AC 36 ms
53,580 KB
testcase_15 AC 35 ms
53,580 KB
testcase_16 AC 220 ms
98,160 KB
testcase_17 AC 309 ms
113,652 KB
testcase_18 AC 226 ms
108,156 KB
testcase_19 AC 439 ms
117,088 KB
testcase_20 AC 445 ms
117,744 KB
testcase_21 AC 469 ms
118,000 KB
testcase_22 AC 339 ms
114,832 KB
testcase_23 AC 351 ms
113,208 KB
testcase_24 AC 348 ms
113,784 KB
testcase_25 AC 461 ms
117,252 KB
testcase_26 AC 457 ms
117,148 KB
testcase_27 AC 452 ms
117,128 KB
testcase_28 AC 441 ms
116,988 KB
testcase_29 AC 439 ms
117,132 KB
testcase_30 AC 434 ms
117,344 KB
testcase_31 AC 443 ms
116,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Unionfind:
     
    def __init__(self,n):
        self.uf = [-1]*n
 
    def find(self,x):
        if self.uf[x] < 0:
            return x
        else:
            self.uf[x] = self.find(self.uf[x])
            return self.uf[x]
 
    def same(self,x,y):
        return self.find(x) == self.find(y)
 
    def union(self,x,y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if self.uf[x] > self.uf[y]:
            x,y = y,x
        self.uf[x] += self.uf[y]
        self.uf[y] = x
        return True
 
    def size(self,x):
        x = self.find(x)
        return -self.uf[x]

n,m = map(int,input().split())
l = [set() for i in range(n+1)]
for i in range(n):
    b,c = map(int,input().split())
    l[c].add(b)

uf = Unionfind(m+1)
ans = 0
for i in range(1,n+1):
    if len(l[i]) < 2:
        continue
    now = list(l[i])
    for i,j in zip(now,now[1:]):
        if uf.same(i,j):
            continue
        uf.union(i,j)
        ans += 1
print(ans)
0