結果
問題 | No.1605 Matrix Shape |
ユーザー | yuruhiya |
提出日時 | 2021-07-17 12:15:21 |
言語 | Crystal (1.11.2) |
結果 |
MLE
|
実行時間 | - |
コード長 | 9,243 bytes |
コンパイル時間 | 18,071 ms |
コンパイル使用メモリ | 297,952 KB |
実行使用メモリ | 513,532 KB |
最終ジャッジ日時 | 2024-07-06 23:59:11 |
合計ジャッジ時間 | 18,699 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 1 ms
6,944 KB |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 1 ms
6,940 KB |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | AC | 1 ms
6,940 KB |
testcase_07 | AC | 2 ms
6,944 KB |
testcase_08 | AC | 1 ms
6,940 KB |
testcase_09 | AC | 2 ms
6,940 KB |
testcase_10 | AC | 2 ms
6,944 KB |
testcase_11 | AC | 2 ms
6,944 KB |
testcase_12 | AC | 1 ms
6,940 KB |
testcase_13 | AC | 1 ms
6,944 KB |
testcase_14 | AC | 1 ms
6,944 KB |
testcase_15 | AC | 2 ms
6,940 KB |
testcase_16 | AC | 1 ms
6,940 KB |
testcase_17 | AC | 2 ms
6,944 KB |
testcase_18 | AC | 2 ms
6,940 KB |
testcase_19 | AC | 253 ms
41,308 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 | -- | - |
ソースコード
# 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 n = input(i) vertex = input(i[n * 2]).compress m = vertex.max + 1 graph = UnweightedDirectedGraph.new m, vertex.each_slice(2).map { |v| {Int32, Int32}.from v } _, _, groups = graph.components indegree, outdegree = graph.indegree, graph.outdegree degree = indegree.zip(outdegree).map { |x, y| x - y } if groups.size >= 2 puts 0 elsif degree.all?(0) puts m elsif degree.tally == {-1 => 1, 1 => 1, 0 => m - 2} puts 1 else puts 0 end