207. Course Schedule
题目地址:https://leetcode.com/problems/course-schedule/
求解思路
节点的入度,出度
AOE网的拓扑排序算法
ac代码
class Solution {
public:
vector<int> in_degree; // 入度
vector<vector<int>> adj_table; //邻接表
int n;
bool top(int n)
{
int num = 0;
while (num < n)
{
int i = 0;
while (i < n && in_degree[i] != 0){
i++;
}
if (i >= n){
return false;
}
num++;
in_degree[i] = -2; // 删除该节点,相应的入度减1
int len = adj_table[i].size();
for (int j = 0; j < len; j++){
in_degree[adj_table[i][j]] --;
}
}
return true;
}
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
in_degree = vector<int>(numCourses, 0);//包括孤立点在内,in_degree=0
adj_table.resize(numCourses);
vector<pair<int, int>>::iterator it = prerequisites.begin();
while (it != prerequisites.end())
{
in_degree[it->second] ++;
adj_table[it->first].push_back(it->second);
++it;
}
return top(numCourses);
}
};
210. Course Schedule II
题目地址
https://leetcode.com/problems/course-schedule-ii/
求解思路
在上一题基础上,需要求出序列
因为只要求出满足要求的一个解就行了,所以仍然可以按照上题的求解思路
ac代码如下
class Solution {
public:
vector<int> in_degree; // 入度
vector<vector<int>> adj_table; //邻接表
vector<int> ans;
bool flag;
bool top(int n)
{
int num = 0;
while (num < n)
{
int i = 0;
while (i < n && in_degree[i] != 0){
i++;
}
if (i >= n){
return false;
}
ans.push_back(i);
num++;
in_degree[i] = -2; // 删除该节点,相应的入度减1
int len = adj_table[i].size();
for (int j = 0; j < len; j++){
in_degree[adj_table[i][j]] --;
}
}
return true;
}
vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) {
in_degree = vector<int>(numCourses, 0);//包括孤立点在内,in_degree=0
adj_table.resize(numCourses);
vector<pair<int, int>>::iterator it = prerequisites.begin();
while (it != prerequisites.end())
{
in_degree[it->second] ++;
adj_table[it->first].push_back(it->second);
++it;
}
vector<int> rs;
if (top(numCourses))
{
rs.assign(ans.rbegin(), ans.rend());
}
return rs;
}
};
超时,超内存代码
dfs求解,可以得到所有的解,代码超时
class Solution {
public:
vector<int> in_degree; // 入度
vector<vector<int>> adj_table; //邻接表
int n;
vector<int> ans;
vector<bool> vis;
bool flag;
vector<int> rs;
void top(int cnt)
{
if (flag)
return;
if(cnt > 0)
{
vector<int> in_d0;
int in_len = 0;
for (int i = 0; i < n; i++)
{
if (!vis[i] && in_degree[i] == 0)
{
in_d0.push_back(i);
in_len++;
}
}
if (in_len == 0)
{
return;
}
for (int i = 0; i < in_len; i++)
{
int po = in_d0[i];
int tmp = in_degree[po];
in_degree[po] = -2; // 删除该节点,相应的入度减1
vis[po] = true;
int len = adj_table[po].size();
for (int j = 0; j < len; j++){
in_degree[adj_table[po][j]] --;
}
ans.push_back(po);
top(cnt - 1); // 递归 回溯
if (flag)
return;
int ansLen = ans.size();
if (ansLen == n)
{
/*for (int k = 0; k < n; k++) cout << ans[k] << " "; cout << endl;*/
flag = true;
rs.assign(ans.rbegin(), ans.rend());
return;
}
ans.pop_back();
in_degree[po] = tmp;
vis[po] = false;
for (int j = 0; j < len; j++){
in_degree[adj_table[po][j]] ++;
}
}
}
return;
}
vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) {
in_degree = vector<int>(numCourses, 0);//包括孤立点在内,in_degree=0
adj_table.resize(numCourses);
vector<pair<int, int>>::iterator it = prerequisites.begin();
while (it != prerequisites.end())
{
in_degree[it->second] ++;
adj_table[it->first].push_back(it->second);
++it;
}
n = numCourses;
vis = vector<bool>(numCourses, false);
flag = false;
top(n);
if (flag)
return rs;
return vector<int>();
}
};