POJ2367 Genealogical tree (拓扑排序)
裸拓扑排序。
拓扑排序
用一个队列实现,先把入度为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_拓扑排序
Genealogical tree poj-2367 题目大意:给你一个n个点关系网,求任意一个满足这个关系网的序列,使得前者是后者的上级. 注释:1<=n<=100. 想法:刚刚学习to ...
- timus 1022 Genealogical Tree(拓扑排序)
Genealogical Tree Time limit: 1.0 secondMemory limit: 64 MB Background The system of Martians’ blood ...
- POJ 2367 Genealogical tree 拓扑排序入门题
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8003 Accepted: 5184 ...
- Poj 2367 Genealogical tree(拓扑排序)
题目:火星人的血缘关系,简单拓扑排序.很久没用邻接表了,这里复习一下. import java.util.Scanner; class edge { int val; edge next; } pub ...
- poj2367 Genealogical tree
思路: 拓扑排序,这里是用染色的dfs实现的.在有环的情况下可以判断出来,没有环的情况下输出拓扑排序序列. 实现: #include <vector> #include <cstri ...
- POJ 2367 Genealogical tree 拓扑题解
一条标准的拓扑题解. 我这里的做法就是: 保存单亲节点作为邻接表的邻接点,这样就非常方便能够查找到那些点是没有单亲的节点,那么就能够输出该节点了. 详细实现的方法有非常多种的,比方记录每一个节点的入度 ...
- 【拓扑排序】Genealogical tree
[POJ2367]Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5696 Accep ...
- POJ 2367:Genealogical tree(拓扑排序模板)
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7285 Accepted: 4704 ...
- poj 2367 Genealogical tree【拓扑排序输出可行解】
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3674 Accepted: 2445 ...
随机推荐
- less.css基础学习,陆续更新中
//基础//概念:动态样式语言,有很多语言的特性:变量,函数,运算等 //变量:通过一个简单的@+字母,数字下划线等,但不能以数字开头,不能关键字,保留字等//注意less.css是全局变量,除在函数 ...
- [深入React] 6.组件
组件是react的大杀器,超出其他框架几百里 react 组件和dom一样也是树状结构,只能由上而下传递变量(或者调用),不可以兄弟间或者更远的发生关系,为的就是简单,而且工作的很好. 每个组件有自己 ...
- [ES6] When should use Map instead of Object
Use Maps when keys are unknown until runtime: Map: let recentPosts = new Map(); createPost( newPost, ...
- [AngularJS] Using AngularJS's ngClass
.blue{ color: blue } .bold{ font-weight: bold; } .large{ font-size: 40px; } ngClass can accept an ar ...
- 设计模式(3)-对象创建型模式-Abstract Factory模式
1.对象创建型模式 1.3 Abstract Factory模式 1.3.1 需求 在下面情况能够使用Abstract Factory模式: • 一个系统要独立于它的产品的创建. ...
- 详解C++ friend关键字
1. 为什么要使用友元? 通常对于普通函数来说,要访问类的保护成员是不可能的,如果想这么做那么必须把类的成员都生命成为 public( 共用的) ,然而这做带来的问题遍是任何外部函数都可以毫无约束的访 ...
- linux 下文件的比较
1.cmp命令,比较两个文件是否相同 比较文件test1和test2,如果这两个文件完全相同,则无任何输出,否则,输出第一处不同所在的字节以及行号,然后忽略后面的不同之处,退出命令的执行. [root ...
- mybati之运行过程
mybatis其实就只有两个配置文件(mybatis-config.xml与mapper.xml) mybatis-config.xml配置基本的数据,和数据源,全局参数 mapper.xml 多个s ...
- 代码段编辑器SnippetEditor 2.1
1.选择程序版本 2.可以创建文件夹 3.新建片段 4.给片段取名 5.双击进行编辑 6.点击保存 7.直接使用
- 《第一行代码》学习笔记35-服务Service(2)
1.Android的UI线程不安全,想要更新应用程序里的UI元素,则须在主线程中进行,否则会出现异常. 2.Android不允许在子线程里进行UI操作,对于该情况,Android提供了一套异步消息处理 ...