forked from charlesfranciscodev/codingame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dwarfs_giants.py
54 lines (43 loc) · 1.57 KB
/
dwarfs_giants.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
from typing import Dict, Set
class Node:
def __init__(self):
self.children: Set[int] = set()
self.depth = 0
class Graph:
def __init__(self):
self.nodes: Dict[int, Node] = {} # node_id => node
def build(self):
nb_edges = int(input()) # the number of relationships of influence
for _ in range(nb_edges):
parent_id, child_id = map(int, input().split())
if child_id in self.nodes:
child: Node = self.nodes[child_id]
else:
child: Node = Node()
self.nodes[child_id] = child
if parent_id in self.nodes:
parent: Node = self.nodes[parent_id]
parent.children.add(child_id)
else:
parent: Node = Node()
parent.children.add(child_id)
self.nodes[parent_id] = parent
def solve(self) -> int:
"""Returns the number of people involved in the longest succession of influences."""
max_depth = 0
for node in self.nodes.values():
self.traverse(node)
for node in self.nodes.values():
if node.depth > max_depth:
max_depth = node.depth
return max_depth + 1
def traverse(self, node: Node):
for child_id in node.children:
child: Node = self.nodes[child_id]
if child.depth <= node.depth:
child.depth = node.depth + 1
self.traverse(child)
if __name__ == "__main__":
graph = Graph()
graph.build()
print(graph.solve())