-
Notifications
You must be signed in to change notification settings - Fork 3
/
2277.cpp
42 lines (42 loc) · 1.47 KB
/
2277.cpp
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
class Solution {
public:
bool dfs(int parent, int curr, int target, vector<vector<int>>& graph, unordered_set<int>& st) {
if (curr == target) {
st.insert(curr);
return true;
}
for (auto& neighbor : graph[curr]) {
if (neighbor != parent && dfs(curr, neighbor, target, graph, st)) {
st.insert(curr);
return true;
}
}
return false;
}
bool findPath(int parent, int curr, int target, vector<vector<int>>& graph, unordered_set<int>& st) {
if (curr == target) return true;
for (auto& neighbor : graph[curr]) {
if (neighbor != parent && st.find(neighbor) == st.end() && findPath(curr, neighbor, target, graph, st)) return true;
}
return false;
}
vector<int> closestNode(int n, vector<vector<int>>& edges, vector<vector<int>>& query) {
vector<vector<int>> graph(n);
vector<int> res(query.size(), 0);
for (auto& edge : edges) {
graph[edge[0]].push_back(edge[1]);
graph[edge[1]].push_back(edge[0]);
}
for (int i = 0; i < query.size(); ++i) {
vector<int>& q = query[i];
unordered_set<int> path;
dfs(-1, q[0], q[1], graph, path);
for (auto& node : path) {
if (findPath(-1, node, q[2], graph, path)) {
res[i] = node;
}
}
}
return res;
}
};