Submission #2497462
Source Code Expand
class UnionFind
attr_reader :set_size
def initialize(n)
@par = Array.new(n)
n.times{ |i| @par[i] = i }
@rank = Array.new(n, 0)
@set_size = Array.new(n, 0)
end
def find(x)
if @par[x] == x
x
else
@par[x] = find(@par[x])
end
end
def unite(x,y)
x = find(x)
y = find(y)
return if x == y
if @rank[x] < @rank[y]
@par[x] = y
@set_size[y] += @set_size[x]
else
@par[y] = x
@set_size[x] += @set_size[y]
@rank[x] += 1 if @rank[x] == @rank[y]
end
end
def same?(x, y)
find(x) == find(y)
end
end
N, M = gets.split.map(&:to_i)
ps = gets.split.map(&:to_i)
uf = UnionFind.new(N)
M.times do |i|
x, y = gets.split.map(&:to_i)
uf.unite(x-1, y-1)
end
ans = 0
N.times do |i|
ans += 1 if uf.same?(i, ps[i] - 1)
end
puts ans
Submission Info
| Submission Time | |
|---|---|
| Task | D - Equals |
| User | betrue12 |
| Language | Ruby (2.3.3) |
| Score | 400 |
| Code Size | 1004 Byte |
| Status | AC |
| Exec Time | 315 ms |
| Memory | 15116 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 400 / 400 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt |
| All | 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt, 1_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 1_018.txt, 1_019.txt, 1_020.txt, 1_021.txt, 1_022.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 0_000.txt | AC | 7 ms | 1788 KiB |
| 0_001.txt | AC | 7 ms | 1788 KiB |
| 0_002.txt | AC | 7 ms | 1788 KiB |
| 0_003.txt | AC | 7 ms | 1788 KiB |
| 1_004.txt | AC | 171 ms | 1788 KiB |
| 1_005.txt | AC | 244 ms | 13068 KiB |
| 1_006.txt | AC | 315 ms | 13068 KiB |
| 1_007.txt | AC | 7 ms | 1788 KiB |
| 1_008.txt | AC | 7 ms | 1788 KiB |
| 1_009.txt | AC | 7 ms | 1788 KiB |
| 1_010.txt | AC | 7 ms | 1788 KiB |
| 1_011.txt | AC | 7 ms | 1788 KiB |
| 1_012.txt | AC | 7 ms | 1788 KiB |
| 1_013.txt | AC | 9 ms | 1788 KiB |
| 1_014.txt | AC | 16 ms | 1788 KiB |
| 1_015.txt | AC | 7 ms | 1788 KiB |
| 1_016.txt | AC | 8 ms | 1788 KiB |
| 1_017.txt | AC | 10 ms | 1788 KiB |
| 1_018.txt | AC | 163 ms | 1788 KiB |
| 1_019.txt | AC | 74 ms | 10508 KiB |
| 1_020.txt | AC | 73 ms | 10508 KiB |
| 1_021.txt | AC | 75 ms | 10636 KiB |
| 1_022.txt | AC | 297 ms | 15116 KiB |