Skip to content

Instantly share code, notes, and snippets.

@marianabianca
Created November 22, 2020 20:13
Show Gist options
  • Save marianabianca/4a74074ce12740bd7ca382b5487f801d to your computer and use it in GitHub Desktop.
Save marianabianca/4a74074ce12740bd7ca382b5487f801d to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
def calcula_familia(pessoa):
global grafo
global familia
global familias
if pessoa in grafo:
for parente in grafo[pessoa]:
if parente not in familias:
familias[parente] = familia
calcula_familia(parente)
m, n = list(map(int, input().split()))
grafo = {}
for i in range(n):
p1, _, p2 = list(input().split())
if p1 not in grafo:
grafo[p1] = []
if p2 not in grafo:
grafo[p2] = []
grafo[p1].append(p2)
grafo[p2].append(p1)
familia = 1
familias = {}
for pessoa in grafo.keys():
if not pessoa in familias:
calcula_familia(pessoa)
familia += 1
print(familia-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment