結果

問題 No.1605 Matrix Shape
ユーザー yuruhiyayuruhiya
提出日時 2021-07-17 11:56:49
言語 Crystal
(1.11.2)
結果
WA  
実行時間 -
コード長 8,982 bytes
コンパイル時間 13,327 ms
コンパイル使用メモリ 295,552 KB
実行使用メモリ 561,456 KB
最終ジャッジ日時 2024-07-06 23:27:45
合計ジャッジ時間 18,716 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
54,160 KB
testcase_01 AC 73 ms
54,016 KB
testcase_02 AC 71 ms
54,144 KB
testcase_03 AC 69 ms
54,016 KB
testcase_04 AC 70 ms
54,016 KB
testcase_05 AC 70 ms
54,016 KB
testcase_06 AC 72 ms
54,016 KB
testcase_07 AC 72 ms
54,144 KB
testcase_08 AC 73 ms
54,144 KB
testcase_09 AC 73 ms
54,144 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 71 ms
54,016 KB
testcase_13 AC 70 ms
54,016 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 72 ms
54,016 KB
testcase_18 WA -
testcase_19 AC 144 ms
38,016 KB
testcase_20 MLE -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

# require "/scanner"
class Scanner
  private def self.skip_to_not_space
    peek = STDIN.peek
    not_space = peek.index { |x| x != 32 && x != 10 } || peek.size
    STDIN.skip(not_space)
  end

  def self.c
    skip_to_not_space
    STDIN.read_char.not_nil!
  end

  def self.s
    skip_to_not_space

    peek = STDIN.peek
    if index = peek.index { |x| x == 32 || x == 10 }
      STDIN.skip(index + 1)
      return String.new(peek[0, index])
    end

    String.build do |buffer|
      loop do
        buffer.write peek
        STDIN.skip(peek.size)
        peek = STDIN.peek
        break if peek.empty?
        if index = peek.index { |x| x == 32 || x == 10 }
          buffer.write peek[0, index]
          STDIN.skip(index)
          break
        end
      end
    end
  end
end

macro internal_input(s, else_ast)
  {% if Scanner.class.has_method?(s.id) %}
    Scanner.{{s.id}}
  {% elsif s.stringify == "String" %}
    Scanner.s
  {% elsif s.stringify == "Char" %}
    Scanner.c
  {% elsif s.stringify =~ /[A-Z][a-z0-9_]*/ %}
    {{s.id}}.new(Scanner.s)
  {% elsif String.has_method?("to_#{s}".id) %}
    Scanner.s.to_{{s.id}}
  {% else %}
    {{else_ast}}
  {% end %}
end

macro internal_input_array(s, args, else_ast)
  {% if Scanner.class.has_method?(s.id) ||
          s.stringify =~ /[A-Z][a-z0-9_]*/ ||
          String.has_method?("to_#{s}".id) %}
    Array.new({{args.first}}) do
      {% if args.size == 1 %}
        input({{s.id}})
      {% else %}
        internal_input_array({{s}}, {{args[1...args.size]}}, else_ast)
      {% end %}
    end
  {% else %}
    {{else_ast}}
  {% end %}
end

macro input(s)
  {% if s.is_a?(Call) %}
    {% if s.receiver.is_a?(Nop) %}
      internal_input(
        {{s.name}}, {{s.name}}(
          {% for argument in s.args %}
            input({{argument}}),
          {% end %}
        )
      )
    {% elsif s.name.stringify == "[]" %}
      internal_input_array(
        {{s.receiver}}, {{s.args}}, {{s.receiver}}[
          {% for argument in s.args %}
            input({{argument}}),
          {% end %}
        ] {{s.block}}
      )
    {% else %}
      input({{s.receiver}}).{{s.name.id}}(
        {% for argument in s.args %}
          input({{argument}}),
        {% end %}
      ) {{s.block}}
    {% end %}
  {% else %}
    internal_input({{s.id}}, {{s.id}})
  {% end %}
end

macro input(*s)
  {
    {% for s in s %}
      input({{s}}),
    {% end %}
  }
end

# require "/graph"
struct Edge(T)
  include Comparable(Edge(T))

  property to : Int32, cost : T

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

  def <=>(other : Edge(T))
    {cost, to} <=> {other.cost, other.to}
  end

  def to_s(io) : Nil
    io << '(' << to << ", " << cost << ')'
  end

  def inspect(io) : Nil
    io << "->#{to}(#{cost})"
  end
end

struct Edge2(T)
  include Comparable(Edge2(T))

  property from : Int32, to : Int32, cost : T

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

  def <=>(other : Edge2(T))
    {cost, from, to} <=> {other.cost, other.from, other.to}
  end

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

  def sort
    Edge2(T).new(*{to, from}.minmax, cost)
  end

  def to_s(io) : Nil
    io << '(' << from << ", " << to << ", " << cost << ')'
  end

  def inspect(io) : Nil
    io << from << "->" << to << '(' << cost << ')'
  end
end

struct UnweightedEdge2
  property from : Int32, to : Int32

  def initialize(@from, @to)
  end

  def reverse
    UnweightedEdge2.new(to, from)
  end

  def sort
    UnweightedEdge2.new(*{to, from}.minmax)
  end

  def to_s(io) : Nil
    io << '(' << from << ", " << to << ')'
  end

  def inspect(io) : Nil
    io << from << "->" << to
  end
end

