POJ2367 Genealogical tree (拓扑排序)
来源:互联网 发布:淘宝快递费用价格表 编辑:程序博客网 时间:2024/05/20 18:40
裸拓扑排序。
拓扑排序
用一个队列实现,先把入度为0的点放入队列。然后考虑不断在图中删除队列中的点,每次删除一个点会产生一些新的入度为0的点。把这些点插入队列。
注意:有向无环图
g[] : g[i]表示从点i连出去的边
L[] :拓扑排序的结构
code:
#include <cstdio>#include <cstring>#include <queue>using namespace std;const int maxn = 100 + 5;vector<int> g[maxn];int du[maxn], n, m, L[maxn];bool toposort(){ memset(du, 0, sizeof du ); for(int i=0; i<n; ++i) for(int j=0; j<g[i].size(); ++j) du[g[i][j]]++; int tot = 0; queue<int> Q; for(int i=0; i<n; ++i) if(!du[i]) Q.push(i); while(!Q.empty()) { int x = Q.front(); Q.pop(); L[tot++] = x+1; for(int j=0; j<g[x].size(); ++j) { int t = g[x][j]; du[t]--; if(!du[t]) Q.push(t); } } if(tot == n) return 1; else return 0;}int main(){ int x, i; while(~scanf("%d",&n)) { for(int i=0; i<n; ++i) { g[i].clear(); while(scanf("%d",&x),x) { g[i].push_back(x-1); } } if(toposort()) { for(i=0; i<n-1; ++i) { printf("%d ",L[i]); } printf("%d\n",L[i]); } } return 0;}
- POJ2367 Genealogical tree (拓扑排序)
- POJ2367 Genealogical tree(拓扑排序)
- poj2367 Genealogical tree【拓扑排序】
- poj2367 Genealogical tree 拓扑排序初接触
- (拓扑排序)poj2367,Genealogical tree
- poj2367 Genealogical tree(拓扑排序:输出方案)
- poj2367 Genealogical tree (裸拓扑排序)
- 拓扑入门 poj2367 Genealogical tree
- POJ2367——Genealogical tree(拓扑排序模板)
- [学习][poj2367]拓扑序 Genealogical tree
- Genealogical tree 拓扑排序
- POJ2367 Genealogical tree
- poj2367 Genealogical tree
- POJ2367--Genealogical tree
- poj2367 Genealogical tree
- poj2367 Genealogical tree
- <poj2367>Genealogical tree
- 【POJ2367】Genealogical tree
- VC中调用EXECL模板生成新的Excel文档
- 线程的状态转换图
- CSS中常用中文字体转Unicode编码表
- java支付宝支付成功但是验证失败AlipayNotify.returnverify
- 百度IOS SDK若干问题解决办法
- POJ2367 Genealogical tree (拓扑排序)
- 分享三个iframe自适应高度的例子
- NoSql大比拼--方案
- WCF的配置Web.config及发布需要注意的问题
- ADF页面中嵌入javascript有两种方式
- 全球十大长寿食物排行榜
- 我是新成员
- 税收征收管理法律制度(重点考点)
- eclipse显示jar源代码中文乱码问题