标签:
5 0 4 5 1 0 1 0 5 3 0 3 0
2 4 5 3 1
求拓扑序列,拓扑排序
#include<iostream> #include<cstdio> #define N 110 using namespace std; struct edge_node { int to,next; }e[N*N]; int head[N]; int rudu[N]; int q[N]; int n,cnt,h,t; int main() { scanf("%d",&n); for (int u=1;u<=n;u++) { int v; for (;scanf("%d",&v) && v;) { e[++cnt].to = v; e[cnt].next = head[u]; head[u] = cnt; rudu[v] ++; } } for (int i=1;i<=n;i++) if (rudu[i] == 0) q[++t] = i; for (;h<t;) { int u = q[++h]; for (int i = head[u];i;i = e[i].next) { int v = e[i].to; rudu[v] -- ; if (!rudu[v]) q[++t] = v; } printf("%d ",u); } }
标签:
原文地址:http://www.cnblogs.com/liumengyue/p/5495523.html