結果

問題 No.583 鉄道同好会
ユーザー 双六双六
提出日時 2020-08-26 15:05:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 137 ms / 2,000 ms
コード長 1,572 bytes
コンパイル時間 280 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 76,800 KB
最終ジャッジ日時 2024-04-25 00:56:21
合計ジャッジ時間 3,230 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
53,504 KB
testcase_01 AC 48 ms
53,888 KB
testcase_02 AC 49 ms
54,016 KB
testcase_03 AC 47 ms
53,888 KB
testcase_04 AC 48 ms
54,272 KB
testcase_05 AC 47 ms
53,888 KB
testcase_06 AC 52 ms
54,016 KB
testcase_07 AC 47 ms
54,272 KB
testcase_08 AC 48 ms
53,888 KB
testcase_09 AC 47 ms
54,272 KB
testcase_10 AC 54 ms
59,648 KB
testcase_11 AC 92 ms
76,672 KB
testcase_12 AC 95 ms
76,288 KB
testcase_13 AC 100 ms
76,800 KB
testcase_14 AC 99 ms
76,416 KB
testcase_15 AC 104 ms
76,544 KB
testcase_16 AC 118 ms
76,672 KB
testcase_17 AC 125 ms
76,672 KB
testcase_18 AC 137 ms
76,544 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]

#処理内容
def main():
	N, M = getlist()
	UF = UnionFind(N)
	D = defaultdict(int)
	for i in range(M):
		a, b = getlist()
		UF.union(a, b)
		D[a] += 1; D[b] += 1

	total = len(D)

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

	judge = 0
	for i in range(N):
		if UF.size[i] == total:
			judge = 1

	# print(total)
	# print(judge)

	if judge == 0:
		print("NO")
		return

	# 一筆書き判定
	oddcount = 0
	for i in range(N):
		if UF.size[UF.par[i]] == total:
			if D[i] % 2 == 1:
				oddcount += 1

	if oddcount <= 2:
		print("YES")
	else:
		print("NO")

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