結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー グミグミ
提出日時 2023-08-12 14:17:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 284 ms / 2,000 ms
コード長 659 bytes
コンパイル時間 272 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 105,216 KB
最終ジャッジ日時 2024-04-30 05:34:11
合計ジャッジ時間 5,076 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,760 KB
testcase_01 AC 39 ms
53,504 KB
testcase_02 AC 39 ms
53,760 KB
testcase_03 AC 232 ms
96,096 KB
testcase_04 AC 120 ms
101,120 KB
testcase_05 AC 220 ms
80,836 KB
testcase_06 AC 180 ms
97,252 KB
testcase_07 AC 223 ms
79,348 KB
testcase_08 AC 185 ms
105,216 KB
testcase_09 AC 265 ms
81,920 KB
testcase_10 AC 247 ms
94,336 KB
testcase_11 AC 219 ms
92,672 KB
testcase_12 AC 174 ms
91,648 KB
testcase_13 AC 150 ms
80,896 KB
testcase_14 AC 180 ms
86,272 KB
testcase_15 AC 159 ms
77,824 KB
testcase_16 AC 284 ms
85,632 KB
testcase_17 AC 153 ms
77,312 KB
testcase_18 AC 161 ms
94,592 KB
testcase_19 AC 109 ms
98,116 KB
testcase_20 AC 154 ms
77,056 KB
testcase_21 AC 159 ms
82,816 KB
testcase_22 AC 155 ms
77,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import Counter
class UnionFind():
	def __init__(self,n):
		self.parents = list(range(n))

	def find(self, x):
		if self.parents[x] == x:
			return x
		else:
			self.parents[x] = self.find(self.parents[x])
			return self.parents[x]
	
	def union(self, x, y):
		px = self.find(x)
		py = self.find(y)
		if px != py:
			self.parents[px] = py

def main():
	n,m=map(int,input().split())
	uf = UnionFind(2*n+1)
	for i in range(m):
		a,b=map(int,input().split())
		uf.union(a,b)
	p = [uf.find(x) for x in range(2*n+1)]
	c = Counter(p[1:])
	ans = 0
	for v in c.values():
		if v % 2 == 1:
			ans += 1
	
	print(ans//2)

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