題目地址:點擊打開鏈接
拓撲排序
#include#include #include #include using namespace std; const int maxsize = 110; int graph[maxsize][maxsize]; int inDegree[maxsize]; int main() { int n,m; while(cin>>n>>m&&(n||m)) { memset(graph,0,sizeof(graph)); memset(inDegree,0,sizeof(inDegree)); int i; for(i=0;i >a>>b; graph[a][b]=1; ++inDegree[b]; } deque di; for(i=1;i<=n;++i) { if(inDegree[i]==0) di.push_back(i); } vector p; while(!di.empty()) { int x=di.front(); di.pop_front(); p.push_back(x); for(i=1;i<=n;++i) { if(graph[x][i]!=0) { graph[x][i]=0; if(--inDegree[i]==0) di.push_back(i); } } } for(i=0;i