結果

問題 No.583 鉄道同好会
ユーザー TANIGUCHI KousukeTANIGUCHI Kousuke
提出日時 2021-02-16 21:18:06
言語 Ruby
(3.3.0)
結果
AC  
実行時間 459 ms / 2,000 ms
コード長 680 bytes
コンパイル時間 307 ms
コンパイル使用メモリ 11,336 KB
実行使用メモリ 25,464 KB
最終ジャッジ日時 2023-10-11 11:56:05
合計ジャッジ時間 5,471 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
15,176 KB
testcase_01 AC 85 ms
15,168 KB
testcase_02 AC 84 ms
15,152 KB
testcase_03 AC 85 ms
15,184 KB
testcase_04 AC 82 ms
15,100 KB
testcase_05 AC 83 ms
15,304 KB
testcase_06 AC 83 ms
15,344 KB
testcase_07 AC 84 ms
15,308 KB
testcase_08 AC 85 ms
15,044 KB
testcase_09 AC 85 ms
15,180 KB
testcase_10 AC 87 ms
15,356 KB
testcase_11 AC 159 ms
17,336 KB
testcase_12 AC 186 ms
18,608 KB
testcase_13 AC 183 ms
18,420 KB
testcase_14 AC 187 ms
18,592 KB
testcase_15 AC 225 ms
19,628 KB
testcase_16 AC 353 ms
23,076 KB
testcase_17 AC 404 ms
25,464 KB
testcase_18 AC 459 ms
25,340 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)

E = M.times.map{ gets.split.map(&:to_i) }
E.each do |a,b| 
  D[a] += 1
  D[b] += 1
  R.unite!(a,b)
end

s = E.flatten.min

o = N.times.count{|i| D[i].odd? }
if o == 2 || o == 0
  puts E.all?{|a,b| R.same?(s, a) } ? "YES" : "NO"
else
  puts "NO"
end
0