結果

問題 No.1370 置換門松列
ユーザー simansiman
提出日時 2022-05-13 15:08:01
言語 Ruby
(3.3.0)
結果
AC  
実行時間 603 ms / 2,000 ms
コード長 714 bytes
コンパイル時間 544 ms
コンパイル使用メモリ 11,184 KB
実行使用メモリ 44,156 KB
最終ジャッジ日時 2023-10-12 21:07:41
合計ジャッジ時間 7,665 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
15,080 KB
testcase_01 AC 80 ms
15,156 KB
testcase_02 AC 79 ms
15,128 KB
testcase_03 AC 78 ms
15,268 KB
testcase_04 AC 79 ms
15,280 KB
testcase_05 AC 80 ms
15,052 KB
testcase_06 AC 80 ms
15,072 KB
testcase_07 AC 80 ms
15,248 KB
testcase_08 AC 80 ms
15,132 KB
testcase_09 AC 80 ms
15,088 KB
testcase_10 AC 79 ms
15,148 KB
testcase_11 AC 79 ms
15,124 KB
testcase_12 AC 79 ms
15,148 KB
testcase_13 AC 79 ms
15,056 KB
testcase_14 AC 79 ms
15,232 KB
testcase_15 AC 79 ms
15,248 KB
testcase_16 AC 79 ms
15,304 KB
testcase_17 AC 79 ms
15,048 KB
testcase_18 AC 79 ms
15,144 KB
testcase_19 AC 79 ms
15,328 KB
testcase_20 AC 79 ms
15,136 KB
testcase_21 AC 581 ms
44,156 KB
testcase_22 AC 268 ms
35,736 KB
testcase_23 AC 545 ms
36,816 KB
testcase_24 AC 169 ms
25,000 KB
testcase_25 AC 601 ms
43,892 KB
testcase_26 AC 603 ms
43,796 KB
testcase_27 AC 257 ms
31,348 KB
testcase_28 AC 259 ms
31,320 KB
testcase_29 AC 191 ms
27,820 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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