-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1082.py
59 lines (51 loc) · 1.74 KB
/
1082.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""
author: Alice Francener
problem: 1082 - Connected Components
description: https://www.urionlinejudge.com.br/judge/en/problems/view/1082
"""
'''Problema: quantos grafos conexos existem'''
class DisjointSets:
def __init__(self, n):
self.rank = [0] * n
self.parent = []
for i in range(0, n):
self.parent.append(i)
def find(self, u):
if u != self.parent[u]:
self.parent[u] = self.find(self.parent[u])
return self.parent[u]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if self.rank[x] > self.rank[y]:
self.parent[y] = x
else:
self.parent[x] = y
if self.rank[x] == self.rank[y]:
self.rank[y] = self.rank[y] + 1
'''input: 1. numero de casos, 2. numero de nos & numero de arestas, 3. arestas'''
n_casos = int(input())
for caso in range(n_casos):
entrada = input().split()
n_nodes = int(entrada[0])
n_edges = int(entrada[1])
alfabeto = DisjointSets(n_nodes)
for e in range(n_edges):
edge = input().split()
alfabeto.union(ord(edge[0])-97, ord(edge[1])-97)
for i in range(0, n_nodes):
alfabeto.find(i)
print("Case #{}:".format(caso+1))
connected = 0
for i in range(0, len(alfabeto.parent)):
graph = ''
if alfabeto.parent[i] != -1:
graph = graph + chr(i+97) + ','
for j in range(i+1, len(alfabeto.parent)):
if alfabeto.parent[i] == alfabeto.parent[j]:
graph = graph + chr(j+97) + ','
alfabeto.parent[j] = -1
if len(graph):
print(graph)
connected = connected + 1
print('{} connected components\n'.format(connected))