Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
all-paths-from-source-lead-to-destination.cpp 1.18 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2019-06-02 23:08 +08:00 . Create all-paths-from-source-lead-to-destination.cpp
// 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;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助