Submission #6498791


Source Code Expand

# require 'set'
N,M = gets.chomp.split(" ").map(&:to_i)
mat = []
M.times do
    mat << gets.chomp.split(" ").map(&:to_i)
end
S,T = gets.chomp.split(" ").map(&:to_i)

@graph = Array.new(N+1){[]}

# p [N,M]
# p mat
# p [S,T]

mat.each do |arr|
    u = arr[0]
    v = arr[1]
    @graph[u] << v
end
# p @graph
# (1..N).each do |n|

# next1 = []
=begin
@graph_2 = Array.new(N+1){Set.new}
(1..N).each do |n|
    @graph[n].each do |s1|
        @graph[s1].each do |s2|
            @graph[s2].each do |s3|
                if n != s3
                    @graph_2[n] << s3
                end
            end
        end
    end
end
=end
# p @graph_2
# next1.each do |n|
#     p n
# end
@visited = Array.new(N+1).map{Array.new(3)}
@q = []
@dist = Array.new(N+1).map{Array.new(3)}

def bfs(v)
    @visited[v][0] = true
    @dist[S][0]=0
    @q.push [v,0]

    while(node = @q.shift) do
        tmp,s = node
        # break if tmp == T and @visited[n][dist_tmp % 3]
        # p [tmp,s]
        break if tmp == T and s == 0

        nodes = @graph[tmp]
        nodes.each do |n|
            dist_tmp = @dist[tmp][s]
            # p [tmp,dist_tmp]
            if not @visited[n][(s+1) % 3]
                @visited[n][(s+1) % 3] = true
                @q.push [n, (s+1) % 3]
                @dist[n][(s+1) % 3] = dist_tmp + 1
            end
        end
    end
end

bfs(S)
# p @dist
# p @dist
# p @visited
puts @dist[T][0] ? @dist[T][0] / 3 : -1

Submission Info

Submission Time
Task E - Hopscotch Addict
User rk018
Language Ruby (2.3.3)
Score 500
Code Size 1506 Byte
Status AC
Exec Time 452 ms
Memory 29180 KiB

Judge Result

Set Name All Sample
Score / Max Score 500 / 500 0 / 0
Status
AC × 23
AC × 4
Set Name Test Cases
All cycle_01, cycle_02, cycle_03, killer_01, killer_02, killer_03, killer_04, long_01, long_02, random_dense_01, random_dense_02, random_max_01, random_max_02, random_max_03, random_max_04, random_max_05, random_max_06, sample_01, sample_02, sample_03, sample_04, tournament_01, tournament_02
Sample sample_01, sample_02, sample_03, sample_04
Case Name Status Exec Time Memory
cycle_01 AC 326 ms 27132 KiB
cycle_02 AC 390 ms 29180 KiB
cycle_03 AC 452 ms 28396 KiB
killer_01 AC 206 ms 10364 KiB
killer_02 AC 201 ms 10364 KiB
killer_03 AC 389 ms 28220 KiB
killer_04 AC 289 ms 18232 KiB
long_01 AC 221 ms 11388 KiB
long_02 AC 400 ms 28396 KiB
random_dense_01 AC 79 ms 5756 KiB
random_dense_02 AC 169 ms 10364 KiB
random_max_01 AC 343 ms 23332 KiB
random_max_02 AC 325 ms 23440 KiB
random_max_03 AC 275 ms 19052 KiB
random_max_04 AC 337 ms 22752 KiB
random_max_05 AC 227 ms 17916 KiB
random_max_06 AC 421 ms 28308 KiB
sample_01 AC 7 ms 1788 KiB
sample_02 AC 7 ms 1788 KiB
sample_03 AC 7 ms 1788 KiB
sample_04 AC 7 ms 1788 KiB
tournament_01 AC 218 ms 10492 KiB
tournament_02 AC 216 ms 10492 KiB