結果

問題 No.1390 Get together
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2021-02-12 21:33:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 226 ms / 2,000 ms
コード長 807 bytes
コンパイル時間 398 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 83,236 KB
最終ジャッジ日時 2024-07-19 20:28:58
合計ジャッジ時間 5,860 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

"""

"""

def uf_find(n,p):

    ufl = []

    while p[n] != n:
        ufl.append(n)
        n = p[n]

    for i in ufl:
        p[i] = n

    return n


def uf_union(a,b,p,rank):

    ap = uf_find(a,p)
    bp = uf_find(b,p)

    if ap == bp:
        return True
    else:

        if rank[ap] > rank[bp]:
            p[bp] = ap
        elif rank[ap] < rank[bp]:
            p[ap] = bp
        else:
            p[bp] = ap
            rank[ap] += 1

        return False

import sys
from sys import stdin

N,M = map(int,stdin.readline().split())

ctop = [None] * (N+1)

p = [i for i in range(M+1)]
rank = [0] * (M+1)
ans = 0

for i in range(N):

    b,c = map(int,stdin.readline().split())

    if ctop[c] == None:
        ctop[c] = b
    elif not uf_union(ctop[c],b,p,rank):
        ans += 1

print (ans)
0