分析
拓扑序列的核心思想在于调整入度,每次输出的都是入度为0的点
因此找个数组专门保存入度
进一步分析
通过vector容器以便传值,而非用传统数组,从而达成多次判断
代码
#include<bits/stdc++.h>
using namespace std;
vector<int>print, * graph;
int vertex, edge, quantity, x, y;
bool Judge(vector<int>);
int main() {
scanf("%d %d", &vertex, &edge);
vector<int> indegree;
indegree.resize(vertex + 1);
fill(indegree.begin(), indegree.end(), 0);
graph = new vector<int>[vertex + 1];
int x, y;
for (int i = 0; i < edge; i++) {
scanf("%d %d", &x, &y);
graph[x].push_back(y);
indegree[y]++;
}
scanf("%d", &quantity);
for (int i = 0; i < quantity; i++) {
if (!Judge(indegree))
print.push_back(i);
}
bool first{ true };
for (auto j : print)
first ? printf("%d",j), first = false : printf(" %d",j);
return 0;
}
bool Judge(vector<int> indegree) {
bool flag{ true };
for (int i = 0; i < vertex; i++) {
scanf("%d", &x);
if (flag == true) {
if (indegree[x] == 0) {
for (auto j : graph[x])
indegree[j]--;
}
else
flag = false;
}
}
if (flag)
return true;
else
return false;
}