思路:

拓扑排序,这里是用染色的dfs实现的。在有环的情况下可以判断出来,没有环的情况下输出拓扑排序序列。

实现:

 #include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#define w 0
#define g 1
#define b 2
using namespace std;
const int N = ;
int G[N][N];
vector<int> res, vis;
bool cycle;
int n, m;
void init()
{
cycle = false;
cin >> n;
memset(G, , sizeof G);
for (int i = ; i <= n; i++) vis.push_back(w);
int x;
for (int i = ; i <= n; i++)
{
while (cin >> x, x)
{
G[i][x] = ;
}
}
}
void dfs(int u)
{
if (vis[u] == b) return;
if (vis[u] == g) { cycle = true; return; }
vis[u] = g;
for (int i = n; i >= ; i--)
{
if (G[u][i]) dfs(i);
}
vis[u] = b;
res.push_back(u);
return;
}
void topsort()
{
for (int i = n; i >= ; i--)
{
if (vis[i] == w) dfs(i);
}
reverse(res.begin(), res.end());
}
int main()
{
init();
topsort();
if (cycle) cout << "There is a cycle in G!" << endl;
else
{
for (int i = ; i < res.size(); i++)
cout << res[i] << " ";
cout << endl;
}
return ;
}

poj2367 Genealogical tree的更多相关文章

  1. POJ2367 Genealogical tree (拓扑排序)

    裸拓扑排序. 拓扑排序 用一个队列实现,先把入度为0的点放入队列.然后考虑不断在图中删除队列中的点,每次删除一个点会产生一些新的入度为0的点.把这些点插入队列. 注意:有向无环图 g[] : g[i] ...

  2. 【拓扑排序】Genealogical tree

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

  3. [poj2367]Genealogical tree_拓扑排序

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

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

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

  5. poj 2367 Genealogical tree

    题目连接 http://poj.org/problem?id=2367 Genealogical tree Description The system of Martians' blood rela ...

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

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

  7. Genealogical tree(拓扑结构+邻接表+优先队列)

    Genealogical tree Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) ...

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

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

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

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

随机推荐

  1. windows 7 忘記密碼,用“带命令行的安全模式”

    net user administrator /active:yes net user tester /add net localgroup administrators tester /add

  2. python搭建web server

    假设你急需一个简单的Web Server,但你又不想去下载并安装那些复杂的HTTP服务程序,比方:Apache,ISS等.那么, Python 可能帮助你.使用Python能够完毕一个简单的内建 HT ...

  3. mysql查看所有存储过程,函数,视图,触发器,表

    查询数据库中的存储过程和函数 方法一: select `name` from mysql.proc where db = 'your_db_name' and `type` = 'PROCEDURE' ...

  4. 剑指offer面试题24-二叉搜索树的后序遍历序列

    题目: /*  * 输入一个整数数组,推断该数组是不是某二叉搜索树的兴许遍历的结果.<br/>  * 假设是则返回true,否则返回false.<br/>  * 如果输入的数组 ...

  5. 【hdu3518】Boring counting

    题意:找出一个字符串中至少重复出现两次的字串的个数(重复出现时不能重叠). 后缀数组 枚举字串长度h,对于每一次的h,利用height数组,找出连续的height大于等于h的里面最左端和最右端得为之l ...

  6. oss

    import oss2 ''' auth = oss2.Auth('您的AccessKeyId', '您的AccessKeySecret') bucket = oss2.Bucket(auth, '您 ...

  7. HDU 1379:DNA Sorting

    DNA Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  8. 推理集 —— death

    事故: 自杀: 他杀: 1. 跳楼 头向下死得比较快,没那么痛苦. 脚向下,不会立刻死亡,痛苦至极.死亡原因可能不是跳楼,而是失血过多而死 扑下去, 同头向下. 仰着跌下去,同头向下.. 跳楼最好头先 ...

  9. 【SDOI 2008】 递归数列

    [题目链接] 点击打开链接 [算法] 矩阵乘法优化递推 由于本博客不支持数学公式,所以不能将矩阵画出来,请谅解! [代码] #include<bits/stdc++.h> using na ...

  10. varnish的架构和日志

    varnish的架构和日志 varnish的架构 知道varnish的内部结构有两个重要的原因: 首先,架构主要负责性能,其次,它影响你如何将Varnish集成到你自己的架构中. 主程序块是Manag ...