結果

問題 No.1582 Vertexes vs Edges
ユーザー yuruhiyayuruhiya
提出日時 2021-07-03 09:47:41
言語 Crystal
(1.11.2)
結果
AC  
実行時間 70 ms / 2,000 ms
コード長 6,448 bytes
コンパイル時間 17,211 ms
コンパイル使用メモリ 256,084 KB
実行使用メモリ 17,044 KB
最終ジャッジ日時 2023-09-12 14:41:31
合計ジャッジ時間 20,012 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,460 KB
testcase_01 AC 3 ms
4,420 KB
testcase_02 AC 3 ms
4,420 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 5 ms
5,168 KB
testcase_05 AC 51 ms
16,056 KB
testcase_06 AC 5 ms
5,336 KB
testcase_07 AC 9 ms
6,300 KB
testcase_08 AC 26 ms
9,920 KB
testcase_09 AC 47 ms
15,416 KB
testcase_10 AC 32 ms
10,416 KB
testcase_11 AC 6 ms
5,468 KB
testcase_12 AC 22 ms
9,248 KB
testcase_13 AC 37 ms
10,544 KB
testcase_14 AC 37 ms
10,564 KB
testcase_15 AC 50 ms
14,840 KB
testcase_16 AC 26 ms
9,424 KB
testcase_17 AC 36 ms
10,472 KB
testcase_18 AC 59 ms
16,452 KB
testcase_19 AC 37 ms
10,836 KB
testcase_20 AC 34 ms
10,284 KB
testcase_21 AC 30 ms
10,000 KB
testcase_22 AC 53 ms
15,508 KB
testcase_23 AC 21 ms
8,932 KB
testcase_24 AC 12 ms
7,016 KB
testcase_25 AC 30 ms
9,720 KB
testcase_26 AC 23 ms
9,228 KB
testcase_27 AC 50 ms
14,604 KB
testcase_28 AC 17 ms
7,980 KB
testcase_29 AC 43 ms
13,568 KB
testcase_30 AC 28 ms
9,608 KB
testcase_31 AC 44 ms
13,680 KB
testcase_32 AC 20 ms
8,144 KB
testcase_33 AC 69 ms
16,912 KB
testcase_34 AC 67 ms
17,044 KB
testcase_35 AC 70 ms
16,876 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,508 KB
testcase_38 AC 2 ms
4,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# require "/Scanner"
require "io/error"

class Scanner
  def self.s
    peek = STDIN.peek
    if not_space = peek.index { |x| x != 32 && x != 10 }
      if index = peek.index(not_space) { |x| x == 32 || x == 10 }
        result = String.new(peek[not_space...index])
        STDIN.skip(index + 1)
        result
      else
        result = String.new(peek[not_space..])
        STDIN.skip_to_end
        result
      end
    else
      raise IO::EOFError.new
    end
  end

  def self.i
    s.to_i
  end
end

macro input_array(type, args)
  Array.new({{args.first}}) do
    {% if args.size == 1 %}
      input({{type.id}})
    {% else %}
      input_array({{type}}, {{args[1...args.size]}})
    {% end %}
  end
end

macro input(type)
  {% if type.is_a?(Path) %}
    {{type}}.new(Scanner.s)
  {% elsif type.is_a?(Var) %}
    {% if Scanner.methods.includes?(type.id) %}
      Scanner.{{type.id}}
    {% else %}
      Scanner.s.to_{{type.id}}
    {% end %}
  {% elsif type.is_a?(Call) && type.args.size == 0 %}
    {% if Scanner.methods.includes?(type) %}
      Scanner.{{type.id}}
    {% else %}
      Scanner.s.to_{{type.id}}
    {% end %}
  {% elsif type.name == "[]" %}
    input_array("{{type.receiver}}", {{type.args}})
  {% else %}
    input_array("{{type.name}}", {{type.args}})
  {% end %}
end

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

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

  property to : Int32
  property 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
  property to : Int32
  property 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 to_s(io) : Nil
    io << '(' << from << ", " << to << ", " << cost << ')'
  end

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

struct UnweightedEdge2
  property from : Int32
  property to : Int32

  def initialize(@from, @to)
  end

  def reverse
    UnweightedEdge2.new(to, from)
  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(i : Int32, j : Int32, cost : T)
    add_edge(Edge2.new(i, j, cost))
  end

  def add_edges(edges : Array(Edge2(T)))
    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
end

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

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

  def add_edge(edge : Edge2(T))
    raise IndexError.new unless 0 <= edge.from < size
    raise IndexError.new unless 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 : Array(Edge2(T)))
    super(size)
    add_edges(edges)
  end

  def add_edge(edge : Edge2(T))
    raise IndexError.new unless 0 <= edge.from < size
    raise IndexError.new unless 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(i : Int32, j : Int32)
    add_edge(UnweightedEdge2.new(i, j))
  end

  def add_edges(edges : Array(UnweightedEdge2))
    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
end

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

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

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

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

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

  def add_edge(edge : UnweightedEdge2)
    raise IndexError.new unless 0 <= edge.from < size
    raise IndexError.new unless 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
end

module Comparable(T)
  def max(val : T)
    Math.max(self, val)
  end

  def min(val : T)
    Math.min(self, val)
  end
end

class UnweightedUndirectedGraph
  def solve(v, par) : {Int32, Int32}
    ab = each_child(v, par).map { |u| solve(u, v) }.to_a
    b = ab.sum(0, &.[0])
    a = b + (ab.max_of? { |x, y| y - x }.try(&.succ) || 0).max(0)
    {a, b}
  end
end

n = read_line.to_i
puts UnweightedUndirectedGraph.new(n, Array.new(n - 1) {
  i, j = read_line.split.map(&.to_i)
  UnweightedEdge2.new(i - 1, j - 1)
}).solve(0, nil).max
0