POJ 2367 拓扑排序
来源:互联网 发布:亨德尔 水上音乐 知乎 编辑:程序博客网 时间:2024/06/06 01:33
2367为了找到没有长辈的,或者长辈已经在列表中的人,只需要用拓扑排序就可以很快速地解决
拓扑排序我借鉴了wiki上Kahn的伪代码:
L← Empty list that will contain the sorted elementsS ← Set of all nodes with no incoming edgeswhile S is non-empty do remove a node n from S insert n into L foreach node m with an edge e from nto m do remove edge e from thegraph ifm has no other incoming edges then insert m into Sif graph has edges then return error (graph has at least onecycle)else return L (a topologically sortedorder)
下面是我写的程序的源码
#include <iostream>#include <cmath>#include <cstdio>#include <string>#include <vector>#include <cstdlib>#include <algorithm>#include <stack>#include <queue>#include <list>#include <set>#include <map>#include <climits>/* INT_MAX LONG_MAX LLONE_MIN INT_MIN ULLONG_MIN*/using namespace std;typedef long long ll;int n;list<int > children[110];int indegree[110];void init(){ for(int i=0;i<110;i++){ children[i].clear(); indegree[i]=0; } for(int i=1;i<=n;i++){ int j=1; while(1){ scanf("%d",&j); if(!j)break; indegree[j]++; children[i].push_back(j); } }}void kahn(){ queue<int > noFather; for(int i=1;i<=n;i++) if(!indegree[i])noFather.push(i); list<int > ans; while(!noFather.empty()){ int node=noFather.front(); noFather.pop(); ans.push_back(node); for(list<int>::iterator it= children[node].begin(); it!=children[node].end();it++){ if(!(--indegree[*it]))noFather.push(*it); } children[node].clear(); } for(list<int>::iterator it= ans.begin(); it!=ans.end();it++) printf("%d ",*it);}int main(){ while(cin>>n){ init(); kahn(); } return 0;}
0 0
- poj 2367 拓扑排序
- poj 2367 拓扑排序
- poj 2367 拓扑排序
- POJ 2367 拓扑排序
- POJ 2367 拓扑排序
- poj 2367拓扑排序
- poj 2367 拓扑排序
- 拓扑排序---(poj 2367)
- poj 2367(拓扑排序TopSort)
- 拓扑排序(poj 2367)
- POJ 2367 Genealogical tree 拓扑排序
- POJ 2367 Genealogical tree [拓扑排序]
- Poj 2367 Genealogical tree(拓扑排序)
- 【POJ】2367 Genealogical tree 拓扑排序
- POJ 2367:Genealogical tree(拓扑排序)
- POJ 2367 Genealogical tree 拓扑排序入门
- POJ 2367 Genealogical tree 拓扑排序
- POJ 2367 Genealogical tree(拓扑排序)
- 重要的开源资源及50个c/c++源代码网站
- 前端面试 - Http Session问题,记录我愚蠢的回答=。=
- C++第五章作业
- 正则表达式
- Android 实现监听开机启动开启后台服务,并实现自动重启。
- POJ 2367 拓扑排序
- 第十四周项目1-3:统计字符个数
- 机器学习算法中如何选取超参数:学习速率、正则项系数、minibatch size
- Java堆,方法区,Java栈和本地方法栈浅析
- 一道题目理解js中变量在作用域链与原型链中的查找顺序
- hihocode 第五十周 欧拉路·二
- watch-3D.app开发经验(1)
- android 软键盘在全屏下设置adjustResize无效的问题
- iBokan_ios80_自学系列_1(~循环结构~)