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 ...
随机推荐
- Spring 基于注解零配置开发
本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:< Spring 基于注解零配置开发 > 一:搜索Bean 再也不用在XML文件里写什么配置信息了. Sprin ...
- cavium octeon 处理器启动总线Bootbus 简介
cavium octeon 处理器启动总线Bootbus 简介: 韩大卫@吉林师范大学 Boot-bus(启动总线)是cavium octeon处理器的一种用于启动系统的硬件. CPU通过boot b ...
- C语言--对数组地址的解析
在C编程中,我们进程会用到数组,这看起来很简单,因为,数组就是存储相同类型元素的集合嘛,不过,当你还没考虑到数组的地址问题时,一切都是简单的,如果你接触了数组中的地址概念,也许你会改变你的想法. 下面 ...
- Android设备中实现Orientation Sensor(图)兼谈陀螺仪
设备中的三自由度Orientation Sensor就是一个可以识别设备相对于地面,绕x.y.z轴转动角度的感应器(自己的理解,不够严谨).智能手机,平板电脑有了它,可以实现很多好玩的应用,比如说指南 ...
- Xshell 4的上传与下载
http://blog.csdn.net/lioncode/article/details/7921525
- Mysql相关操作
1. 如何更改系统环境变量PATH?vim /etc/profile 加入 PATH=$PATH:/usr/local/mysql/bin2. 默认mysql安装好后,并没有root密码,如何给ro ...
- 读取xml文件信息
static void Main(string[] args) { XmlTextReader reader = new XmlTextReader(@"D:\zy\visual studi ...
- Js遍历Josn对象(内容对比页实现思路)
更好的遍历Josn的方法,利用jquery的each方法: var arr1 = [ "one", "two", "three", &quo ...
- (转)C# DateTime格式化大全
//c datetime 格式化 DateTime dt = DateTime.Now; Label1.Text = dt.ToString();//2005-11-5 13:21:25 Label2 ...
- O、Ω、Θ表示
转载,原网址为:http://book.2cto.com/201211/8127.html 对于任何数学函数,这三个记号可以用来度量其“渐近表现”,即当趋于无穷大时的阶的情况,这是算法分析中非常重要的 ...