結果

問題 No.1767 BLUE to RED
ユーザー MasKoaTSMasKoaTS
提出日時 2022-01-06 17:19:48
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,552 bytes
コンパイル時間 289 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 149,632 KB
最終ジャッジ日時 2024-04-25 01:01:34
合計ジャッジ時間 7,035 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 130 ms
91,264 KB
testcase_01 AC 127 ms
86,272 KB
testcase_02 AC 130 ms
86,272 KB
testcase_03 AC 128 ms
86,400 KB
testcase_04 AC 138 ms
89,088 KB
testcase_05 AC 142 ms
89,728 KB
testcase_06 AC 135 ms
88,832 KB
testcase_07 AC 147 ms
89,600 KB
testcase_08 AC 162 ms
89,472 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import itertools as iter
import collections as coll
import heapq as hq
import bisect as bis
from decimal import Decimal as dec
from copy import deepcopy as dcopy
import math
import sys
sys.setrecursionlimit(10**6)
def input():
    return sys.stdin.readline().rstrip()
def getN():
	return int(sys.stdin.readline())
def getNs():
	return map(int,sys.stdin.readline().split())
def getList():
	return list(map(int,sys.stdin.readline().split()))
def strinps(n):
	return [sys.stdin.readline().rstrip() for _ in range(n)]
pi = 3.141592653589793
mod = 10**9+7
MOD = 998244353
INF = math.inf
dx = [1,0,-1,0];	dy = [0,1,0,-1]

"""
Union-Find
from : https://github.com/customaddone/beginPython/blob/master/cgi-bin/library/unionfind/unionfind.py
ref : https://algo-logic.info/union-find-tree/
"""

class UnionFind():
	#Uni = UnionFind(n) のようにする
	def __init__(self, n):
		self.n = n
		self.parents = [-1] * n

	#xの親(親がいないときは自身の番号を返す)
	def find(self, x):
		if self.parents[x] < 0:
			return x
		else:
			self.parents[x] = self.find(self.parents[x])
			return self.parents[x]

	#xとyを関係付ける
	def union(self, x, y):
		x = self.find(x)
		y = self.find(y)

		if x == y:
			return

		if self.parents[x] > self.parents[y]:
			x, y = y, x
		# if x > y: # よりrootのインデックスが小さい方が親
			# x, y = y, x

		self.parents[x] += self.parents[y]
		self.parents[y] = x

	#xとyが同じ組に属するかどうか
	def same(self, x, y):
		return self.find(x) == self.find(y)

	#xが属する組の大きさ
	def size(self, x):
		return -self.parents[self.find(x)]

	#xが属する組の全要素をリストとして取得
	def members(self, x):
		root = self.find(x)
		return [i for i in range(self.n) if self.find(i) == root]

	#Union-Find木の根に当たる要素全てをリストとして取得
	def roots(self):
		return [i for i, x in enumerate(self.parents) if x < 0]

	#Union-Find木の根とそれに属する組の全要素
	def all_group_members(self):
		return {r: self.members(r) for r in self.roots()}

"""
Main Code
"""

n,m = getNs()
a = [i - 1 for i in getNs()]
b = [i - 1 for i in getNs()]
rb = max(a[-1],b[-1]) + 1

uni = UnionFind(n + m + 1)
route = []
x = sorted(a + b)
st = set(a)
for i in range(n + m):
	if(x[i] in a):
		route.append((0, i, n + m))
for i in range(n + m - 1):
	route.append((x[i + 1] - x[i], i, i + 1))
route.sort()

ans = 0
for c,s,t in route:
	if(uni.size(0) == n + m + 1):
		break
	if(uni.same(s,t)):
		continue
	uni.union(s,t)
	ans += c
print(ans)
0