結果

問題 No.1390 Get together
ユーザー kojihashimoto-kobekojihashimoto-kobe
提出日時 2021-03-25 17:14:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 633 ms / 2,000 ms
コード長 989 bytes
コンパイル時間 481 ms
コンパイル使用メモリ 86,928 KB
実行使用メモリ 115,888 KB
最終ジャッジ日時 2023-08-18 01:00:16
合計ジャッジ時間 13,445 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
71,636 KB
testcase_01 AC 94 ms
71,656 KB
testcase_02 AC 94 ms
71,316 KB
testcase_03 AC 151 ms
79,140 KB
testcase_04 AC 149 ms
79,400 KB
testcase_05 AC 152 ms
79,032 KB
testcase_06 AC 146 ms
79,252 KB
testcase_07 AC 151 ms
79,224 KB
testcase_08 AC 148 ms
78,964 KB
testcase_09 AC 153 ms
79,108 KB
testcase_10 AC 97 ms
71,684 KB
testcase_11 AC 96 ms
71,380 KB
testcase_12 AC 94 ms
71,676 KB
testcase_13 AC 93 ms
71,468 KB
testcase_14 AC 94 ms
71,676 KB
testcase_15 AC 93 ms
71,300 KB
testcase_16 AC 400 ms
112,088 KB
testcase_17 AC 420 ms
113,544 KB
testcase_18 AC 376 ms
115,072 KB
testcase_19 AC 595 ms
113,336 KB
testcase_20 AC 576 ms
110,084 KB
testcase_21 AC 591 ms
110,624 KB
testcase_22 AC 534 ms
115,888 KB
testcase_23 AC 544 ms
114,932 KB
testcase_24 AC 548 ms
114,640 KB
testcase_25 AC 612 ms
113,880 KB
testcase_26 AC 633 ms
113,660 KB
testcase_27 AC 632 ms
114,044 KB
testcase_28 AC 601 ms
114,116 KB
testcase_29 AC 617 ms
114,160 KB
testcase_30 AC 597 ms
114,008 KB
testcase_31 AC 608 ms
113,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n, m = map(int, input().split())

from collections import deque

#色iのスライムがどの箱に入っているかを管理
color = [[] for _ in range(n+1)]
#箱iにどの色のスライムが入っているかを管理 
box = [[] for _ in range(m+1)]

for i in range(n):
  b, c = map(int, input().split())
  box[b].append(c)
  color[c].append(b)

#アクセス済みの箱を管理
distb = [-1] * (m+1)
distb[0] = 0

#アクセス済みの色を管理
distc = [-1] * (n+1)
distc[0] = 0

ans = 0

for i in range(m+1):
  if distb[i] == -1:
    distb[i] = 0
    d = deque()
    d.append(i)
    while d:
      v = d.popleft()
      for j in box[v]:
        if distc[j] != -1:
          continue
        distc[j] = 0 #距離はどうでもいいので、0とかにしちゃう
        for k in color[j]:
          if distb[k] != -1:
            continue
          distb[k] = 0 #距離はどうでもいいので、0とかにしちゃう
          d.append(k)
          ans += 1

print(ans)
0