-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclosest_node_to_path_in_tree.py
80 lines (60 loc) · 2.58 KB
/
closest_node_to_path_in_tree.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
80
'''
You are given a positive integer n representing the number of nodes in a tree, numbered from 0 to n - 1 (inclusive). You are also given a 2D integer array edges of length n - 1, where edges[i] = [node1i, node2i] denotes that there is a bidirectional edge connecting node1i and node2i in the tree.
You are given a 0-indexed integer array query of length m where query[i] = [starti, endi, nodei] means that for the ith query, you are tasked with finding the node on the path from starti to endi that is closest to nodei.
Return an integer array answer of length m, where answer[i] is the answer to the ith query.
'''
def closestNode(self, n: int, edges: List[List[int]], query: List[List[int]]) -> List[int]:
G = defaultdict(list)
for a,b in edges:
G[a].append(b)
G[b].append(a)
D = [[math.inf]*n for _ in range(n)]
for a in range(n):
que,D[a][a] = [a],0
while que:
tmp = []
for q in que:
for b in G[q]:
if D[a][b]==math.inf:
D[a][b]=D[a][q]+1
tmp.append(b)
que = tmp
return [min(range(n), key=lambda x: D[x][a]+D[x][b]+D[x][q]) for a,b,q in query]
-------------------------------------------------------------------------------------------------
class Solution:
def closestNode(self, n, edges, query):
dict1 = collections.defaultdict(list)
for i,j in edges:
dict1[i].append(j)
dict1[j].append(i)
def dfs(start,end):
if start == end:
return True
visited.add(start)
for neighbor in dict1[start]:
if neighbor not in visited:
stack.append(neighbor)
if dfs(neighbor,end):
return True
stack.remove(neighbor)
def bfs(ans,path):
visited = set()
visited.add(ans[0])
while ans:
node = ans.pop(0)
if node in path:
return node
for neighbor in dict1[node]:
if neighbor not in visited:
visited.add(neighbor)
ans.append(neighbor)
result = []
for i in query:
visited = set()
stack = [i[0]]
dfs(i[0],i[1])
if i[2] in stack:
result.append(i[2])
continue
result.append(bfs([i[2]],stack))
return result