裸拓扑排序。

拓扑排序
用一个队列实现,先把入度为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. 【转】windows下vs2008/2010+opencv2.2开发环境搭建

    版权声明:本文为博主原创文章,未经博主允许不得转载. 1.下载安装Cmake 2.用cmake配置opencv2.2,然后编译,安装 3. 在vs2008中配置opencv2.2 4.Demo 1.下 ...

  2. C#request 请求响应

    /// <summary> /// 提交POST请求 /// </summary> /// <param name="url">提交地址< ...

  3. 关于退运美国转基因玉米含有MRI 162转基因成分的质疑

    6月30日,新华社刊出文章"我国退运125.2万吨进口美国转基因玉米",读后有感. 文章说:国家质检总局办公厅副主任陆春明30日介绍,截至今年6月16日,全国出入境检验检疫机构共在 ...

  4. JSTL配合正则表达式在JSP中的应用

    <%@ page language="java" import="java.util.*,cn.com.Person" pageEncoding=&quo ...

  5. [React] React Router: Named Components

    In this lesson we'll learn how to render multiple component children from a single route. Define a n ...

  6. mysql索引简单介绍

    索引从本质上来说也是一种表,这样的表存储被列为索引的列项值和指向真正完整记录的指针.索引对用户透明.仅仅被数据库引擎用来加速检索真实记录.有索引的表.insert和update操作会耗费很多其它时间而 ...

  7. 数据视化Echarts+百度地图API实现市县区级下钻

    开始 这两天公司有个页面需要做数据可视化的展示,数据视化采用的是Echarts+百度地图API做展示,需要用到县级区级下钻的一个联动效果发现网上关于Echarts做到县区级下钻的资料很少,有的话也不是 ...

  8. (转)Phonegap VS AppCan

    简介 Phonegap PhoneGap是一个用基于HTML,CSS和JavaScript的,创建移动跨平台移动应用程序的快速开发平台.它使开发者能够利用iPhone,Android,Palm,Sym ...

  9. (转)SQL Server 2005附加2008的数据库

    1. 生成for 2005版本的数据库脚本  2008 的manger studio  -- 打开"对象资源管理器"(没有的话按F8), 连接到你的实例  -- 右键要转到2005 ...

  10. java比较器Comparator 使用

    PresonDemo package cn.stat.p5.person.demo; public class PresonDemo implements Comparable { private S ...