mirror of
https://github.com/iritmaximus/tira.git
synced 2026-10-12 19:13:47 +03:00
43 lines
990 B
Python
43 lines
990 B
Python
class CountPaths:
|
|
def __init__(self, nodes):
|
|
self.nodes = nodes
|
|
self.graph = {node: [] for node in self.nodes}
|
|
|
|
def add_edge(self, a, b):
|
|
self.graph[a].append(b)
|
|
|
|
def count_from(self, node):
|
|
if node in self.result:
|
|
return self.result[node]
|
|
|
|
path_count = 0
|
|
for next_node in self.graph[node]:
|
|
path_count += self.count_from(next_node)
|
|
|
|
self.result[node] = path_count
|
|
return path_count
|
|
|
|
def count_paths(self, x, y):
|
|
self.result = {y: 1}
|
|
return self.count_from(x)
|
|
|
|
|
|
def create_edges():
|
|
result = []
|
|
for i in range(2, 100):
|
|
result.extend([(1, i), (i, 100)])
|
|
|
|
result.append((97,99))
|
|
result.append((98,99))
|
|
return result
|
|
|
|
|
|
if __name__ == "__main__":
|
|
edges = create_edges()
|
|
print(edges)
|
|
|
|
counter = CountPaths(range(1, 100 + 1))
|
|
for edge in edges:
|
|
counter.add_edge(edge[0], edge[1])
|
|
print(counter.count_paths(1, 100)) # 100
|