結果

問題 No.1390 Get together
コンテスト
ユーザー kojihashimoto-kobe
提出日時 2021-11-25 10:46:08
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 381 ms / 2,000 ms
コード長 667 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 374 ms
コンパイル使用メモリ 85,692 KB
実行使用メモリ 120,596 KB
最終ジャッジ日時 2026-03-16 10:02:28
合計ジャッジ時間 8,851 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

boxused = [-1]*(m+1)
boxused[0]= 0
colorused = [-1]*(n+1)
colorused[0] = 0

from collections import deque

cnt = 0
for i in range(m+1):
  if boxused[i] == -1:
    d = deque()
    d.append(i)
    boxused[i] = 0
    while d:
      v = d.popleft()
      for x in box[v]:
        if colorused[x] == -1:
          colorused[x] = 0
          for y in color[x]:
            if boxused[y] == -1:
              boxused[y] = 0
              d.append(y)
              cnt += 1

print(cnt)
0