|
1 | | -# Problem from Hackerearth |
2 | | -# https://www.hackerearth.com/fr/practice/algorithms/graphs/depth-first-search/practice-problems/algorithm/bishu-and-his-girlfriend/description/ |
| 1 | +# Quy Nguyen's late-night adventure. |
3 | 2 |
|
| 3 | +# https://www.hackerearth.com/fr/practice/algorithms/graphs/depth-first-search/practice-problems/algorithm/bishu-and-his-girlfriend/description/ |
| 4 | +# Quy Nguyen lives in node 1, desperate and lonely |
| 5 | +# He must find the nearest "girlfriend" in the Geylang tree |
| 6 | +# (We all know who Quy Nguyen is really looking for) |
| 7 | +# If tie on distance, pick the one with smallest node number |
4 | 8 |
|
5 | | -def find_girl(N, graph, girls): |
6 | | - s = [] |
7 | | - visited = [False for i in range(N + 1)] |
| 9 | +from collections import deque |
8 | 10 |
|
9 | | - roads = [0 for i in range(N + 1)] |
10 | | - |
11 | | - visited[1] = True |
12 | | - s.append(1) |
13 | | - |
14 | | - selected_girl = N |
15 | | - selected_road = N |
16 | | - |
17 | | - while len(s) > 0: |
18 | | - u = s[-1] |
19 | | - s.pop() |
20 | | - |
21 | | - for v in graph[u]: |
22 | | - if not visited[v]: |
23 | | - roads[v] = roads[u] + 1 |
24 | | - if roads[v] <= selected_road: |
25 | | - if girls[v]: |
26 | | - if roads[v] < selected_road or (roads[v] == selected_road and selected_girl > v): |
27 | | - selected_girl = v |
28 | | - selected_road = roads[v] |
29 | | - visited[v] = True |
30 | | - else: |
31 | | - visited[v] = True |
32 | | - s.append(v) |
33 | | - |
34 | | - return selected_girl |
35 | | - |
36 | | - |
37 | | -def solution(): |
| 11 | +def solve(): |
38 | 12 | N = int(input()) |
39 | | - roads = [] |
40 | | - graph = [[] for i in range(N + 1)] |
41 | | - for i in range(N-1): |
| 13 | + g = [[] for _ in range(N + 1)] |
| 14 | + for _ in range(N - 1): |
42 | 15 | u, v = map(int, input().split()) |
43 | | - graph[u].append(v) |
44 | | - graph[v].append(u) |
| 16 | + g[u].append(v) |
| 17 | + g[v].append(u) |
45 | 18 |
|
46 | 19 | Q = int(input()) |
47 | | - girls = [0 for i in range(N + 1)] |
48 | | - for i in range(Q): |
49 | | - girls[int(input())] = 1 |
50 | | - |
51 | | - print(find_girl(N, graph, girls)) |
52 | | - |
53 | 20 |
|
54 | | -solution() |
| 21 | + # Mark the "ladies of the night" locations |
| 22 | + hoes = set(int(input()) for _ in range(Q)) |
| 23 | + |
| 24 | + # BFS from Quy Nguyen's house (node 1) — the horny search algorithm |
| 25 | + dist = [-1] * (N + 1) |
| 26 | + dist[1] = 0 |
| 27 | + q = deque([1]) |
| 28 | + best, best_d = N, N # worst case: the farthest hooker |
| 29 | + while q: |
| 30 | + u = q.popleft() |
| 31 | + if u in hoes and (dist[u] < best_d or (dist[u] == best_d and u < best)): |
| 32 | + best, best_d = u, dist[u] # found a closer thot |
| 33 | + for v in g[u]: |
| 34 | + if dist[v] == -1: |
| 35 | + dist[v] = dist[u] + 1 |
| 36 | + if dist[v] <= best_d: # pruning: no point going deeper than current best |
| 37 | + q.append(v) |
| 38 | + |
| 39 | + # Do we even have Jenny in the testcases? Wondering |
| 40 | + print(best) |
| 41 | + |
| 42 | +solve() |
0 commit comments