結果

問題 No.556 仁義なきサルたち
ユーザー 双六双六
提出日時 2020-08-28 10:03:05
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 194 ms / 2,000 ms
コード長 1,364 bytes
コンパイル時間 292 ms
コンパイル使用メモリ 87,148 KB
実行使用メモリ 79,688 KB
最終ジャッジ日時 2023-08-08 18:03:04
合計ジャッジ時間 3,829 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
71,416 KB
testcase_01 AC 86 ms
71,868 KB
testcase_02 AC 86 ms
71,560 KB
testcase_03 AC 91 ms
71,768 KB
testcase_04 AC 88 ms
71,664 KB
testcase_05 AC 90 ms
71,556 KB
testcase_06 AC 91 ms
71,484 KB
testcase_07 AC 95 ms
71,532 KB
testcase_08 AC 93 ms
72,080 KB
testcase_09 AC 97 ms
76,468 KB
testcase_10 AC 103 ms
77,196 KB
testcase_11 AC 104 ms
77,064 KB
testcase_12 AC 107 ms
77,040 KB
testcase_13 AC 111 ms
77,880 KB
testcase_14 AC 136 ms
78,428 KB
testcase_15 AC 150 ms
78,552 KB
testcase_16 AC 130 ms
78,960 KB
testcase_17 AC 161 ms
79,688 KB
testcase_18 AC 194 ms
79,296 KB
testcase_19 AC 116 ms
78,144 KB
testcase_20 AC 118 ms
78,404 KB
testcase_21 AC 119 ms
78,108 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys; input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
from collections import defaultdict
con = 10 ** 9 + 7; INF = float("inf")

def getlist():
	return list(map(int, input().split()))

class UnionFind:
	def __init__(self, n):
		self.par = [i for i in range(n + 1)]
		self.rank = [0] * (n + 1)
		self.size = [1] * (n + 1)

	def find(self, x):
		if self.par[x] == x:
			return x
		else:
			self.par[x] = self.find(self.par[x])
			return self.par[x]

	def same_check(self, x, y):
		return self.find(x) == self.find(y)

	def union(self, x, y):
		x = self.find(x); y = self.find(y)
		if self.same_check(x, y) != True:
			if self.size[x] > self.size[y]:
				self.size[x] += self.size[y]
				self.size[y] = 0
				self.par[y] = x
			elif self.size[x] < self.size[y]:
				self.size[y] += self.size[x]
				self.size[x] = 0
				self.par[x] = y
			else:
				if x < y:
					self.size[x] += self.size[y]
					self.size[y] = 0
					self.par[y] = x
				else:
					self.size[y] += self.size[x]
					self.size[x] = 0
					self.par[x] = y

	def siz(self, x):
		x = self.find(x)
		return self.size[x]

#処理内容
def main():
	N, M = getlist()
	UF = UnionFind(N)
	for i in range(M):
		a, b = getlist()
		a -= 1; b -= 1
		UF.union(a, b)

	for i in range(N):
		UF.par[i] = UF.find(i)

	for i in range(N):
		print(UF.par[i] + 1)

if __name__ == '__main__':
	main()
0