拓扑排序模板
2016-02-22 17:40
357 查看
#include<cstdio> #include<cstring> #include<stack> using namespace std; struct edge{ int w,next; }e[10000]; stack <int> s; int n,m,cnt=1,ans; int head[1000],d[1000],f[1000]; void adde(int u,int w) { e[cnt].w=w; e[cnt].next=head[u]; head[u]=cnt++; d[w]++; } bool topsort() { for(int i=1;i<=n;i++) if(!d[i]) s.push(i); while(!s.empty()) { int k=s.top();s.pop(); ans++; f[ans]=k; for(int u=head[k];u!=-1;u=e[u].next) { d[e[u].w]--; if(!d[e[u].w]) s.push(e[u].w); } } if(ans==n) return 1; else return 0; } int main() { int u,w,v; memset(head,-1,sizeof(head)); scanf("%d%d",&n,&m); for(int i=1;i<=m;i++)//建图 { scanf("%d%d",&u,&w); adde(u,w); } if(topsort()) { for(int i=1;i<=ans;i++) printf("%d ",f[i]); } return 0; }
相关文章推荐
- is_balance函数优化
- hdu3280Equal Sum Partitions (区间DP)
- 变频器联网控制
- Android平台中关于音频播放
- Jenkins+Maven+SVN快速搭建持续集成环境(转)
- New Activity探索(序)
- ps切图(1)——界面设置
- java数组与容器
- instancetype、id、NSObject的区别
- HDU 4006 The kth great number(优先队列)
- web学习之路—H5(五)
- 第9章协同程序
- Boost智能指针——shared_ptr
- article元素
- boost for android
- 海啸的数学模型
- 大坑 ------被String == 坑了
- Notification屏蔽下拉菜单具体消息
- 实现内核驱动程序模块
- 工欲善其事——Sublime Text