結果

問題 No.583 鉄道同好会
ユーザー TANIGUCHI KousukeTANIGUCHI Kousuke
提出日時 2021-02-16 21:27:09
言語 Ruby
(3.3.0)
結果
AC  
実行時間 284 ms / 2,000 ms
コード長 694 bytes
コンパイル時間 335 ms
コンパイル使用メモリ 11,280 KB
実行使用メモリ 15,440 KB
最終ジャッジ日時 2023-10-11 12:05:28
合計ジャッジ時間 3,979 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
15,316 KB
testcase_01 AC 84 ms
15,168 KB
testcase_02 AC 85 ms
15,132 KB
testcase_03 AC 83 ms
15,208 KB
testcase_04 AC 85 ms
15,088 KB
testcase_05 AC 83 ms
15,108 KB
testcase_06 AC 83 ms
15,304 KB
testcase_07 AC 85 ms
15,312 KB
testcase_08 AC 84 ms
15,172 KB
testcase_09 AC 83 ms
15,136 KB
testcase_10 AC 86 ms
15,268 KB
testcase_11 AC 131 ms
15,388 KB
testcase_12 AC 151 ms
15,132 KB
testcase_13 AC 152 ms
15,396 KB
testcase_14 AC 152 ms
15,432 KB
testcase_15 AC 173 ms
15,256 KB
testcase_16 AC 249 ms
15,440 KB
testcase_17 AC 284 ms
15,332 KB
testcase_18 AC 280 ms
15,228 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class UnionFind
  def initialize(n); @data = Array.new(n, &:itself); end
  def root(i); _compact(i, _root(i)); end
  def unite!(i,j); _compact(i, root(j)); end
  def same?(i,j); root(i) == root(j); end
    
  def _root(i); i = @data[i] until @data[i] == i; return i; end
  def _compact(i, r); @data[i], i = r, @data[i] until @data[i] == r; return r; end
end

N, M = gets.split.map(&:to_i)
D = Array.new(N, 0)
R = UnionFind.new(N)
M.times do
  a, b = gets.split.map(&:to_i)
  D[a] += 1
  D[b] += 1
  R.unite!(a,b)
end

s = D.index{|v| v > 0 }
o = N.times.count{|i| D[i].odd? }
if o == 2 || o == 0
  puts D.each_with_index.all?{|c,u| c.zero? ^ R.same?(s, u) } ? "YES" : "NO"
else
  puts "NO"
end
0