裸拓扑排序。

拓扑排序
用一个队列实现,先把入度为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. 冷门却使用的 javascript 技巧

    前端已经被玩儿坏了!像用近似乱码的 javascript 拼一个图形,并且能够正常执行等炫酷的玩意已经不是什么新闻了,像用||操作符给变量赋默认值也是人尽皆知的旧闻了,今天看到Quora上一个帖子,瞬 ...

  2. C语言文件详解

    1.C语言FILE类,在stdio.h头文件中,FILE类是一个结构体:定义如下: 通过typedef定义了 文件类型 的别名: “FILE”,这样以后需要读写文件的时候直接定义FILE就行了.   ...

  3. [RxJS] Observables can throw errors

    Whenever we are writing code, we need to remember that things may go wrong. If an error happens in a ...

  4. [ES6] WeakMap vs Map

    WeakMap: is a type of Map where only objects can be passed as keys. Primitive data type -- such are ...

  5. IOS成长之路-Nsstring中搜索方法rangeOfString

    NSString *str1 = @"can you \n speak English"; NSString *str = @"\n"; //在str1这个字符 ...

  6. HashMap学习笔记

        概述   HashMap是Map接口的一个哈希表的实现,内部是一个数组表示的.数组中的元素叫做一个Node,一个Node可以一个是一个简单的表示键值对的二元组,也可以是一个复杂的TreeNod ...

  7. MVC Controller 基类中的Request

    今天在测试自己MVC程序的时候发现之前写代码的一个BUG,需求是每个页面要获取当前URL链接中包含的城市ID,我把获取url的方法写到了Controller的基类BaseController(Base ...

  8. 实现类似QQ的折叠效果

    //  主要核心是点击自定义header来展开和收起每一组里面的cell,模型里面应该有isShow此属性来记录开展还是收起. //  ViewController.m//  实现类似QQ的折叠效果/ ...

  9. angularjs中关于ng-if的一些理论

    ng-if简介: ● 使用ng-if指令可以完全根据表达式的值在DOM中生成或移除一个元素.如果赋值给ng-if 的表达式的值是false,那对应的元素将会从DOM中移除,否则对应元素的一个克隆将被重 ...

  10. uva 10038 - Jolly Jumpers

    #include <iostream> #include <cstdio> #include <stdlib.h> using namespace std; ], ...