結果

問題 No.1390 Get together
ユーザー ayaoniayaoni
提出日時 2021-02-12 21:28:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 424 ms / 2,000 ms
コード長 1,531 bytes
コンパイル時間 314 ms
コンパイル使用メモリ 86,844 KB
実行使用メモリ 104,408 KB
最終ジャッジ日時 2023-09-27 02:53:04
合計ジャッジ時間 8,948 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,256 KB
testcase_01 AC 74 ms
71,488 KB
testcase_02 AC 73 ms
71,212 KB
testcase_03 AC 109 ms
78,268 KB
testcase_04 AC 107 ms
78,312 KB
testcase_05 AC 107 ms
77,960 KB
testcase_06 AC 109 ms
78,244 KB
testcase_07 AC 101 ms
76,792 KB
testcase_08 AC 101 ms
76,776 KB
testcase_09 AC 111 ms
77,976 KB
testcase_10 AC 74 ms
71,360 KB
testcase_11 AC 74 ms
71,328 KB
testcase_12 AC 76 ms
71,324 KB
testcase_13 AC 76 ms
71,124 KB
testcase_14 AC 75 ms
71,312 KB
testcase_15 AC 74 ms
71,324 KB
testcase_16 AC 180 ms
94,640 KB
testcase_17 AC 291 ms
104,408 KB
testcase_18 AC 173 ms
95,684 KB
testcase_19 AC 404 ms
98,932 KB
testcase_20 AC 394 ms
99,212 KB
testcase_21 AC 424 ms
100,116 KB
testcase_22 AC 287 ms
98,096 KB
testcase_23 AC 302 ms
98,112 KB
testcase_24 AC 294 ms
98,148 KB
testcase_25 AC 411 ms
99,464 KB
testcase_26 AC 404 ms
99,392 KB
testcase_27 AC 401 ms
99,200 KB
testcase_28 AC 380 ms
99,072 KB
testcase_29 AC 376 ms
99,244 KB
testcase_30 AC 369 ms
98,744 KB
testcase_31 AC 382 ms
99,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def LS2(): return list(sys.stdin.readline().rstrip())


class UnionFind:
    def __init__(self,n):
        self.par = [i for i in range(n+1)]  # 親のノード番号
        self.rank = [0]*(n+1)

    def find(self,x):  # xの根のノード番号
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def same_check(self,x,y):  # x,yが同じグループか否か
        return self.find(x) == self.find(y)

    def unite(self,x,y):  # x,yの属するグループの併合
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            x,y = y,x
        if self.rank[x] == self.rank[y]:
            self.rank[x] += 1
        self.par[y] = x


N,M = MI()
UF = UnionFind(M)

color = [[] for _ in range(N+1)]
for _ in range(N):
    b,c = MI()
    color[c].append(b)

ans = 0
for i in range(1,N+1):
    X = color[i]
    if X:
        for j in range(len(X)-1):
            b0,b1 = X[j],X[j+1]
            if not UF.same_check(b0,b1):
                ans += 1
                UF.unite(b0,b1)

print(ans)
0