标签:拓扑排序 push 排序 vector can return sizeof -- color
拓扑排序问题。
class Solution { public: bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) { int Hash[10010];//每个节点的入度 vector<vector<int>> v(numCourses);//用于存储每个节点相邻的边 stack<int> s;//存放入度为0的节点 memset(Hash, 0, sizeof(Hash)); for(int i =0 ; i < prerequisites.size();i++){ pair<int, int>p = prerequisites[i]; int x = p.first; int y = p.second; cout<<"x = "<<x <<"y = "<<y<<endl; v[x].push_back(y); ++Hash[y]; } for(int i = 0; i < numCourses; i++){ if(Hash[i] == 0){ s.push(i); } } while (!s.empty()) { int cur = s.top();//找到入度为0的点 s.pop(); for(int i = 0; i < prerequisites.size(); i++){ pair<int, int>p = prerequisites[i]; int x = p.first; int y = p.second; if(cur == x){ for(int j = 0; j < v[cur].size(); j++){ --Hash[v[cur][j]];//删除以该点为起点的所有有向边 if(Hash[v[cur][j]] == 0) s.push(v[cur][j]); } break; } } } for(int i = 0; i < numCourses; i++){ if(Hash[i] != 0) return false; } return true; } };
标签:拓扑排序 push 排序 vector can return sizeof -- color
原文地址:https://www.cnblogs.com/asenyang/p/9746691.html