結果

問題 No.583 鉄道同好会
ユーザー ciel
提出日時 2017-10-29 17:40:14
言語 Ruby
(3.4.1)
結果
AC  
実行時間 412 ms / 2,000 ms
コード長 459 bytes
コンパイル時間 152 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 18,304 KB
最終ジャッジ日時 2024-11-22 05:21:19
合計ジャッジ時間 4,173 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:14: warning: assigned but unused variable - n
Syntax OK

ソースコード

diff #

#!/usr/bin/ruby
class UnionFind < Hash
	def root(a)
		self[a]=a if !has_key?(a)
		return self[a]==a ? a : (self[a]=root(self[a]))
	end
	def unite(a,b)
		x=root(a)
		y=root(b)
		self[x]=y
	end
end

n,m=gets.split.map(&:to_i)
deg=Hash.new 0
uf=UnionFind.new
m.times{
	a,b=gets.split.map(&:to_i)
	deg[a]^=1
	deg[b]^=1
	uf.unite(a,b)
}
puts uf.keys.all?{|k|uf.root(k)==uf.root(uf.each_key.next)}&&deg.values.all?{|e|e<=1}&&deg.values.count{|e|e==1}<3 ? :YES : :NO
0