Skip to content

Instantly share code, notes, and snippets.

@xyos
Created April 3, 2014 21:25
Show Gist options
  • Save xyos/9963230 to your computer and use it in GitHub Desktop.
Save xyos/9963230 to your computer and use it in GitHub Desktop.
t = int(raw_input())
def find_set(a,graph):
s = set([a])
for st in graph:
if a in st:
s = st
return s
def add_to_graph(a,b,graph):
s_a = find_set(a,graph)
s_b = find_set(b,graph)
s_a_b = s_a | s_b
if s_a in graph:
graph.remove(s_a)
if s_b in graph:
graph.remove(s_b)
graph.append(s_a_b)
print len(s_a_b)
while t:
t -= 1
graph = []
nodes = int(raw_input())
for i in range(nodes):
a,b = raw_input().split()
add_to_graph(a,b,graph)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment