Skip to content

Commit 5e13716

Browse files
committed
[feat ]: leetcode 1971
Signed-off-by: Bo-Wei Chen(BWbwchen) <tim.chenbw@gmail.com>
1 parent 6d78a31 commit 5e13716

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public:
3+
bool validPath(int n, vector<vector<int>>& edges, int source, int destination) {
4+
if (source == destination)
5+
return true;
6+
stack<int> st;
7+
st.push(source);
8+
bool ret = false;
9+
10+
vector<bool> visited(n, false);
11+
vector<vector<int>> adj(n);
12+
for (auto e: edges) {
13+
adj[e[0]].push_back(e[1]);
14+
adj[e[1]].push_back(e[0]);
15+
}
16+
17+
while (!st.empty()) {
18+
auto top = st.top();
19+
st.pop();
20+
21+
if (!visited[top]) {
22+
visited[top] = true;
23+
for (auto neigh : adj[top]) {
24+
st.push(neigh);
25+
if (neigh == destination) {
26+
ret = true;
27+
goto found;
28+
}
29+
}
30+
}
31+
}
32+
found:
33+
return ret;
34+
}
35+
};

0 commit comments

Comments
 (0)