思路:

拓扑排序,这里是用染色的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. ESG操作指南

    ESG使用指南:1.ESG操作文档网站:ESG有个网站,是专门的操作文档网站,因为ESG三个环境,流程各不一样.地址:http://10.20.12.90:20567/esg-help-doc/2.E ...

  2. pycharm内存不足时如何修改设置?

    Help->Find Action->(type "VM Options")->(Click)"Edit Custom VM Options" ...

  3. 使用maven时,如何修改JVM的配置参数;maven命令执行时到底消耗多少内存?

    maven是使用java启动的,因此依赖JVM,那么如何修改JVM参数? MAVEN_OPTS 在系统的环境变量中,设置MAVEN_OPTS,用以存放JVM的参数,具体设置的步骤,参数示例如下: MA ...

  4. Linux学习系列之memcached

    memcached简介 一.memcached是什么 memcached是一个开源的.支持高性能.高并发的分布式内存缓存系统 mem+cache+daemon:分布式内存缓存守护进程 memcache ...

  5. (6)文本挖掘(三)——文本特征TFIDF权重计算及文本向量空间VSM表示

    建立文本数据数学描写叙述的过程分为三个步骤:文本预处理.建立向量空间模型和优化文本向量. 文本预处理主要採用分词.停用词过滤等技术将原始的文本字符串转化为词条串或者特点的符号串.文本预处理之后,每个文 ...

  6. BUILD FAILED D:\build.xml:2: 前言中不同意有内容。

    1.错误描写叙述 Microsoft Windows [版本号 6.1.7601] 版权全部 (c) 2009 Microsoft Corporation. 保留全部权利. C:\Users\Admi ...

  7. JSTL简单入门学习实例

    Maven依赖: <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</ ...

  8. Android自己主动提示文本框(AutoCompleteTextView)

    自己主动提示文本框(AutoCompleteTextView)能够加强用户体验,缩短用户的输入时间(百度的搜索框就是这个效果). 首先.在xml中定义AutoCompleteTextView控件: a ...

  9. IOS-Storyboard控制器切换之Modal(1)

    Modal模式是指模态切换.新开的界面会挡住之前的界面,使之不能获取焦点. 创建一个singleView模板的程序,打开storyboard文件.拖动2个UIViewController到界面中.按住 ...

  10. 在阿里云域名https配置(nginx为例)

    如题: 在阿里云上注册了域名之后在阿里云域名控制台配置https: 1.在域名控制台选择要配置的域名,并在操作栏点击“解析” 2.在域名解析点击更多下的SSL进入到证书列表页,这里有收费的也有免费的, ...