-
Notifications
You must be signed in to change notification settings - Fork 694
/
Copy pathsolution.py3
62 lines (50 loc) · 1.8 KB
/
solution.py3
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
60
61
62
'''
Problem: https://www.hackerrank.com/challenges/journey-to-the-moon
Python 3
Thoughts: Using Quick Find approach to build
Connected componenets. Basically each Connected
Componenet is a Country. Number of nodes in each component
is number of people in each country.
Using a counts array and an iteration over connected components
array to get count of people in each country.
Simple Mathematics to get total possibilities.
Time Complexity: O(N + I)
Space Complexity: O(N)
'''
N,P = list(map(int,input().strip().split(' ')))
connections = [-1] * N
'''Basic way of storing connected components for quick find.'''
def connect_quick_find(connections, u, v):
if connections[u] > -1 and connections[v] == -1:
connections[v] = connections[u]
return
if connections[v] > -1 and connections[u] == -1:
connections[u] = connections[v]
return
if connections[u] > -1 and connections[v] > -1:
connect_to = min(connections[u], connections[v])
connect_it = connections[v]
for i in range(len(connections)):
if connections[i] == connect_it:
connections[i] = connections[u]
return
connections[u] = u
connections[v] = u
return
for a0 in range(P):
u,v = input().strip().split(' ')
u,v = [int(u),int(v)]
connect_quick_find(connections,u,v)
'''Preparing a counts array of number of nodes in each connected component i.e number of persons in each country.'''
counts = [0] * N
for i in range(len(connections)):
if connections[i] == -1:
connections[i] = i
counts[connections[i]] = counts[connections[i]] + 1
combinations = 0
sum = 0
'''calculating combination'''
for i in range(len(counts)-1,-1,-1):
combinations = combinations + abs(counts[i])*sum
sum = sum + abs(counts[i])
print(combinations)