結果

問題 No.1477 Lamps on Graph
ユーザー yuruhiyayuruhiya
提出日時 2021-04-16 21:58:04
言語 Crystal
(1.11.2)
結果
AC  
実行時間 67 ms / 2,000 ms
コード長 5,428 bytes
コンパイル時間 18,941 ms
コンパイル使用メモリ 257,836 KB
実行使用メモリ 18,632 KB
最終ジャッジ日時 2023-09-16 00:34:21
合計ジャッジ時間 22,136 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,384 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,392 KB
testcase_03 AC 2 ms
4,436 KB
testcase_04 AC 3 ms
4,448 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 2 ms
4,416 KB
testcase_09 AC 2 ms
4,388 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 45 ms
11,192 KB
testcase_13 AC 37 ms
9,924 KB
testcase_14 AC 52 ms
11,188 KB
testcase_15 AC 28 ms
9,436 KB
testcase_16 AC 16 ms
7,628 KB
testcase_17 AC 14 ms
7,536 KB
testcase_18 AC 36 ms
15,396 KB
testcase_19 AC 34 ms
9,368 KB
testcase_20 AC 18 ms
7,916 KB
testcase_21 AC 27 ms
10,612 KB
testcase_22 AC 16 ms
7,428 KB
testcase_23 AC 36 ms
9,684 KB
testcase_24 AC 54 ms
13,536 KB
testcase_25 AC 25 ms
7,524 KB
testcase_26 AC 54 ms
15,312 KB
testcase_27 AC 31 ms
9,900 KB
testcase_28 AC 33 ms
9,264 KB
testcase_29 AC 33 ms
8,876 KB
testcase_30 AC 16 ms
8,336 KB
testcase_31 AC 16 ms
8,032 KB
testcase_32 AC 62 ms
18,632 KB
testcase_33 AC 61 ms
15,320 KB
testcase_34 AC 58 ms
15,456 KB
testcase_35 AC 66 ms
15,464 KB
testcase_36 AC 65 ms
15,624 KB
testcase_37 AC 61 ms
16,424 KB
testcase_38 AC 65 ms
15,016 KB
testcase_39 AC 67 ms
17,044 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# require "template"
lib C
  fun strtoll(s : UInt8*, p : UInt8**, b : Int32) : Int64
end

class String
  def to_i64
    C.strtoll(self, nil, 10)
  end
end

# require "graph"
struct Edge(T)
  property to : Int32
  property cost : T

  def initialize(@to : Int32, @cost : T)
  end
end

struct Edge2(T)
  property from : Int32
  property to : Int32
  property cost : T

  def initialize(@from : Int32, @to : Int32, @cost : T)
  end

  def reverse
    Edge2(T).new(to, from, cost)
  end
end

class Graph(T)
  getter size : Int32
  getter graph : Array(Array(Edge(T)))

  def initialize(@size : Int32)
    raise ArgumentError.new("Negative graph size: #{size}") unless size >= 0
    @graph = Array.new(size) { Array(Edge(T)).new }
  end

  def initialize(@size, edges : Array(Edge2(T)), undirected : Bool)
    raise ArgumentError.new("Negative graph size: #{size}") unless size >= 0
    @graph = Array.new(size) { Array(Edge(T)).new }
    edges.each do |edge|
      @graph[edge.from] << Edge.new(edge.to, edge.cost)
      @graph[edge.to] << Edge.new(edge.from, edge.cost) if undirected
    end
  end

  def add_edge(i : Int32, j : Int32, cost : T)
    raise IndexError.new unless 0 <= i < size
    raise IndexError.new unless 0 <= j < size
    graph[i] << Edge(T).new(j, cost)
    graph[j] << Edge(T).new(i, cost)
  end

  def add_edge_directed(i : Int32, j : Int32, cost : T)
    raise IndexError.new unless 0 <= i < size
    raise IndexError.new unless 0 <= j < size
    graph[i] << Edge(T).new(j, cost)
  end

  def [](i : Int32)
    graph[i]
  end

  def each_edge : Nil
    (0...size).each do |v|
      graph[v].each do |edge|
        yield Edge2(T).new(v, edge.to, edge.cost)
      end
    end
  end

  def edges : Array(Edge2(T))
    result = [] of Edge2(T)
    each_edge do |edge|
      result << edge
    end
    result
  end
