poj2367 Genealogical tree
思路:
拓扑排序,这里是用染色的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的更多相关文章
- POJ2367 Genealogical tree (拓扑排序)
裸拓扑排序. 拓扑排序 用一个队列实现,先把入度为0的点放入队列.然后考虑不断在图中删除队列中的点,每次删除一个点会产生一些新的入度为0的点.把这些点插入队列. 注意:有向无环图 g[] : g[i] ...
- 【拓扑排序】Genealogical tree
[POJ2367]Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5696 Accep ...
- [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
题目连接 http://poj.org/problem?id=2367 Genealogical tree Description The system of Martians' blood rela ...
- poj 2367 Genealogical tree【拓扑排序输出可行解】
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3674 Accepted: 2445 ...
- Genealogical tree(拓扑结构+邻接表+优先队列)
Genealogical tree Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) ...
- POJ 2367 Genealogical tree 拓扑排序入门题
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8003 Accepted: 5184 ...
- POJ 2367:Genealogical tree(拓扑排序模板)
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7285 Accepted: 4704 ...
随机推荐
- JSP的会话(Session)跟踪
以下内容引用自http://wiki.jikexueyuan.com/project/jsp/session-tracking.html: 会话(Session) HTTP是一个“无状态”协议,这意味 ...
- CF #367 DIV2 E
直接使用指针,交换时交换矩阵周围的指针即可. #include <iostream> #include <cstdio> #include <cstring> us ...
- Mac使用技巧之Finder的个人收藏
当使Finder的时候,左側会列出来个人收藏,能够非常方便的打开对应的文件夹.那么怎样把自己新建的文件夹也增加到个人收藏呢? 1.默认的个人收藏 2.新建名字为my_ios_demo文件夹,拖动这个文 ...
- prototype与几个循环的心得
<一>prototypeprototype其实是函数的一个属性,并且只有函数有这个属性,这个属性就是给函数增加函数或者属性的,比如写一个function one(){},那么one.pro ...
- 用jquery给元素动态绑定事件及样式
网页输出的时候,可以用jquery给各种元素绑定事件,或设置样式. 之所以这样做,好处是节省代码,尤其适合元素很多,并且元素的事件对应的函数雷同的情况. 看看以下代码: <div id=&quo ...
- exception log
except Exception as e: l = [str(i) for i in [dbid, f_mp3, e]] log_s = '||'.join(l) logging.exception ...
- Massive Data Mining学习记录
第一周: 学习PageRank, 知识点:每个节点的权值由其他节点的投票决定,所有节点的权值和为1 当节点很多时候必须转换成矩阵运算来计算节点的最终值,由马尔可夫链可以证明,这个值可以迭代得到 问题: ...
- 在MAC端查看win7
在MAC端查看win7,在finder中打开网络,输入win7地址,填入用户名和密码,就可以了
- 利用 C# dynamic 减少创建模型类
C# 的 dynamic 关键字可以是C#可以像 javascript 这种弱类型语言一样具有随时可以添加属性的能力.C# 是一种强类型语言,dynamic 要摆脱类型的限制,自然是有代价的.这里不讨 ...
- 4.4 Top-Down Parsing
4.4 Top-Down Parsing Top-down parsing can be viewed as the problem of constructing a parse tree for ...