結果

問題 No.1605 Matrix Shape
ユーザー yuruhiyayuruhiya
提出日時 2021-07-17 12:18:24
言語 Crystal
(1.11.2)
結果
AC  
実行時間 261 ms / 2,000 ms
コード長 9,896 bytes
コンパイル時間 21,759 ms
コンパイル使用メモリ 257,048 KB
実行使用メモリ 29,624 KB
最終ジャッジ日時 2023-09-21 05:32:38
合計ジャッジ時間 26,839 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,380 KB
testcase_01 AC 3 ms
4,436 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 2 ms
4,472 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 3 ms
4,388 KB
testcase_08 AC 3 ms
4,416 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 2 ms
4,388 KB
testcase_11 AC 3 ms
4,384 KB
testcase_12 AC 2 ms
4,392 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 2 ms
4,384 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 261 ms
29,432 KB
testcase_20 AC 212 ms
19,568 KB
testcase_21 AC 214 ms
19,452 KB
testcase_22 AC 249 ms
25,668 KB
testcase_23 AC 256 ms
29,364 KB
testcase_24 AC 258 ms
29,624 KB
testcase_25 AC 86 ms
11,140 KB
testcase_26 AC 88 ms
11,088 KB
testcase_27 AC 86 ms
11,144 KB
testcase_28 AC 84 ms
11,116 KB
testcase_29 AC 86 ms
11,156 KB
testcase_30 AC 208 ms
19,444 KB
testcase_31 AC 209 ms
19,524 KB
testcase_32 AC 166 ms
14,236 KB
testcase_33 AC 163 ms
13,956 KB
testcase_34 AC 47 ms
11,480 KB
testcase_35 AC 185 ms
23,132 KB
testcase_36 AC 113 ms
16,164 KB
権限があれば一括ダウンロードができます

ソースコード

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

# require "/array/compress"
class Array(T)
  def compress(values : Array(T))
    map do |x|
      values.bsearch_index { |y| y >= x }.not_nil!
    end
  end

  def compress : Array(Int32)
    compress(uniq.sort!)
  end
end

# require "/datastructure/union_find"
class UnionFind
  @d : Array(Int32)

  def initialize(n : Int32)
    @d = Array.new(n, -1)
  end

  def root(x : Int32)
    @d[x] < 0 ? x : (@d[x] = root(@d[x]))
  end

  def unite(x : Int32, y : Int32)
    x = root(x)
    y = root(y)
    return false if x == y
    x, y = y, x if @d[x] > @d[y]
    @d[x] += @d[y]
    @d[y] = x
    true
  end

  def same?(x : Int32, y : Int32)
    root(x) == root(y)
  end

  def size(x : Int32)
    -@d[root(x)]
  end

  def groups
    groups = Hash(Int32, Set(Int32)).new { |h, k| h[k] = Set(Int32).new }
    @d.size.times do |i|
      groups[root(i)] << i
    end
    groups.values.to_set
  end
end

n = input(i)
vertex = input(i[n * 2]).compress
m = vertex.max + 1
uf = UnionFind.new(m)
graph = UnweightedDirectedGraph.new(m)
vertex.each_slice(2) do |(u, v)|
  graph.add_edge(u, v)
  uf.unite(u, v)
end

if uf.size(0) != m
  puts 0
  exit
end

degree = graph.indegree.zip(graph.outdegree).map { |x, y| x - y }
if degree.all?(0)
  puts m
elsif degree.tally == {-1 => 1, 1 => 1, 0 => m - 2}
  puts 1
else
  puts 0
end
0