DAG问题
vector<int> g[5000];
int vis[5000],ok = 0;
vector<int> r;
void dfs(int cur)
{
vis[cur] = 1;
if(ok)
return;
for(int i:g[cur])
{
if(vis[i] == 1)
{
ok = 1;
return;
}
if(vis[i]!=2)
dfs(i);
}
vis[cur] = 2;
r.push_back(cur);
return;
}
vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
for(int i = 0;i < prerequisites.size();i++)
{
g[prerequisites[i][0]].push_back(prerequisites[i][1]);
}
for(int i =0;i < numCourses;i++)
if(vis[i]!=2)
dfs(i);
if(ok)
return {};
return r;
}