Submission #47534879
Source Code Expand
n, q = read_line.split.map(&.to_i)
qs = [] of Tuple(Int32, Int32, Int64)
# 非連結な頂点同士を結ぶ辺のみを抽出したグラフを作成
graph = Array.new(n) { [] of Tuple(Int32, Int64) }
uf = UnionFind.new(n)
q.times do |i|
a, b, d = read_line.split.map(&.to_i)
a -= 1
b -= 1
qs << {a, b, d.to_i64}
if !uf.find(a, b)
uf.union(a, b)
graph[a] << {b, -d.to_i64}
graph[b] << {a, d.to_i64}
end
end
# 各連結成分でBFSしてvalidなXの値(のひとつ)を構築
x = Array.new(n, 0i64)
visited = Array.new(n, false)
n.times do |i|
next if visited[i]
que = [i]
qi = 0
visited[i] = true
while qi < que.size
cur = que[qi]
qi += 1
graph[cur].each do |adj, diff|
if !visited[adj]
visited[adj] = true
x[adj] = x[cur] + diff
que << adj
end
end
end
end
# Xの値のつじつまが合っているものを出力
ans = [] of Int32
q.times do |i|
a, b, d = qs[i]
if x[a] - x[b] == d
ans << i + 1
end
end
puts ans.join(" ")
class UnionFind
def initialize(size : Int32)
@root = Array(Int32).new(size, -1)
end
def union(i, j)
a = root(i)
b = root(j)
if a != b
@root[a] += @root[b]
@root[b] = a
end
end
def find(i, j)
root(i) == root(j)
end
def root(i)
return i if @root[i] < 0
@root[i] = root(@root[i])
end
def size(i)
-@root[root(i)]
end
end
Submission Info
| Submission Time | |
|---|---|
| Task | F - Good Set Query |
| User | tomerun |
| Language | Crystal (Crystal 1.9.1) |
| Score | 525 |
| Code Size | 1496 Byte |
| Status | AC |
| Exec Time | 131 ms |
| Memory | 54444 KiB |
Compile Error
I: Dependencies are satisfied I: Building: main
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 525 / 525 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | example0.txt, example1.txt, example2.txt |
| All | 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, example0.txt, example1.txt, example2.txt, hack_001.txt, hack_002.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 000.txt | AC | 38 ms | 18616 KiB |
| 001.txt | AC | 43 ms | 17864 KiB |
| 002.txt | AC | 65 ms | 28108 KiB |
| 003.txt | AC | 57 ms | 30272 KiB |
| 004.txt | AC | 64 ms | 25968 KiB |
| 005.txt | AC | 86 ms | 52628 KiB |
| 006.txt | AC | 84 ms | 52536 KiB |
| 007.txt | AC | 82 ms | 54444 KiB |
| 008.txt | AC | 82 ms | 52840 KiB |
| 009.txt | AC | 47 ms | 22204 KiB |
| 010.txt | AC | 41 ms | 16556 KiB |
| 011.txt | AC | 44 ms | 19028 KiB |
| 012.txt | AC | 45 ms | 20768 KiB |
| 013.txt | AC | 39 ms | 14104 KiB |
| 014.txt | AC | 40 ms | 17556 KiB |
| 015.txt | AC | 46 ms | 17496 KiB |
| 016.txt | AC | 47 ms | 19476 KiB |
| 017.txt | AC | 40 ms | 18816 KiB |
| 018.txt | AC | 42 ms | 20112 KiB |
| 019.txt | AC | 47 ms | 19828 KiB |
| 020.txt | AC | 50 ms | 19080 KiB |
| 021.txt | AC | 42 ms | 16972 KiB |
| 022.txt | AC | 43 ms | 16276 KiB |
| 023.txt | AC | 50 ms | 19000 KiB |
| 024.txt | AC | 51 ms | 15152 KiB |
| 025.txt | AC | 44 ms | 19524 KiB |
| 026.txt | AC | 55 ms | 22376 KiB |
| 027.txt | AC | 60 ms | 23376 KiB |
| 028.txt | AC | 63 ms | 26388 KiB |
| 029.txt | AC | 54 ms | 24132 KiB |
| 030.txt | AC | 129 ms | 52040 KiB |
| 031.txt | AC | 131 ms | 52068 KiB |
| 032.txt | AC | 128 ms | 51900 KiB |
| 033.txt | AC | 130 ms | 51952 KiB |
| example0.txt | AC | 1 ms | 3240 KiB |
| example1.txt | AC | 16 ms | 19524 KiB |
| example2.txt | AC | 1 ms | 3352 KiB |
| hack_001.txt | AC | 1 ms | 3320 KiB |
| hack_002.txt | AC | 1 ms | 3248 KiB |