結果

問題 No.1390 Get together
ユーザー NoaSaberNoaSaber
提出日時 2021-03-09 10:14:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 354 ms / 2,000 ms
コード長 689 bytes
コンパイル時間 174 ms
コンパイル使用メモリ 82,136 KB
実行使用メモリ 102,656 KB
最終ジャッジ日時 2024-04-19 08:26:13
合計ジャッジ時間 7,529 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
52,480 KB
testcase_01 AC 36 ms
52,096 KB
testcase_02 AC 38 ms
52,352 KB
testcase_03 AC 86 ms
76,576 KB
testcase_04 AC 79 ms
76,928 KB
testcase_05 AC 80 ms
77,056 KB
testcase_06 AC 80 ms
76,784 KB
testcase_07 AC 76 ms
74,896 KB
testcase_08 AC 74 ms
75,160 KB
testcase_09 AC 85 ms
77,328 KB
testcase_10 AC 35 ms
52,224 KB
testcase_11 AC 36 ms
52,352 KB
testcase_12 AC 37 ms
52,224 KB
testcase_13 AC 36 ms
52,224 KB
testcase_14 AC 36 ms
52,336 KB
testcase_15 AC 35 ms
51,968 KB
testcase_16 AC 197 ms
95,276 KB
testcase_17 AC 295 ms
102,656 KB
testcase_18 AC 189 ms
95,392 KB
testcase_19 AC 349 ms
97,664 KB
testcase_20 AC 354 ms
97,336 KB
testcase_21 AC 352 ms
97,532 KB
testcase_22 AC 282 ms
97,064 KB
testcase_23 AC 294 ms
96,540 KB
testcase_24 AC 288 ms
96,816 KB
testcase_25 AC 350 ms
97,640 KB
testcase_26 AC 349 ms
97,440 KB
testcase_27 AC 349 ms
97,536 KB
testcase_28 AC 341 ms
97,280 KB
testcase_29 AC 350 ms
97,544 KB
testcase_30 AC 344 ms
97,512 KB
testcase_31 AC 341 ms
97,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N,M=map(int,input().split())
l=[[] for i in range(N)]
for i in range(N):
    b,c=map(int,input().split())
    b-=1
    c-=1
    l[c].append(b)
root=[i for i in range(M)]
height=[0]*M
def find(a):
    f=a
    if a==root[a]:
        return a
    while a!=root[a]:
        a=root[a]
    root[f]=a
    return a
def union(a,b):
    A=find(a)
    B=find(b)
    if A==B:
        return 0
    if height[A]>height[B]:
        root[B]=root[A]
        return 1
    else:
        root[A]=root[B]
        if height[A]==height[B]:
            height[B]+=1
        return 1
ans=0
for i in l:
    if len(i)<2:
        continue
    S=i[0]
    for j in range(1,len(i)):
        ans+=union(i[j],S)
print(ans)
0