标签:技术总结 拓扑排序 stream cto 个数 empty 关于 code 序列
vector<int> G[MAXV];
int n, m, in[MAXV];
bool topologicalSort(){
int num = 0//用于记录进入结点的次数是否为结点的个数
queue<int> q;
for(int i = 0; i < n; i++){
if(in[i] == 0){
q.push(i);
}
}
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = 0; i < G[u].size(); i++){
int v = G[u][i];
in[v]--;
if(in[v] == 0){
q.push(v);
}
}
G[u].clear();
num++;
}
if(num == n) return true;
else return false;
}
#include<iostream>
#include<vector>
using namespace std;
//bool topologicalSort(vector<int> G[], int inDegree[]){
// return;
//}
int main(){
int m, n, a, b, c, flag = 0;
scanf("%d%d", &n, &m);
//vector<vector<int> > v(n);//用于存储图
vector<int> v[1010];
int inDegree[1010];//记录结点的入度
for(int i = 0; i < m; i++){
scanf("%d%d", &a, &b);
v[a].push_back(b);
inDegree[b]++;
}
int k;
scanf("%d", &k);
for(int i = 0; i < k; i++){
int judge = 1;
vector<int> tin(inDegree, inDegree+n+1);
for(int j = 0; j < n; j++){
scanf("%d", &c);
if(tin[c] != 0) judge = 0;
//for(int l = 0; l < v[c].size(); l++) tin[v[c][l]]--;
for (int it : v[c]) tin[it]--;
}
if(judge == 1) continue;
printf("%s%d", flag == 1 ? " " : "", i);
flag = 1;
}
return 0;
}
A 1146 Topological Order (25分)(拓扑排序)
标签:技术总结 拓扑排序 stream cto 个数 empty 关于 code 序列
原文地址:https://www.cnblogs.com/tsruixi/p/13170177.html