活跃农民
- 积分
- 380
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2014-7-20
- 最后登录
- 1970-1-1
|
- class Solution482 {
- public:
- vector<string> detectCycle(vector<vector<string>>& image) {
- if (image.empty()) {
- return {};
- }
- unordered_map<string, bool> available;
- vector<string> output;
- unordered_map<string, vector<string>> graph;
- buildGraph(image, graph);
- int leng=graph.size();
- unordered_map<string, bool> visited;
- unordered_map<string, bool> recStack;
- for (auto it = graph.begin(); it != graph.end(); ++it) {
- //case 1:not visited
- visited[it->first]=false;
- recStack[it->first]=false;
- }
- for (auto it = graph.begin(); it != graph.end(); ++it) {
- if (recStack[it->first]==true) {
- continue;
- }
- if (!isCyclicHelper(it->first, graph, visited, recStack)) {
- available[it->first]=true;
- }
- }
- topoSort(graph, available, output);
- return output;
- }
- void test() {
- vector<vector<string>> graph;
- vector<string> result;
- graph={{"master", "ubuntu"}, {"numpy", "master"}, {"tensorflow", "numpy"}};
- result=detectCycle(graph);
- //Output: ubuntu, master, numpy, tensorflow
- for (auto i : result) {
- cout<<i<<" ";
- }
- cout<<endl;
- graph={{"python", "numpy"}, {"numpy", "python"}, {"tensorflow", "ubuntu"}};
- result=detectCycle(graph);
- //Output: ubuntu, tensorflow
- for (auto i : result) {
- cout<<i<<" ";
- }
- cout<<endl;
- graph={{"b", "c"}, {"c", "d"}, {"a", "b"}, {"d", "e"}, {"e","c"}, {"f", "g"}};
- result=detectCycle(graph);
- //Ouput: g, f
- for (auto i : result) {
- cout<<i<<" ";
- }
- cout<<endl;
- }
- private:
- void topoSort(unordered_map<string, vector<string>> graph, unordered_map<string, bool>& available, vector<string>& result) {
- unordered_map<string, int> state;
- for (auto it = graph.begin(); it != graph.end(); ++it) {
- state[it->first]=0;
- }
- for (auto it = graph.begin(); it != graph.end(); ++it) {
- if (available[it->first]==true) {
- visit(graph, it->first, result, state);
- }
- }
- return;
- }
-
- void visit(unordered_map<string, vector<string>>& image,
- string it,
- vector<string>& list,
- unordered_map<string, int>& state) {
- if (state[it]==3) {
- return;
- }
- if (state[it]==2) {
- return;
- }
- state[it]=2;
- for (auto neib:image[it]) {
- visit(image, neib, list, state);
- }
- state[it]=3;
- list.push_back(it);
- }
-
- void buildGraph(vector<vector<string>>& image, unordered_map<string, vector<string>>& graph) {
- for (int i=0; i<image.size(); i++) {
- graph[image[i][0]].push_back(image[i][1]);
- }
- }
-
- bool isCyclicHelper(string v, unordered_map<string, vector<string>>& graph, unordered_map<string, bool>& visited, unordered_map<string, bool>& recStack) {
- if (visited[v]==false) {
- visited[v]=true;
- recStack[v]=true;
- for (auto it=graph[v].begin(); it!=graph[v].end(); ++it) {
- if (!visited[*it] && isCyclicHelper(*it, graph, visited, recStack)) {
- return true;
- }
- else if (recStack[*it]==true) {
- return true;
- }
- }
- }
- recStack[v]=false;
- return false;
- }
- };
- 这里的E1 为什么不输出ubuntu, E2 也是, E3 为什么不输出g呢
复制代码 |
|