代码拉取完成,页面将自动刷新
// Time: O(n + e)
// Space: O(n + e)
class Solution {
public:
enum Status { UNVISITED, VISITING, DONE };
bool leadsToDestination(int n, vector<vector<int>>& edges, int source, int destination) {
unordered_map<int, vector<int>> children;
for (const auto& edge : edges) {
children[edge[0]].emplace_back(edge[1]);
}
vector<Status> status(n);
return dfs(children, source, destination, &status);
}
private:
bool dfs(const unordered_map<int, vector<int>>& children,
int node,
int destination,
vector<Status> *status) {
if ((*status)[node] == DONE) {
return true;
}
if ((*status)[node] == VISITING) {
return false;
}
(*status)[node] = VISITING;
if (!children.count(node) && node != destination) {
return false;
}
if (children.count(node)) {
for (const auto& child : children.at(node)) {
if (!dfs(children, child, destination, status)) {
return false;
}
}
}
(*status)[node] = DONE;
return true;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。