-
Notifications
You must be signed in to change notification settings - Fork 0
/
bowserPipeNewApproach.py
86 lines (58 loc) · 2.31 KB
/
bowserPipeNewApproach.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
68
69
70
71
72
73
74
75
76
77
78
79
from collections import deque
nNodes = int(input())
nodes = list(map(lambda x: int(x), input().split(" ")))
nLeafToEnter = int(input())
leafToEnter = list(map(lambda x: int(x), input().split(" ")))
#Children for each node
children = [[] for _ in range(nNodes)]
graphIds = [0]*nNodes
graphId = 0
for i in range(nNodes):
nodeVal = nodes[i]
if nodeVal == -1:
graphId = graphId+1
graphIds[i] = graphId
else:
children[nodeVal].append(i)
#Given a forest of "graphs",
# Use bfs to label the "graph" each node belong in and the closet node to root(other than root) for a graph
bestNodesForEachGraph = dict()
nodeToGraphId = dict()
visited = [False]*nNodes
def bfs(graphId, start):
currBestNode = (nNodes, nNodes)
#Create a queue for bfs
queue = deque([])
# currDepth = 0
# (depth, index of pipe)
queue.append((0, start))
while len(queue) > 0:
depth, nodeIndex = queue.popleft()
nodeToGraphId[nodeIndex] = graphId
# If this node has no children, then it is a leaf node.
if len(children[nodeIndex]) == 0:
#Min function will check first by depth, and break tie by node index
currBestNode = min(currBestNode, (depth, nodeIndex))
else:
nodeChildren = children[nodeIndex]
#Why can't I use current depth? If I pop a element, add its children,
# the current depth increase by one and now is 1, and the children has depth of 1.
# Then if I pop another element, add its children, the current depth is now 2 and the children has depth of 2
# However, this popped element is at the same level as the previous popped element,
# so its children should have depth of 2 as well.
# currDepth = currDepth+1
for child in nodeChildren:
if visited[child]:
continue
else:
visited[child] = True
queue.append((depth+1, child))
bestNodesForEachGraph[graphId] = currBestNode[1]
for i in range(len(graphIds)):
if graphIds[i] > 0:
bfs(graphIds[i], i)
for i in range(nLeafToEnter):
leaf = leafToEnter[i]
graphId = nodeToGraphId[leaf]
bestNode = bestNodesForEachGraph[graphId]
print(bestNode)