思路:

拓扑排序,这里是用染色的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. sqlit中使用到的查询语句

    近期使用sqlite查询比較多,包含连表查询等. 记录一下.以免忘记! 1.先依据时间排序后选择前十条: select * from MyBill order by  createTime desc ...

  2. dataTables 添加行内操作按钮

    在上一篇博客中我们提到了用dataTables 做一个分页表格.今天进一步进阶,做一个行内带操作按钮的表格.效果如图. 记得最基础的实现方式是,我们要在js 中拼接字符串,嵌入一个带按钮的语句.但是现 ...

  3. 【nginx】nginx与apache的优缺点比较

    参考: http://zyan.cc/nginx_php_v6/ nginx相对于apache的优点: 1.轻量级,同样的web 服务,比apache服务器占用更少的内存及资源 2.抗并发,nginx ...

  4. Node.js安装及环境配置之Windows篇(转:https://www.cnblogs.com/zhouyu2017/p/6485265.html)

    Node.js安装及环境配置之Windows篇(原文地址:https://www.cnblogs.com/zhouyu2017/p/6485265.html)   一.安装环境 1.本机系统:Wind ...

  5. Vue中删除重复上传的文件

    上传控件: <el-upload class="upload-demo"  :on-change="filesChange"> filesChang ...

  6. C#.NET 如何在系统变量中加入新的环境变量

    比如我要将C:\Windows\Microsoft.NET\Framework\v3.5这个目录加入环境变量 则在系统的环境变量中点击Path,编辑,然后加入一个分号";",然后粘 ...

  7. visio中怎样画线条或箭头

    1.在"画图"工具栏上,单击"铅笔"工具  或"线条"工具  . (凝视   假设看不到"画图"工具栏,请单击" ...

  8. HDU-3295-An interesting mobile game(BFS+DFS)

    Problem Description XQ,one of the three Sailormoon girls,is usually playing mobile games on the clas ...

  9. Project Euler problem 68

    题意须要注意的一点就是, 序列是从外层最小的那个位置顺时针转一圈得来的.而且要求10在内圈 所以,这题暴力的话,假定最上面那个点一定是第一个点,算下和更新下即可. #include <iostr ...

  10. bug统计分析续(一)基于SQL的Bug统计方法

    本文由 @lonelyrains 出品.转载请注明出处. 文章链接: http://blog.csdn.net/lonelyrains/article/details/44225533 上一篇为 bu ...