結果

問題 No.826 連絡網
ユーザー 双六双六
提出日時 2020-07-28 00:47:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 328 ms / 2,000 ms
コード長 1,329 bytes
コンパイル時間 1,718 ms
コンパイル使用メモリ 86,972 KB
実行使用メモリ 113,748 KB
最終ジャッジ日時 2023-09-11 06:14:34
合計ジャッジ時間 8,808 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 98 ms
71,564 KB
testcase_01 AC 98 ms
71,664 KB
testcase_02 AC 108 ms
76,532 KB
testcase_03 AC 139 ms
77,884 KB
testcase_04 AC 138 ms
77,924 KB
testcase_05 AC 130 ms
77,544 KB
testcase_06 AC 126 ms
77,340 KB
testcase_07 AC 135 ms
77,888 KB
testcase_08 AC 130 ms
78,276 KB
testcase_09 AC 133 ms
77,884 KB
testcase_10 AC 120 ms
77,224 KB
testcase_11 AC 134 ms
77,880 KB
testcase_12 AC 262 ms
104,228 KB
testcase_13 AC 172 ms
87,580 KB
testcase_14 AC 220 ms
97,176 KB
testcase_15 AC 143 ms
79,892 KB
testcase_16 AC 187 ms
90,292 KB
testcase_17 AC 177 ms
87,792 KB
testcase_18 AC 167 ms
85,372 KB
testcase_19 AC 293 ms
108,208 KB
testcase_20 AC 293 ms
107,604 KB
testcase_21 AC 136 ms
78,068 KB
testcase_22 AC 175 ms
87,932 KB
testcase_23 AC 190 ms
90,952 KB
testcase_24 AC 155 ms
83,040 KB
testcase_25 AC 328 ms
113,748 KB
testcase_26 AC 168 ms
84,716 KB
testcase_27 AC 272 ms
104,728 KB
testcase_28 AC 234 ms
98,240 KB
testcase_29 AC 173 ms
87,512 KB
testcase_30 AC 320 ms
113,740 KB
testcase_31 AC 180 ms
89,796 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.rank[x] < self.rank[y]:
			if self.same_check(x, y) != True:
				self.size[y] += self.size[x]
				self.size[x] = 0
			self.par[x] = y
		else:
			if self.same_check(x, y) != True:
				self.size[x] += self.size[y]
				self.size[y] = 0
			self.par[y] = x
			if self.rank[x] == self.rank[y]:
				self.rank[x] += 1

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



N, P = getlist()
UF = UnionFind(N + 1)

num = N + 1 #適当に値を入れる
L = [1 for i in range(num)]; L[0] = 0; L[1] = 0
plist = []
for i in range(num):
	if L[i] == 1:
		plist.append(i); c = 2
		while i * c <= num - 1:
			UF.union(i, i * c)
			L[i * c] = 0; c += 1


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

ans = UF.siz(P)
print(ans)
0