本文共 905 字,大约阅读时间需要 3 分钟。
#include#include #include #include #include using namespace std;const int maxn = 20010;//ip表示第几条边//indeg表示入度int head[maxn], ip, indeg[maxn];int n, m, seq[maxn];//seg表示要干的事struct note{ int v,next;} edge[maxn];void Init(){ memset(head, -1, sizeof(head)); memset(indeg, 0, sizeof(indeg)); ip = 0;}void addedge(int u, int v)//增加边,u是初始点,v是终点{ edge[ip].v = v; edge[ip].next = head[u]; head[u] = ip++;}int topo()//拓扑排序{ queue q; for(int i=1; i<=n; i++) if(indeg[i] == 0) q.push(i); int k = 0; bool res = false; while(!q.empty()) { if(q.size() != 1) res = true; int u = q.front(); q.pop(); k++; for(int i=head[u]; i!=-1; i=edge[i].next) { int v = edge[i].v; indeg[v]--; if(indeg[v] == 0) { //seq[v] = seq[u]+1; q.push(v); } } } if(k < n) return -1;//No if(res) return 0;//Only return 1;//正常}
转载地址:http://gzdso.baihongyu.com/