結果

問題 No.416 旅行会社
ユーザー 双六双六
提出日時 2020-08-10 04:01:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 604 ms / 4,000 ms
コード長 2,149 bytes
コンパイル時間 1,213 ms
コンパイル使用メモリ 86,848 KB
実行使用メモリ 123,060 KB
最終ジャッジ日時 2023-08-21 10:37:07
合計ジャッジ時間 9,350 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 257 ms
97,872 KB
testcase_01 AC 94 ms
71,584 KB
testcase_02 AC 95 ms
71,664 KB
testcase_03 AC 94 ms
71,636 KB
testcase_04 AC 95 ms
71,676 KB
testcase_05 AC 94 ms
71,584 KB
testcase_06 AC 99 ms
72,504 KB
testcase_07 AC 109 ms
76,828 KB
testcase_08 AC 151 ms
78,864 KB
testcase_09 AC 248 ms
81,916 KB
testcase_10 AC 268 ms
97,664 KB
testcase_11 AC 283 ms
97,964 KB
testcase_12 AC 296 ms
97,992 KB
testcase_13 AC 268 ms
98,132 KB
testcase_14 AC 604 ms
123,044 KB
testcase_15 AC 590 ms
122,788 KB
testcase_16 AC 573 ms
122,624 KB
testcase_17 AC 587 ms
122,888 KB
testcase_18 AC 599 ms
123,060 KB
testcase_19 AC 497 ms
103,004 KB
testcase_20 AC 529 ms
104,092 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.cnt = [INF] * (n + 1)
		self.cnt[0] = -1

	def ansfind(self, x):
		while True:
			if self.cnt[x] != INF:
				return self.cnt[x]
			else:
				x = self.par[x]

			if x == self.par[x] and self.cnt[x] == INF:
				return INF

	def find(self, x):
		while True:
			if self.par[x] == x:
				return x
			else:
				x = self.par[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, itr):
		x2 = self.find(x); y2 = self.find(y)
		root = self.find(0)
		if x2 != root and y2 != root:

			if self.rank[x2] < self.rank[y2]:
				self.par[x2] = y2
			else:
				self.par[y2] = x2
				if self.rank[x2] == self.rank[y2]:
					self.rank[x2] += 1
			return

		if y2 == root:
			x2, y2, x, y = y2, x2, y, x

		if self.rank[x2] < self.rank[y2]:
			self.par[x2] = y2
			self.cnt[y2] = itr
		else:
			self.par[y2] = x2
			self.cnt[y2] = itr
			if self.rank[x2] == self.rank[y2]:
				self.rank[x2] += 1
		return

#処理内容
def main():
	N, M, Q = getlist()
	UF = UnionFind(N)
	edge = defaultdict(int)
	for i in range(M):
		A, B = getlist()
		A -= 1; B -= 1
		edge[A * (10 ** 6) + B] = 1

	bridge = []
	for i in range(Q):
		C, D = getlist()
		C -= 1; D -= 1
		bridge.append([C, D])
		edge[C * (10 ** 6) + D] = 0

	for key, value in edge.items():
		if value == 1:
			a, b = int(key // (10 ** 6)), key % (10 ** 6)
			if not UF.same_check(a, b):
				UF.union(a, b, -1)
				# print(UF.par)

	for i in range(Q):
		c, d = bridge[-1 - i]
		if not UF.same_check(c, d):
			UF.union(c, d, i)

	# print(UF.cnt)
	# print(UF.par)

	for i in range(1, N):
		anspre = UF.ansfind(i)
		if anspre == INF:
			print(0)
		elif anspre == -1:
			print(-1)
		else:
			print(Q - anspre)


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