Submission #7268354


Source Code Expand

Copy
n = read_line.to_i
a = Array.new(n) { read_line.split.map { |v| v.to_i - 1 }.reverse }
ans = 0
update = Array.new(n) { |i| i }
time = Array.new(n, -1)
while !update.empty?
  n = [] of Int32
  update.each do |i|
    next if a[i].empty?
    next if time[i] == ans
    p = a[i][-1]
    next if time[p] == ans
    if a[p][-1] == i
      n << i
      n << p
      a[i].pop
      a[p].pop
      time[i] = time[p] = ans
    end
  end
  update = n
  ans += 1
end
puts a.all? { |p| p.empty? } ? ans - 1 : -1

Submission Info

Submission Time
Task E - League
User tomerun
Language Crystal (0.20.5)
Score 500
Code Size 523 Byte
Status AC
Exec Time 488 ms
Memory 13180 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 23
Set Name Test Cases
Sample a01, a02, a03
All a01, a02, a03, b04, b05, b06, b07, b08, b09, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23
Case Name Status Exec Time Memory
a01 AC 1 ms 636 KB
a02 AC 1 ms 636 KB
a03 AC 1 ms 636 KB
b04 AC 1 ms 636 KB
b05 AC 1 ms 636 KB
b06 AC 1 ms 636 KB
b07 AC 1 ms 636 KB
b08 AC 488 ms 10620 KB
b09 AC 303 ms 10236 KB
b10 AC 444 ms 12796 KB
b11 AC 434 ms 13180 KB
b12 AC 126 ms 4476 KB
b13 AC 105 ms 3196 KB
b14 AC 48 ms 2172 KB
b15 AC 39 ms 1788 KB
b16 AC 20 ms 1276 KB
b17 AC 15 ms 1020 KB
b18 AC 90 ms 2812 KB
b19 AC 74 ms 2556 KB
b20 AC 21 ms 1276 KB
b21 AC 19 ms 1020 KB
b22 AC 7 ms 892 KB
b23 AC 4 ms 764 KB