You are getting an incorrect answer, because you're calculating the count incorrectly. The it takes n-1
edges to connect n
nodes into a tree, and num_clusters-1
of those have to be red.
But if you fix that, your program will still be very slow, because of your disjoint set implementation.
Thankfully, it's actually pretty easy to implement a very efficient disjoint set data structure in a single array/list/vector in just about any programming language. Here's a nice one in python. I have python 2 on my box, so my print and input statements are a little different from yours:
# Create a disjoint set data structure, with n singletons, numbered 0 to n-1
# This is a simple array where for each item x:
# x > 0 => a set of size x, and x <= 0 => a link to -x
def ds_create(n):
return [1]*n
# Find the current root set for original singleton index
def ds_find(ds, index):
val = ds[index]
if (val > 0):
return index
root = ds_find(ds, -val)
if (val != -root):
ds[index] = -root # path compression
return root
# Merge given sets. returns False if they were already merged
def ds_union(ds, a, b):
aroot = ds_find(ds, a)
broot = ds_find(ds, b)
if aroot == broot:
return False
# union by size
if ds[aroot] >= ds[broot]:
ds[aroot] += ds[broot]
ds[broot] = -aroot
else:
ds[broot] += ds[aroot]
ds[aroot] = -broot
return True
# Count root sets
def ds_countRoots(ds):
return sum(1 for v in ds if v > 0)
#
# CherriesMesh solution
#
numTests = int(raw_input())
for testNum in range(1,numTests+1):
numNodes, numEdges = map(int,raw_input().split())
sets = ds_create(numNodes)
for _ in range(numEdges):
a,b = map(int,raw_input().split())
print a,b
ds_union(sets, a-1, b-1)
count = numNodes + ds_countRoots(sets) - 2
print 'Case #{0}: {1}'.format(testNum, count)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…