結果

問題 No.1370 置換門松列
ユーザー simansiman
提出日時 2022-05-13 15:08:01
言語 Ruby
(3.4.1)
結果
AC  
実行時間 756 ms / 2,000 ms
コード長 714 bytes
コンパイル時間 624 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 35,196 KB
最終ジャッジ日時 2024-09-14 19:27:13
合計ジャッジ時間 8,880 ms
ジャッジサーバーID
(参考情報)
judge6 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 25
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:48: warning: assigned but unused variable - e
Syntax OK

ソースコード

diff #

require 'tsort'

class Hash
  include TSort
  alias tsort_each_node each_key
  def tsort_each_child(node, &block)
    fetch(node).each(&block)
  end
end

N, M = gets.split.map(&:to_i)
A = gets.split.map(&:to_i)
E = Hash.new { |h, k| h[k] = [] }

A.each_cons(3) do |a, _, c|
  if a == c
    puts "No"
    exit
  end
end

A.each_cons(2).with_index do |(u, v), i|
  if u == v
    puts "No"
    exit
  end

  if i.even?
    E[u] << v
  else
    E[v] << u
  end
end

1.upto(M) do |i|
  E[i] = E[i].uniq
end

ans = Array.new(M, -1)

begin
  E.tsort.each.with_index(1) do |u, a|
    ans[u - 1] = a
  end

  puts "Yes"
  puts ans.join(" ")
rescue SystemStackError =>e
  puts "No"
rescue TSort::Cyclic => e
  puts "No"
end
0