POJ 2367 Genealogical tree 拓扑题解
一条标准的拓扑题解。
我这里的做法就是:
保存单亲节点作为邻接表的邻接点,这样就非常方便能够查找到那些点是没有单亲的节点,那么就能够输出该节点了。
详细实现的方法有非常多种的,比方记录每一个节点的入度,输出一个节点之后,把这个节点对于其它节点的入度去掉,然后继续查找入度为零的点输出。这个是一般的做法了,效果和我的程序一样的。
有兴趣的也能够參考下我这样的做法。
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std; const int MAX_N = 101;
int N, v;
vector<int> gra[MAX_N];
bool vis[MAX_N]; void topologicalSort()
{
int c = 0;
while (c < N)
{
for (int i = 1; i <= N; i++)
{
if (vis[i]) continue;
bool ind = 0;
for (int j = 0; j < (int)gra[i].size(); j++)
{
if (!vis[gra[i][j]])
{
ind = true;
break;
}
}
if (!ind)
{
c++;
vis[i] = true;
printf("%d ", i);
}
}
} } int main()
{
while (~scanf("%d", &N))
{
for (int i = 1; i <= N; i++) gra[i].clear();
memset(vis, 0, sizeof(vis)); for (int u = 1; u <= N; u++)
{
while (~scanf("%d", &v) && v)
gra[v].push_back(u);
}
topologicalSort();
putchar('\n');
}
return 0;
}
POJ 2367 Genealogical tree 拓扑题解的更多相关文章
- 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 ...
- 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 ...
- POJ 2367 Genealogical tree【拓扑排序/记录路径】
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7101 Accepted: 4585 Spe ...
- 图论之拓扑排序 poj 2367 Genealogical tree
题目链接 http://poj.org/problem?id=2367 题意就是给定一系列关系,按这些关系拓扑排序. #include<cstdio> #include<cstrin ...
- poj 2367 Genealogical tree (拓扑排序)
火星人的血缘关系很奇怪,一个人可以有很多父亲,当然一个人也可以有很多孩子.有些时候分不清辈分会产生一些尴尬.所以写个程序来让n个人排序,长辈排在晚辈前面. 输入:N 代表n个人 1~n 接下来n行 第 ...
- POJ 2367 Genealogical tree【拓扑排序】
题意:大概意思是--有一个家族聚集在一起,现在由家族里面的人讲话,辈分高的人先讲话.现在给出n,然后再给出n行数 第i行输入的数表示的意思是第i行的子孙是哪些数,然后这些数排在i的后面. 比如样例 5 ...
- timus 1022 Genealogical Tree(拓扑排序)
Genealogical Tree Time limit: 1.0 secondMemory limit: 64 MB Background The system of Martians’ blood ...
随机推荐
- Angular——MVC模式开发实战
创建项目 创建工作目录 使用bower下载需要插件 git init.add.commit之后得到分支master,再创建developer分支,然后再此分支上进行具体功能开发 MVC架构 之前小项目 ...
- LDA算法(入门篇)
一. LDA算法概述: 线性判别式分析(Linear Discriminant Analysis, LDA),也叫做Fisher线性判别(Fisher Linear Discriminant ,FLD ...
- swift protocol 与类继承结合时的bug
protocol CommonTrait: class { func commonBehavior() -> String } extension CommonTrait { func comm ...
- 校内测之zay与银临 (day2)(只有T1)
一些与题目无关的碎碎念 推出式子来一定要化简!!!freopen不要写错!!!特判不要瞎搞!!!! 做到以上三点能高35分qwq T1 江城唱晚 你看数据那么大,显然又是一道数学题. 这里有n个种海棠 ...
- HDU - 5952 Counting Cliques(dfs搜索)
题目: A clique is a complete graph, in which there is an edge between every pair of the vertices. Give ...
- Python使用Flask框架,结合Highchart,搭配数据功能模块处理csv数据
参考链接:https://www.highcharts.com.cn/docs/data-modules 1.javascript代码 var csv = document.getElementByI ...
- Python supprocess模块
当我们需要调用系统的命令的时候,最先考虑的os模块.用os.system()和os.popen()来进行操作.但是这两个命令过于简单,不能完成一些复杂的操作,如给运行的命令提供输入或者读取命令的输出, ...
- js cookies all in one
js cookies all in one cookies // http://10.1.5.202/auto-deploy-platform/publish/index.html // 非当前 UR ...
- [luoguP1316] 丢瓶盖(二分答案)
传送门 二分答案再判断即可 ——代码 #include <cstdio> #include <iostream> #include <algorithm> #def ...
- 夜话JAVA设计模式之适配器模式(adapter pattern)
适配器模式:将一个类的接口,转换成客户期望的另一个接口,让不兼容的接口变成兼容. 1.类适配器模式:通过多重继承来实现适配器功能.多重继承就是先继承要转换的实现类,再实现被转换的接口. 2.对象适配器 ...