結果
| 問題 |
No.1605 Matrix Shape
|
| コンテスト | |
| ユーザー |
yuruhiya
|
| 提出日時 | 2021-07-17 12:18:24 |
| 言語 | Crystal (1.14.0) |
| 結果 |
AC
|
| 実行時間 | 206 ms / 2,000 ms |
| コード長 | 9,896 bytes |
| コンパイル時間 | 13,231 ms |
| コンパイル使用メモリ | 297,352 KB |
| 実行使用メモリ | 28,008 KB |
| 最終ジャッジ日時 | 2024-07-07 00:03:15 |
| 合計ジャッジ時間 | 17,582 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 34 |
ソースコード
# 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
yuruhiya