裸拓扑排序。

拓扑排序
用一个队列实现,先把入度为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 (拓扑排序)的更多相关文章

  1. [poj2367]Genealogical tree_拓扑排序

    Genealogical tree poj-2367 题目大意:给你一个n个点关系网,求任意一个满足这个关系网的序列,使得前者是后者的上级. 注释:1<=n<=100. 想法:刚刚学习to ...

  2. timus 1022 Genealogical Tree(拓扑排序)

    Genealogical Tree Time limit: 1.0 secondMemory limit: 64 MB Background The system of Martians’ blood ...

  3. POJ 2367 Genealogical tree 拓扑排序入门题

    Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8003   Accepted: 5184 ...

  4. Poj 2367 Genealogical tree(拓扑排序)

    题目:火星人的血缘关系,简单拓扑排序.很久没用邻接表了,这里复习一下. import java.util.Scanner; class edge { int val; edge next; } pub ...

  5. poj2367 Genealogical tree

    思路: 拓扑排序,这里是用染色的dfs实现的.在有环的情况下可以判断出来,没有环的情况下输出拓扑排序序列. 实现: #include <vector> #include <cstri ...

  6. POJ 2367 Genealogical tree 拓扑题解

    一条标准的拓扑题解. 我这里的做法就是: 保存单亲节点作为邻接表的邻接点,这样就非常方便能够查找到那些点是没有单亲的节点,那么就能够输出该节点了. 详细实现的方法有非常多种的,比方记录每一个节点的入度 ...

  7. 【拓扑排序】Genealogical tree

    [POJ2367]Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5696   Accep ...

  8. POJ 2367:Genealogical tree(拓扑排序模板)

    Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7285   Accepted: 4704 ...

  9. poj 2367 Genealogical tree【拓扑排序输出可行解】

    Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3674   Accepted: 2445 ...

随机推荐

  1. uvalive 4851 Restaurant(扫描法)

    题意:有一个M*M的网格,坐标[0...M-1,0...M-1] 网格里面有两个y坐标同样的宾馆A和B.以及n个餐厅,宾馆AB里面各有一个餐厅,编号1,2,其它餐厅编号3-n.如今你打算新开一家餐厅. ...

  2. KVM硬件辅助虚拟化之 EPT(Extended Page Table)

    传统OS环境中,CPU对内存的訪问都必须通过MMU将虚拟地址VA转换为物理地址PA从而得到真正的Physical Memory Access,即:VA->MMU->PA,见下图. 虚拟执行 ...

  3. 使用const 提高函数的健壮性

    1.如果输入参数采用“指针传递”,那么加const 修饰可以防止意外地改动该指针指向的内存单元,起到保护的作用. 例如:void StringCopy(char *strDest, const cha ...

  4. USB HID Report Descriptor 报告描述符详解

    Report descriptors are composed of pieces of information. Each piece of information is called an Ite ...

  5. Draggable(拖动)组件

    一.加载方式 //class 加载方式 <div id="box" class="easyui-draggable" style="width: ...

  6. document.onclick vs window.onclick

    The JavaScript Window object is the highest level JavaScript object which corresponds to the web bro ...

  7. 关于为什么RAID5往往掉一个盘后第二个盘也立刻挂掉的原因分析

    很多人遇到过服务器RAID5挂掉,往往掉一个盘后,第二个盘也立刻挂掉. 大家都知道RAID5 一次允许一个盘缺失, RAID 5也是以数据的校验位来保证数据的安全,但它不是以单独硬盘来存放数据的校验位 ...

  8. C#中的switch case

    在C#中switch(type){case tpye1:break;case tpye2:break;case tpye3:break;case tpye4:break;};其中type可以是数字,也 ...

  9. 读取oracle页面或者进程卡住不动(死锁)

    oracle最坑爹的地方:你insert   update  delete之后  或者kill死锁的时候记得一定要提交事务不然就是死锁卡在那里了 记住  kill死锁也是要提交事务的 select * ...

  10. Jenkins学习之——(2)插件的安装

    本章节将讲解如何安装jenkins的插件. 其实jenkins本身不具有任何集成的功能,而是依靠众多的插件实现功能.就像eclipse一样,期本身只是一个编辑器,而当你安装了其他的第三方插件后,就能实 ...