結果
問題 |
No.1420 国勢調査 (Easy)
|
ユーザー |
![]() |
提出日時 | 2021-12-11 15:28:20 |
言語 | Ruby (3.4.1) |
結果 |
AC
|
実行時間 | 1,057 ms / 2,000 ms |
コード長 | 708 bytes |
コンパイル時間 | 346 ms |
コンパイル使用メモリ | 7,424 KB |
実行使用メモリ | 45,696 KB |
最終ジャッジ日時 | 2024-07-19 20:32:22 |
合計ジャッジ時間 | 21,425 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 30 |
コンパイルメッセージ
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
ソースコード
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]