結果

問題 No.2301 Namorientation
ユーザー siman
提出日時 2023-05-16 14:41:51
言語 Ruby
(3.4.1)
結果
RE  
実行時間 -
コード長 1,241 bytes
コンパイル時間 255 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 89,664 KB
最終ジャッジ日時 2024-12-14 15:08:09
合計ジャッジ時間 30,428 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 27 RE * 3
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:37: warning: assigned but unused variable - f
Syntax OK

ソースコード

diff #

N = gets.to_i

O = Hash.new { |h, k| h[k] = {} }
E = Array.new(N + 1) { [] }
OE = []

N.times do
  a, b = gets.split.map(&:to_i)
  E[a] << b
  E[b] << a
  OE << [a, b]
end

def find_loop(v, par, path, visited)
  if visited[v]
    from = path.index(v)
    return [path[from..], true]
  end

  visited[v] = true
  path << v

  E[v].each do |u|
    next if u == par

    res, found = find_loop(u, v, path, visited)
    if found
      return [res, found]
    end
  end

  path.pop
end

path = []
visited = Array.new(N + 1, false)
loop_path, f = find_loop(1, nil, path, visited)
queue = []

visited = Array.new(N + 1, false)
n = loop_path.size
n.times do |i|
  a = loop_path[i]
  b = loop_path[(i + 1) % n]

  queue << a
  visited[a] = true
  O[a][b] = 1
end

until queue.empty?
  v = queue.shift

  E[v].each do |u|
    next if visited[u]
    visited[u] = true

    O[v][u] = 1
    queue << u
  end
end

1.upto(N) do |n|
  next if visited[n]
  queue << n

  until queue.empty?
    v = queue.shift

    E[v].each do |u|
      next if visited[u]
      visited[u] = true

      O[v][u] = 1
    end
  end
end

counter = Hash.new(0)

OE.each do |a, b|
  if O[a][b]
    counter[b] += 1
    puts '<-'
  else
    counter[a] += 1
    puts '->'
  end
end
0