hdu1285(拓扑排序)
2015-08-14 23:07
239 查看
这道题要求没有输赢关系的两个元素必须按照升序输出,有输赢关系的,赢得在输的前面,所以用队列或者栈来降低时间复杂度的优化过的拓扑排序会出错。
比如这组输入
5 3
1 2
2 3
4 5
至少我写的两种拓扑排序都wa了。但是不用队列或者栈来优化的话,
1.每次都从头至尾扫描一遍,找到一个没标记过的节点,
2.将它标记
3.然后删除从它出来的每条边。
重复这三个操作,加标记的次序,就是题目要的答案。
下面的代码中用到了队列,但只是用来保存答案而已。并没有用它优化的意思。
比如这组输入
5 3
1 2
2 3
4 5
至少我写的两种拓扑排序都wa了。但是不用队列或者栈来优化的话,
1.每次都从头至尾扫描一遍,找到一个没标记过的节点,
2.将它标记
3.然后删除从它出来的每条边。
重复这三个操作,加标记的次序,就是题目要的答案。
下面的代码中用到了队列,但只是用来保存答案而已。并没有用它优化的意思。
#include <iostream> #include <vector> #include <queue> #include <stdio.h> #include <memory.h> #include <stack> using namespace std; int counter[505]; bool vis[505]; queue<int> ans; int N,M; vector<vector<int> > v; bool TopoOrder() { for(int i=0;i<N;i++) { int j=1; while(vis[j]||counter[j]!=0) j++; vis[j]=true; ans.push(j); for(int k=0;k<v[j].size();k++) counter[v[j][k]]--; } return true; } void output() { bool isbegin=true; while(!ans.empty()) { if(isbegin) { isbegin=false; cout<<ans.front(); } else cout<<' '<<ans.front(); ans.pop(); } cout<<endl; } int main() { int a,b; while(~scanf("%d%d",&N,&M)) { memset(counter,0,sizeof(counter)); memset(vis,false,sizeof(vis)); v.clear(); v.resize(N+1); for(int i=0;i<M;i++) { scanf("%d%d",&a,&b); v[a].push_back(b); counter[b]++; } TopoOrder(); output(); } return 0; }
相关文章推荐
- 九度 Online Judge 算法 刷题 题目1087:约数的个数
- ANT介绍及ANT系统环境变量的配置
- RPC与REST的区别
- 【JS】<a>标签调用js中函数的几种方法
- 黑马程序员Java笔记——集合篇(二)
- JAVA 23 种设计模式
- iphone之打开pdf、doc、xls文件用UIWebView
- 2015-08-14
- 第43讲:Scala中类型变量Bounds代码实战及其在Spark中的应用源码解析学习笔记
- 九度 Online Judge 算法 刷题 题目1083:特殊乘法
- 关于C++虚函数表的那些事儿
- 单例模式
- MAX7456 OSD
- Struts之mvc设计模式
- 【Matrix Factorization】林轩田机器学习技法
- 【JSP】JSP动态显示时间
- shell 分割字符串存至数组
- poj 2945 Find the Clones (map+string,hash思维)
- 九度 Online Judge 算法 刷题 题目1075:斐波那契数列
- oracle用户操作