end

# require "atcoder/PriorityQueue"
# ac-library.cr by hakatashi https://github.com/google/ac-library.cr
#
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module AtCoder
  # Implements standard priority queue like [std::priority_queue](https://en.cppreference.com/w/cpp/container/priority_queue).
  #
  # ```
  # q = AtCoder::PriorityQueue(Int64).new
  # q << 1_i64
  # q << 3_i64
  # q << 2_i64
  # q.pop # => 3
  # q.pop # => 2
  # q.pop # => 1
  # ```
  class PriorityQueue(T)
    getter heap : Array(T)

    def initialize
      initialize(&.itself)
    end

    # Initializes queue with the custom comperator.
    #
    # ```
    # q = AtCoder::PriorityQueue(Int64).new {|n| -n}
    # q << 1_i64
    # q << 3_i64
    # q << 2_i64
    # q.pop # => 1
    # q.pop # => 2
    # q.pop # => 3
    # ```
    def initialize(&block : T -> (Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64))
      @heap = Array(T).new
      @priority_proc = block
    end

    # Pushes value into the queue.
    def push(v : T)
      @heap << v
      index = @heap.size - 1
      while index != 0
        parent = (index - 1) // 2
        if @priority_proc.call(@heap[parent]) >= @priority_proc.call(@heap[index])
          break
        end
        @heap[parent], @heap[index] = @heap[index], @heap[parent]
        index = parent
      end
    end

    # Alias of `push`
    def <<(v : T)
      push(v)
    end

    # Pops value from the queue.
    def pop
      if @heap.size == 0
        return nil
      end
      if @heap.size == 1
        return @heap.pop
      end
      ret = @heap.first
      @heap[0] = @heap.pop
      index = 0
      while index * 2 + 1 < @heap.size
        child = if index * 2 + 2 < @heap.size && @priority_proc.call(@heap[index * 2 + 1]) < @priority_proc.call(@heap[index * 2 + 2])
          index * 2 + 2
        else
          index * 2 + 1
        end
        if @priority_proc.call(@heap[index]) >= @priority_proc.call(@heap[child])
          break
        end
        @heap[child], @heap[index] = @heap[index], @heap[child]
        index = child
      end
      ret
    end

    # Returns `true` if the queue is empty.
    delegate :empty?, to: @heap

    # Returns size of the queue.
    delegate :size, to: @heap
  end
end


n, m = read_line.split.map(&.to_i)
a = read_line.split.map(&.to_i)
edges = (0...m).flat_map {
  u, v = read_line.split.map(&.to_i.pred)
  e = Edge2(Nil).new(u, v, nil)
  [e, e.reverse]
}
k = read_line.to_i
b = read_line.split.map(&.to_i.pred)

graph = Graph.new(n, edges.select { |e| a[e.from] < a[e.to] }, undirected: false)

count_in = [0] * n
graph.each_edge do |e|
  count_in[e.to] += 1
end

flag = [false] * n
b.each { |i| flag[i] = true }

que = Deque.new((0...n).select { |i| count_in[i] == 0 })
ans = [] of Int32
while v = que.shift?
  op = flag[v]
  if op
    flag[v] = false
    ans << v
  end
  graph[v].each do |e|
    count_in[e.to] -= 1
    flag[e.to] = !flag[e.to] if op
    que << e.to if count_in[e.to] == 0
  end
end

if flag.none?
  puts ans.size, ans.join('\n', &.succ)
else
  puts -1
end
0