結果
問題 | No.1507 Road Blocked |
ユーザー | yuruhiya |
提出日時 | 2021-05-15 18:20:50 |
言語 | Crystal (1.11.2) |
結果 |
AC
|
実行時間 | 90 ms / 2,000 ms |
コード長 | 6,024 bytes |
コンパイル時間 | 13,880 ms |
コンパイル使用メモリ | 295,220 KB |
実行使用メモリ | 22,740 KB |
最終ジャッジ日時 | 2024-10-03 05:43:11 |
合計ジャッジ時間 | 17,894 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 1 ms
6,820 KB |
testcase_02 | AC | 2 ms
6,820 KB |
testcase_03 | AC | 79 ms
22,740 KB |
testcase_04 | AC | 85 ms
13,840 KB |
testcase_05 | AC | 86 ms
13,696 KB |
testcase_06 | AC | 85 ms
13,824 KB |
testcase_07 | AC | 85 ms
13,820 KB |
testcase_08 | AC | 81 ms
13,824 KB |
testcase_09 | AC | 85 ms
13,784 KB |
testcase_10 | AC | 84 ms
13,820 KB |
testcase_11 | AC | 82 ms
13,696 KB |
testcase_12 | AC | 83 ms
13,836 KB |
testcase_13 | AC | 82 ms
13,696 KB |
testcase_14 | AC | 87 ms
13,832 KB |
testcase_15 | AC | 87 ms
13,696 KB |
testcase_16 | AC | 89 ms
13,824 KB |
testcase_17 | AC | 88 ms
13,824 KB |
testcase_18 | AC | 83 ms
13,824 KB |
testcase_19 | AC | 88 ms
13,704 KB |
testcase_20 | AC | 85 ms
13,824 KB |
testcase_21 | AC | 83 ms
13,824 KB |
testcase_22 | AC | 85 ms
13,796 KB |
testcase_23 | AC | 85 ms
13,824 KB |
testcase_24 | AC | 87 ms
13,824 KB |
testcase_25 | AC | 85 ms
13,824 KB |
testcase_26 | AC | 84 ms
13,808 KB |
testcase_27 | AC | 84 ms
13,824 KB |
testcase_28 | AC | 82 ms
13,824 KB |
testcase_29 | AC | 90 ms
13,824 KB |
testcase_30 | AC | 88 ms
13,696 KB |
testcase_31 | AC | 85 ms
13,824 KB |
testcase_32 | AC | 85 ms
13,696 KB |
ソースコード
# 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/tree" # 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 class Graph(T) 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 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 each_edge(v : Int32) : Nil graph[v].each do |edge| yield Edge2(T).new(v, edge.to, edge.cost) end end def edges : Array(Edge2(T)) result = [] of Edge2(T) each_edge do |edge| result << edge end result end def edges(v : Int32) : Array(Edge2(T)) result = [] of Edge2(T) each_edge(v) do |edge| result << edge end result end end struct UnWeightedEdge property from : Int32 property to : Int32 def initialize(@from, @to) end end class UnWeightedGraph getter size : Int32 getter graph : Array(Array(Int32)) def initialize(@size) raise ArgumentError.new("Negative graph size: #{size}") unless size >= 0 @graph = Array.new(size) { Array(Int32).new } end def initialize(@size, edges : Array(UnWeightedEdge), *, undirected : Bool) raise ArgumentError.new("Negative graph size: #{size}") unless size >= 0 @graph = Array.new(size) { Array(Int32).new } edges.each do |edge| @graph[edge.from] << edge.to @graph[edge.to] << edge.from if undirected end end delegate size, to: @graph delegate :[], to: @graph def add_edge(i : Int32, j : Int32) raise IndexError.new unless 0 <= i < size raise IndexError.new unless 0 <= j < size graph[i] << j graph[j] << i end def add_edge_directed(i : Int32, j : Int32) raise IndexError.new unless 0 <= i < size raise IndexError.new unless 0 <= j < size graph[i] << j end end class UnWeightedGraph def subtree_size_dfs(v : Int32, p : Int32, result : Array(Int32)) : Int32 result[v] = 1 + self[v].select { |u| u != p }.sum { |u| subtree_size_dfs(u, v, result) } end def subtree_size(root : Int32) result = Array.new(size, 0) subtree_size_dfs(root, -1, result) result end end struct Mint @@MOD = 998244353i64 def self.mod @@MOD end def self.zero Mint.new end @value : Int64 def initialize @value = 0i64 end def initialize(value) @value = value.to_i64 % @@MOD end def initialize(m : Mint) @value = m.value end protected def value=(value : Int64) @value = value end getter value : Int64 def + : self self end def - : self Mint.new(value != 0 ? @@MOD - @value : 0) end def +(m) self + m.to_mint end def +(m : Mint) result = Mint.new result.value = @value + m.value result.value -= @@MOD if result.value >= @@MOD result end def -(m) self - m.to_mint end def -(m : Mint) result = Mint.new result.value = @value - m.value result.value += @@MOD if result.value < 0 result end def *(m) result = Mint.new result.value = @value * Mint.new(m).value % @@MOD result end def /(m) raise DivisionByZeroError.new if m == 0 a, b, u, v = m.to_i64, @@MOD, 1i64, 0i64 while b != 0 t = a // b a -= t * b a, b = b, a u -= t * v u, v = v, u end Mint.new(@value * u) end def //(m) self / m end def **(m : Int) t, res = self, Mint.new(1) while m > 0 res *= t if m.odd? t *= t m >>= 1 end res end def ==(m) @value == m.to_i64 end def !=(m) @value != m.to_i64 end def succ self + 1 end def pred self - 1 end def to_i64 : Int64 @value end delegate to_s, to: @value delegate inspect, to: @value end struct Int def to_mint : Mint Mint.new(self) end end class String def to_mint : Mint Mint.new(self) end end n = read_line.to_i m = Mint.new(n.to_i64 * ~-n // 2) g = UnWeightedGraph.new n, (1..n - 1).map { a, b = read_line.split.map &.to_i.pred UnWeightedEdge.new(a, b) }, undirected: true puts g.subtree_size(0)[1..].sum { |size| size2 = Mint.new(size.to_i64 * (n - size)) (m - size2) / m } / n.pred