AtCoder Typical Contest 001

Submission #7469261

Source codeソースコード

import sys
def input():
	return sys.stdin.readline().strip()

N, Q = list(map(int, input().split()))


class Union_Find():
	def __init__(self, n):
		self.union = [i for i in range(n)]
		self.level = [0 for i in range(n)]

	def root(self, i, mode=0):
		keiro = [i]
		ans = i
		while ans != self.union[ans]:
			ans = self.union[ans]
			keiro.append(ans)
		if mode == 0:
			return ans
		else:
			return ans, keiro

	def unite(self, i, j):
		root_i, list_i = self.root(i, 1)
		root_j, list_j = self.root(j, 1)
		if root_i != root_j:
			if self.level[root_i] < self.level[root_j]:
				self.level[root_j] = max(self.level[root_i] + 1, self.level[root_j])
				for node in list_i:
					self.union[node] = root_j
			else:
				self.level[root_i] = max(self.level[root_j] + 1, self.level[root_i])
				for node in list_j:
					self.union[node] = root_i

UF = Union_Find(N)

for q in range(Q):
	P, A, B = list(map(int, input().split()))
	if P == 0:
		UF.unite(A, B)

	else:
		if UF.root(A) == UF.root(B):
			print("Yes")
		else:
			print("No")

Submission

Task問題 B - Union Find
User nameユーザ名 Awaful
Created time投稿日時
Language言語 PyPy3 (2.4.0)
Status状態 AC
Score得点 100
Source lengthソースコード長 1079 Byte
File nameファイル名
Exec time実行時間 606 ms
Memory usageメモリ使用量 60464 KB

Test case

Set

Set name Score得点 / Max score Cases
Sample - 00_sample_01.txt
All 100 / 100 00_sample_01.txt,subtask_01_01.txt,subtask_01_02.txt,subtask_01_03.txt,subtask_01_04.txt,subtask_01_05.txt,subtask_01_06.txt,subtask_01_07.txt,subtask_01_08.txt,subtask_01_09.txt,subtask_01_10.txt,subtask_01_11.txt,subtask_01_12.txt,subtask_01_13.txt,subtask_01_14.txt,subtask_01_15.txt,subtask_01_16.txt,subtask_01_17.txt,subtask_01_18.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
00_sample_01.txt AC 167 ms 38256 KB
subtask_01_01.txt AC 398 ms 52824 KB
subtask_01_02.txt AC 171 ms 45492 KB
subtask_01_03.txt AC 464 ms 53976 KB
subtask_01_04.txt AC 496 ms 60208 KB
subtask_01_05.txt AC 246 ms 44380 KB
subtask_01_06.txt AC 277 ms 47408 KB
subtask_01_07.txt AC 521 ms 58200 KB
subtask_01_08.txt AC 500 ms 60208 KB
subtask_01_09.txt AC 164 ms 38256 KB
subtask_01_10.txt AC 172 ms 45492 KB
subtask_01_11.txt AC 476 ms 56920 KB
subtask_01_12.txt AC 525 ms 60208 KB
subtask_01_13.txt AC 432 ms 55000 KB
subtask_01_14.txt AC 185 ms 45748 KB
subtask_01_15.txt AC 502 ms 58072 KB
subtask_01_16.txt AC 506 ms 60336 KB
subtask_01_17.txt AC 582 ms 60336 KB
subtask_01_18.txt AC 606 ms 60464 KB