結果

問題 No.1390 Get together
ユーザー titiatitia
提出日時 2021-02-12 21:43:11
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 755 ms / 2,000 ms
コード長 878 bytes
コンパイル時間 108 ms
コンパイル使用メモリ 10,884 KB
実行使用メモリ 42,220 KB
最終ジャッジ日時 2023-09-27 03:27:13
合計ジャッジ時間 13,553 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,948 KB
testcase_01 AC 17 ms
7,844 KB
testcase_02 AC 17 ms
7,856 KB
testcase_03 AC 30 ms
9,036 KB
testcase_04 AC 27 ms
8,844 KB
testcase_05 AC 27 ms
8,840 KB
testcase_06 AC 25 ms
8,816 KB
testcase_07 AC 25 ms
8,936 KB
testcase_08 AC 25 ms
8,848 KB
testcase_09 AC 29 ms
9,072 KB
testcase_10 AC 16 ms
7,920 KB
testcase_11 AC 16 ms
7,856 KB
testcase_12 AC 16 ms
7,936 KB
testcase_13 AC 16 ms
7,916 KB
testcase_14 AC 16 ms
7,860 KB
testcase_15 AC 16 ms
7,968 KB
testcase_16 AC 670 ms
39,508 KB
testcase_17 AC 594 ms
41,896 KB
testcase_18 AC 750 ms
39,644 KB
testcase_19 AC 745 ms
42,092 KB
testcase_20 AC 673 ms
42,064 KB
testcase_21 AC 684 ms
42,156 KB
testcase_22 AC 715 ms
41,312 KB
testcase_23 AC 721 ms
41,432 KB
testcase_24 AC 721 ms
41,396 KB
testcase_25 AC 727 ms
42,160 KB
testcase_26 AC 747 ms
42,168 KB
testcase_27 AC 729 ms
42,216 KB
testcase_28 AC 737 ms
42,220 KB
testcase_29 AC 741 ms
42,164 KB
testcase_30 AC 755 ms
42,124 KB
testcase_31 AC 747 ms
42,052 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N,M=map(int,input().split())

# UnionFind

Group = [i for i in range(M+1)] # グループ分け
Nodes = [1]*(M+1) # 各グループのノードの数

def find(x):
    while Group[x] != x:
        x=Group[x]
    return x

def Union(x,y):
    if find(x) != find(y):
        if Nodes[find(x)] < Nodes[find(y)]:
            
            Nodes[find(y)] += Nodes[find(x)]
            Nodes[find(x)] = 0
            Group[find(x)] = find(y)
            
        else:
            Nodes[find(x)] += Nodes[find(y)]
            Nodes[find(y)] = 0
            Group[find(y)] = find(x)

C=[[] for i in range(N+1)]
for i in range(N):
    b,c=map(int,input().split())
    C[c].append(b)

for i in range(N+1):
    for j in range(len(C[i])-1):
        Union(C[i][j],C[i][j+1])

ANS=0

for i in range(M+1):
    if i!=find(i):
        ANS+=1

print(ANS)


0