标签:topo排序
4 3 1 2 2 3 4 3
1 2 4 3
题解:topo排序 觉得没什么好说的。。代码加了注释 有问题看注释好了
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int maxn = 511; int n, m; //n为顶点个数 int gra[maxn][maxn]; //邻接阵 int degree[maxn]; //记录顶点的入度 int main(){ while(scanf("%d%d", &n, &m)!=EOF){ memset(gra, 0, sizeof(gra)); memset(degree, 0, sizeof(degree)); int p1, p2; for(int i = 0; i < m; ++i){ scanf("%d%d", &p1, &p2); if(!gra[p1][p2]){ gra[p1][p2] = 1; ++degree[p2]; //入度+1 } } for(int i = 1; i <= n; ++i){ //遍历n次每次找出一个顶点 for(int j = 1; j <= n; ++j){ //遍历所有的结点 if(degree[j] == 0){ --degree[j]; //该顶点的入度为-1,防止该顶点被在此遍历到 if(i != n){ printf("%d ", j); } else{ printf("%d\n", j); } for(int k = 1; k <= n; ++k){ if(gra[j][k] == 1){ --degree[k]; //与该顶点关联的顶点的入度递减 } } break; } } } } return 0; }
标签:topo排序
原文地址:http://blog.csdn.net/u012431590/article/details/46053525