abstract class Graph(T)
  getter graph : Array(Array(Edge(T)))

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

  def add_edge(from : Int, to : Int, cost : T)
    add_edge(Edge2.new(from, to, cost))
  end

  def add_edge(from_to_cost : {Int32, Int32, T})
    add_edge(Edge2.new(*from_to_cost))
  end

  def add_edges(edges)
    edges.each { |edge| add_edge(edge) }
    self
  end

  delegate size, to: @graph
  delegate :[], to: @graph

  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
    result = [] of Edge2(T)
    each_edge do |edge|
      result << edge
    end
    result
  end

  def reverse
    result = self.class.new(size)
    each_edge do |edge|
      result.add_edge(edge.reverse)
    end
    result
  end
end

class DirectedGraph(T) < Graph(T)
  def initialize(size : Int)
    super
  end

  def initialize(size : Int, edges : Enumerable(Edge2(T)))
    super(size)
    add_edges(edges)
  end

  def initialize(size : Int, edges : Enumerable({Int32, Int32, T}))
    super(size)
    add_edges(edges)
  end

  def add_edge(edge : Edge2(T))
    raise IndexError.new unless 0 <= edge.from < size && 0 <= edge.to < size
    @graph[edge.from] << Edge.new(edge.to, edge.cost)
    self
  end
end

class UndirectedGraph(T) < Graph(T)
  def initialize(size : Int)
    super
  end

  def initialize(size : Int, edges : Enumerable(Edge2(T)))
    super(size)
    add_edges(edges)
  end

  def initialize(size : Int, edges : Enumerable({Int32, Int32, T}))
    super(size)
    add_edges(edges)
  end

  def add_edge(edge : Edge2(T))
    raise IndexError.new unless 0 <= edge.from < size && 0 <= edge.to < size
    @graph[edge.from] << Edge.new(edge.to, edge.cost)
    @graph[edge.to] << Edge.new(edge.from, edge.cost)
    self
  end
end

abstract class UnweightedGraph
  getter graph : Array(Array(Int32))

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

  def add_edge(from : Int, to : Int)
    add_edge(UnweightedEdge2.new(from, to))
  end

  def add_edge(from_to : {Int32, Int32})
    add_edge(*from_to)
  end

  def add_edges(edges)
    edges.each { |edge| add_edge(edge) }
    self
  end

  delegate size, to: @graph
  delegate :[], to: @graph

  def each_edge : Nil
    (0...size).each do |v|
      graph[v].each do |u|
        yield UnweightedEdge2.new(v, u)
      end
    end
  end

  def edges
    result = [] of UnweightedEdge2
    each_edge do |edge|
      result << edge
    end
    result
  end

  def reverse
    result = self.class.new(size)
    each_edge do |edge|
      result.add_edge(edge.reverse)
    end
    result
  end

  def indegree
    result = Array.new(size, 0)
    each_edge do |edge|
      result[edge.to] += 1
    end
    result
  end

  def outdegree
    result = Array.new(size, 0)
    each_edge do |edge|
      result[edge.from] += 1
    end
    result
  end
end

class UnweightedDirectedGraph < UnweightedGraph
  def initialize(size : Int)
    super
  end

  def initialize(size : Int, edges)
    super(size)
    add_edges(edges)
  end

  def add_edge(edge : UnweightedEdge2)
    raise IndexError.new unless 0 <= edge.from < size && 0 <= edge.to < size
    @graph[edge.from] << edge.to
    self
  end

  def to_undirected : self
    result = UnweightedDirectedGraph.new(size)
    each_edge do |edge|
      result.add_edge(edge)
      result.add_edge(edge.reverse)
    end
    result
  end
end

class UnweightedUndirectedGraph < UnweightedGraph
  def initialize(size : Int)
    super
  end

  def initialize(size : Int, edges)
    super(size)
    add_edges(edges)
  end

  def add_edge(edge : UnweightedEdge2)
    raise IndexError.new unless 0 <= edge.from < size && 0 <= edge.to < size
    @graph[edge.from] << edge.to
    @graph[edge.to] << edge.from
    self
  end

  def each_child(vertex : Int, parent, &block) : Nil
    graph[vertex].each do |u|
      yield u if u != parent
    end
  end

  def each_child(vertex : Int, parent)
    graph[vertex].each.select { |u| u != parent }
  end

  def to_undirected : self
    self
  end
end

# require "/graph/components"
# require "../graph"

class UnweightedGraph
  # Returns {components size, index, groups}
  def components
    graph = to_undirected
    index = Array(Int32?).new(size, nil)
    groups = [] of Set(Int32)
    id = 0
    size.times do |v|
      next if index[v]
      que = Deque{v}
      groups << Set(Int32).new
      while u = que.shift?
        index[u] = id
        groups[id] << u
        graph[u].each do |edge|
          if index[edge].nil?
            que << edge
          end
        end
      end
      id += 1
    end
    {id, index, groups}
  end
end

n = input(i)
graph = UnweightedDirectedGraph.new 2*10**5, Array.new(n) { input(i - 1, i - 1) }
m, index, groups = graph.components
indegree = graph.indegree
groups.select! { |groups|
  groups.any? { |v| indegree[v] > 0 }
}

if groups.size >= 2
  puts 0
else
  group = groups.first
  case group.count { |v| indegree[v] == 0 }
  when 0
    puts group.size
  when 1
    puts 1
  else
    puts 0
  end
end
0