結果

問題 No.1420 国勢調査 (Easy)
ユーザー simansiman
提出日時 2021-12-11 15:28:20
言語 Ruby
(3.3.0)
結果
AC  
実行時間 840 ms / 2,000 ms
コード長 708 bytes
コンパイル時間 511 ms
コンパイル使用メモリ 11,172 KB
実行使用メモリ 52,436 KB
最終ジャッジ日時 2023-09-27 03:09:26
合計ジャッジ時間 20,594 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
15,232 KB
testcase_01 AC 70 ms
15,112 KB
testcase_02 AC 451 ms
38,408 KB
testcase_03 AC 458 ms
38,876 KB
testcase_04 AC 453 ms
39,064 KB
testcase_05 AC 461 ms
38,796 KB
testcase_06 AC 458 ms
38,892 KB
testcase_07 AC 426 ms
35,816 KB
testcase_08 AC 441 ms
37,680 KB
testcase_09 AC 399 ms
33,852 KB
testcase_10 AC 414 ms
33,496 KB
testcase_11 AC 451 ms
35,992 KB
testcase_12 AC 105 ms
17,952 KB
testcase_13 AC 301 ms
31,612 KB
testcase_14 AC 110 ms
19,324 KB
testcase_15 AC 291 ms
31,748 KB
testcase_16 AC 293 ms
31,652 KB
testcase_17 AC 286 ms
31,732 KB
testcase_18 AC 291 ms
31,548 KB
testcase_19 AC 293 ms
31,684 KB
testcase_20 AC 283 ms
31,768 KB
testcase_21 AC 282 ms
31,524 KB
testcase_22 AC 761 ms
52,036 KB
testcase_23 AC 752 ms
51,784 KB
testcase_24 AC 821 ms
52,256 KB
testcase_25 AC 809 ms
51,908 KB
testcase_26 AC 840 ms
52,436 KB
testcase_27 AC 332 ms
34,620 KB
testcase_28 AC 563 ms
46,740 KB
testcase_29 AC 190 ms
27,544 KB
testcase_30 AC 398 ms
41,292 KB
testcase_31 AC 393 ms
41,264 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:13: warning: ambiguous first argument; put parentheses or a space even after `-' operator
Main.rb:41: warning: ambiguous first argument; put parentheses or a space even after `-' operator
Syntax OK

ソースコード

diff #

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

M.times do
  a, b = gets.split.map(&:to_i)
  y = gets.to_i

  E[a] << b
  E[b] << a

  if C[a][b] && C[a][b] != y
    puts -1
    exit
  end

  C[a][b] = C[b][a] = y
end

ans = Array.new(N + 1, -1)
visited = Array.new(N + 1, false)

1.upto(N) do |i|
  next if visited[i]

  queue = []
  queue << [i, 0]

  until queue.empty?
    u, x = queue.shift

    next if visited[u]
    visited[u] = true
    ans[u] = x

    E[u].each do |v|
      c = C[u][v]
      nc = c ^ x

      if ans[v] != -1 && ans[v] != nc
        puts -1
        exit
      end

      queue << [v, nc]
    end
  end
end

puts ans[1..-1]
0