結果

問題 No.1507 Road Blocked
ユーザー te-shte-sh
提出日時 2021-07-03 14:48:35
言語 Crystal
(1.11.2)
結果
AC  
実行時間 94 ms / 2,000 ms
コード長 5,584 bytes
コンパイル時間 23,038 ms
コンパイル使用メモリ 254,880 KB
実行使用メモリ 18,072 KB
最終ジャッジ日時 2023-09-12 19:07:32
合計ジャッジ時間 27,787 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,432 KB
testcase_01 AC 2 ms
4,480 KB
testcase_02 AC 2 ms
4,400 KB
testcase_03 AC 70 ms
15,356 KB
testcase_04 AC 92 ms
16,208 KB
testcase_05 AC 91 ms
16,340 KB
testcase_06 AC 93 ms
18,072 KB
testcase_07 AC 91 ms
16,412 KB
testcase_08 AC 91 ms
16,528 KB
testcase_09 AC 92 ms
16,392 KB
testcase_10 AC 92 ms
16,476 KB
testcase_11 AC 93 ms
16,788 KB
testcase_12 AC 91 ms
16,432 KB
testcase_13 AC 91 ms
16,328 KB
testcase_14 AC 93 ms
16,080 KB
testcase_15 AC 94 ms
16,604 KB
testcase_16 AC 90 ms
16,448 KB
testcase_17 AC 90 ms
16,536 KB
testcase_18 AC 91 ms
16,352 KB
testcase_19 AC 89 ms
16,360 KB
testcase_20 AC 91 ms
16,528 KB
testcase_21 AC 92 ms
16,364 KB
testcase_22 AC 92 ms
16,376 KB
testcase_23 AC 90 ms
16,080 KB
testcase_24 AC 92 ms
16,352 KB
testcase_25 AC 92 ms
16,596 KB
testcase_26 AC 91 ms
16,436 KB
testcase_27 AC 92 ms
16,304 KB
testcase_28 AC 91 ms
16,568 KB
testcase_29 AC 92 ms
16,424 KB
testcase_30 AC 90 ms
16,376 KB
testcase_31 AC 91 ms
16,412 KB
testcase_32 AC 89 ms
16,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main(io)
  n = io.get
  g = Graph.new(n)

  (n-1).times do
    ui, vi = io.get2; ui -= 1; vi -= 1
    g.add_edge_b(ui, vi)
  end
  t = g.tree(0)

  r = Mint.zero
  (1...n).each do |u|
    d1 = Mint.new(t.size_of(0))
    d2 = Mint.new(t.size_of(u))
    d3 = Mint.new(t.size_of(0) - t.size_of(u))
    r += (d3*(d3-1)//2 + d2*(d2-1)//2)//(d1*(d1-1)//2)
  end

  io.put r//(n-1)
end

class Graph
  alias Node = Int32

  def initialize(@n : Node)
    @g = Array(Array(Node)).new(@n) { [] of Node }
  end

  getter n : Node

  delegate :[], to: @g

  def add_edge(u : Node, v : Node)
    @g[u] << v
  end

  def add_edge_b(u : Node, v : Node)
    @g[u] << v
    @g[v] << u
  end
end

class GraphW(T)
  alias Node = Int32

  struct Edge(T)
    def initialize(@src : Node, @dst : Node, @wt : T)
    end

    getter src : Node, dst : Node

    getter wt : T
  end
      
  def initialize(@n : Node, @inf = 10**9)
    @g = Array(Array(Edge(T))).new(@n) { [] of Edge(T) }
  end

  getter n : Node

  getter inf : T

  delegate :[], to: @g

  def add_edge(u : Node, v : Node, wt : T)
    @g[u] << Edge.new(u, v, wt)
  end

  def add_edge_b(u : Node, v : Node, wt : T)
    @g[u] << Edge.new(u, v, wt)
    @g[v] << Edge.new(v, u, wt)
  end
end

class Tree
  alias Node = Graph::Node

  def initialize(@g : Graph, @root : Node)
    n = @g.n
    @parent = Array(Node).new(n, 0)
    @depth = Array(Int32).new(n, -1)
    @dfs_order = Array(Node).new

    s = [{@root, @root}]
    until s.empty?
      u, p = s.pop

      @parent[u] = p
      @depth[u] = @depth[p] + 1
      @dfs_order << u

      @g[u].each do |v|
        s.push({v, u}) if v != p
      end
    end

    @size = Array(Int32).new(n, 1)
    @dfs_order.reverse_each do |u|
      @size[@parent[u]] += @size[u] if u != @root
    end
  end

  getter root : Node

  getter dfs_order : Array(Node)

  def parent_of(u : Node)
    @parent[u]
  end

  def depth_of(u : Node)
    @depth[u]
  end

  def size_of(u : Node)
    @size[u]
  end

  def children_of(u : Node)
    @g[u].reject { |v| v == @parent[u] }
  end
end

class Graph
  def tree(root)
    Tree.new(self, root)
  end
end

macro min_u(a, b)
  {{a}} = Math.min({{a}}, {{b}})
end

macro max_u(a, b)
  {{a}} = Math.max({{a}}, {{b}})
end

def isqrt(n : Int32)
  m = 46340
  r = (1..m).bsearch { |i| i**2 > n }
  r.nil? ? m : r - 1
end

def powr(a : T, n : Int, i : T = T.multiplicative_identity) forall T
  powr(a, n, i) { |a, b| a * b }
end

def powr(a : T, n : Int, i : T = T.multiplicative_identity, &block) forall T
  return i if n == 0
  r, b = i, a
  while n > 0
    r = yield r, b if n.bit(0) == 1
    b = yield b, b
    n >>= 1
  end
  r
end

def ext_gcd(a : T, b : T) forall T
  if a == 0
    {b, T.new(0), T.new(1)}
  else
    g, x, y = ext_gcd(b%a, a)
    {g, y-(b//a)*x, x}
  end
end

abstract struct ModInt < Number
  def self.zero
    self.new(0)
  end

  def self.additive_identity
    self.new(0)
  end

  def self.multiplicative_identity
    self.new(1)
  end

  def initialize(v : Int)
    @v = (v % @@mod).to_i64
  end

  def_hash @@mod, @v

  def to_s
    @v.to_s
  end

  def to_s(io : IO) : Nil
    @v.to_s(io)
  end

  getter v : Int64

  delegate to_i, to: @v

  def ==(r : self)
    @v == r.v
  end

  def ==(r : Int)
    @v == (r % @@mod)
  end

  def - : self
    m(-@v)
  end

  def +(r : self)
    m(@v + r.v)
  end

  def +(r : Int)
    self + m(r)
  end

  def -(r : self)
    m(@v - r.v)
  end

  def -(r : Int)
    self - m(r)
  end

  def *(r : self)
    m(@v * r.v)
  end

  def *(r : Int)
    self * m(r)
  end

  def //(r : self)
    self * r.inv
  end

  def //(r : Int)
    self // m(r)
  end

  def **(n : Int)
    powr(self, n)
  end

  def inv
    m(ext_gcd(@v.to_i32, @@mod)[1])
  end


  private def m(v : Int)
    self.class.new(v)
  end
end
struct Mint < ModInt; @@mod : Int32 = 998244353; end

class ProconIO
  def initialize
    @buf = [] of String
    @index = 0
  end

  def get(k : T.class = Int32) forall T
    get_v(k)
  end

  def get(*ks : T.class) forall T
    ks.map { |k| get_v(k) }
  end

  macro define_getn
    {% for i in (2..9) %}
      def get{{i}}(k : T.class = Int32) forall T
        get({% for j in (1..i) %}k{% if j < i %}, {% end %}{% end %})
      end
    {% end %}
  end
  define_getn

  def get_a(n : Int, k : T.class = Int32) forall T
    Array.new(n) { get_v(k) }
  end

  def get_c(n : Int, k : T.class = Int32) forall T
    get_a(n, k)
  end

  def get_c(n : Int, *ks : T.class) forall T
    a = Array.new(n) { get(*ks) }
    ks.map_with_index { |_, i| a.map { |e| e[i] } }
  end

  macro define_getn_c
    {% for i in (2..9) %}
      def get{{i}}_c(n : Int, k : T.class = Int32) forall T
        get_c(n, {% for j in (1..i) %}k{% if j < i %}, {% end %}{% end %})
      end
    {% end %}
  end
  define_getn_c

  def get_m(r : Int, c : Int, k : T.class = Int32) forall T
    Array.new(r) { get_a(c, k) }
  end

  def put(*vs)
    vs.each.with_index do |v, i|
      put_v(v)
      print i < vs.size - 1 ? " " : "\n"
    end
  end

  def put_e(*vs)
    put(*vs)
    exit
  end


  private def get_v(k : Int32.class); get_token.to_i32; end
  private def get_v(k : Int64.class); get_token.to_i64; end
  private def get_v(k : String.class); get_token; end

  private def get_token
    if @buf.size == @index
      @buf = read_line.split
      @index = 0
    end
    v = @buf[@index]
    @index += 1
    v
  end

  private def put_v(vs : Enumerable)
    vs.each_with_index do |v, i|
      print v
      print " " if i < vs.size - 1
    end
  end

  private def put_v(v)
    print v
  end
end

main(ProconIO.new)
0