結果

問題 No.1390 Get together
ユーザー c-yanc-yan
提出日時 2021-03-30 08:18:07
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,119 ms / 2,000 ms
コード長 897 bytes
コンパイル時間 115 ms
コンパイル使用メモリ 10,932 KB
実行使用メモリ 61,776 KB
最終ジャッジ日時 2023-08-20 05:01:03
合計ジャッジ時間 16,525 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
7,768 KB
testcase_01 AC 18 ms
7,740 KB
testcase_02 AC 17 ms
7,844 KB
testcase_03 AC 30 ms
9,300 KB
testcase_04 AC 29 ms
8,996 KB
testcase_05 AC 29 ms
9,188 KB
testcase_06 AC 26 ms
8,996 KB
testcase_07 AC 26 ms
9,088 KB
testcase_08 AC 28 ms
8,944 KB
testcase_09 AC 31 ms
9,528 KB
testcase_10 AC 15 ms
7,728 KB
testcase_11 AC 16 ms
7,776 KB
testcase_12 AC 16 ms
7,696 KB
testcase_13 AC 16 ms
7,836 KB
testcase_14 AC 16 ms
7,776 KB
testcase_15 AC 16 ms
7,772 KB
testcase_16 AC 796 ms
40,300 KB
testcase_17 AC 699 ms
58,512 KB
testcase_18 AC 798 ms
54,296 KB
testcase_19 AC 1,116 ms
57,640 KB
testcase_20 AC 1,119 ms
43,832 KB
testcase_21 AC 914 ms
43,920 KB
testcase_22 AC 865 ms
61,776 KB
testcase_23 AC 858 ms
59,588 KB
testcase_24 AC 863 ms
60,532 KB
testcase_25 AC 886 ms
58,440 KB
testcase_26 AC 890 ms
58,412 KB
testcase_27 AC 900 ms
58,072 KB
testcase_28 AC 880 ms
58,368 KB
testcase_29 AC 875 ms
58,300 KB
testcase_30 AC 884 ms
58,508 KB
testcase_31 AC 893 ms
58,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import setrecursionlimit, stdin


def find(parent, i):
    t = parent[i]
    if t < 0:
        return i
    t = find(parent, t)
    parent[i] = t
    return t


def unite(parent, i, j):
    i = find(parent, i)
    j = find(parent, j)
    if i == j:
        return
    parent[j] += parent[i]
    parent[i] = j


readline = stdin.readline
setrecursionlimit(10 ** 6)

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

box = {}
color = {}
parent = [-1] * N
for i in range(N):
    b, c = map(int, readline().split())
    box.setdefault(b, [])
    box[b].append(i)
    color.setdefault(c, [])
    color[c].append(i)

for b in box:
    i = box[b][0]
    for j in box[b][1:]:
        unite(parent, i, j)

result = 0
for c in color:
    i = color[c][0]
    for j in color[c][1:]:
        if find(parent, i) == find(parent, j):
            continue
        result += 1
        unite(parent, i, j)
print(result)
0