forked from mbobesic/algorithms-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoneyMatters.py
67 lines (50 loc) · 1.63 KB
/
MoneyMatters.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
60
61
62
63
64
65
66
67
# link: https://open.kattis.com/problems/moneymatters
# name: Money Matters
__author__ = 'mislav'
def is_possible(graph, vertex_values, vertex_count):
visited = [False]*vertex_count
for vertex in xrange(vertex_count):
if visited[vertex]:
continue
if vertex not in graph.keys():
if vertex_values[vertex] != 0:
return False
continue
component_sum = 0
stack = [vertex]
while len(stack) > 0:
current_vertex = stack.pop()
if visited[current_vertex]:
continue
visited[current_vertex] = True
component_sum += vertex_values[current_vertex]
for neighbour in graph[current_vertex]:
if not visited[neighbour]:
stack.append(neighbour)
if component_sum != 0:
return False
return True
def add_friend(graph, first, second):
friends = graph.get(first, [])
friends.append(second)
graph[first] = friends
return graph
if __name__ == "__main__":
input_line = raw_input()
inputs = [int(s) for s in input_line.split()]
n = inputs[0]
m = inputs[1]
vertex_values = []
for _ in xrange(n):
vertex_value = int(raw_input())
vertex_values.append(vertex_value)
graph = {}
for _ in xrange(m):
input_line = raw_input()
inputs = [int(s) for s in input_line.split()]
add_friend(graph, inputs[0], inputs[1])
add_friend(graph, inputs[1], inputs[0])
if is_possible(graph, vertex_values, n):
print "POSSIBLE"
else:
print "IMPOSSIBLE"