結果

問題 No.1390 Get together
ユーザー c-yanc-yan
提出日時 2021-03-30 07:48:40
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 914 bytes
コンパイル時間 250 ms
コンパイル使用メモリ 10,952 KB
実行使用メモリ 60,636 KB
最終ジャッジ日時 2023-08-20 04:29:36
合計ジャッジ時間 20,096 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,852 KB
testcase_01 AC 16 ms
7,876 KB
testcase_02 AC 16 ms
7,792 KB
testcase_03 AC 31 ms
9,360 KB
testcase_04 AC 28 ms
9,016 KB
testcase_05 WA -
testcase_06 AC 28 ms
9,008 KB
testcase_07 AC 27 ms
9,096 KB
testcase_08 AC 27 ms
9,088 KB
testcase_09 AC 33 ms
9,308 KB
testcase_10 AC 16 ms
7,816 KB
testcase_11 WA -
testcase_12 AC 16 ms
7,820 KB
testcase_13 AC 16 ms
7,980 KB
testcase_14 AC 16 ms
7,792 KB
testcase_15 AC 16 ms
7,852 KB
testcase_16 WA -
testcase_17 AC 807 ms
58,572 KB
testcase_18 AC 768 ms
54,488 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 1,033 ms
58,392 KB
testcase_26 WA -
testcase_27 AC 1,042 ms
58,244 KB
testcase_28 AC 1,045 ms
58,428 KB
testcase_29 AC 1,037 ms
58,488 KB
testcase_30 WA -
testcase_31 AC 1,025 ms
58,116 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(lambda x: int(x) - 1, 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 = find(parent, color[c][0])
    for j in color[c][1:]:
        if i == find(parent, j):
            continue
        result += 1
        unite(parent, i, j)
print(result)
0