一条标准的拓扑题解。

我这里的做法就是:

保存单亲节点作为邻接表的邻接点,这样就非常方便能够查找到那些点是没有单亲的节点,那么就能够输出该节点了。

详细实现的方法有非常多种的,比方记录每一个节点的入度,输出一个节点之后,把这个节点对于其它节点的入度去掉,然后继续查找入度为零的点输出。这个是一般的做法了,效果和我的程序一样的。

有兴趣的也能够參考下我这样的做法。

#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 拓扑题解的更多相关文章

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

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

  2. Poj 2367 Genealogical tree(拓扑排序)

    题目:火星人的血缘关系,简单拓扑排序.很久没用邻接表了,这里复习一下. import java.util.Scanner; class edge { int val; edge next; } pub ...

  3. poj 2367 Genealogical tree

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

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

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

  5. POJ 2367 Genealogical tree【拓扑排序/记录路径】

    Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7101 Accepted: 4585 Spe ...

  6. 图论之拓扑排序 poj 2367 Genealogical tree

    题目链接 http://poj.org/problem?id=2367 题意就是给定一系列关系,按这些关系拓扑排序. #include<cstdio> #include<cstrin ...

  7. poj 2367 Genealogical tree (拓扑排序)

    火星人的血缘关系很奇怪,一个人可以有很多父亲,当然一个人也可以有很多孩子.有些时候分不清辈分会产生一些尴尬.所以写个程序来让n个人排序,长辈排在晚辈前面. 输入:N 代表n个人 1~n 接下来n行 第 ...

  8. POJ 2367 Genealogical tree【拓扑排序】

    题意:大概意思是--有一个家族聚集在一起,现在由家族里面的人讲话,辈分高的人先讲话.现在给出n,然后再给出n行数 第i行输入的数表示的意思是第i行的子孙是哪些数,然后这些数排在i的后面. 比如样例 5 ...

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

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

随机推荐

  1. jQuery.fn.extend和jQuery.extend

    <script src="http://www.cssrain.cn/demo/JQuery+API/jquery-1[1].2.1.pack.js" type=" ...

  2. c++写入txt

    用ofstream 输出流,#include <fstream> ofstream outf; outf.open("abc.txt");outf<<123 ...

  3. vue组件---动态组件&异步组件

    (1)在动态组件上使用keep-alive 之前曾经在一个多标签的界面中使用 is 特性来切换不同的组件.接下来简单回顾下 <component>元素是vue 里面的一个内置组件.在里面使 ...

  4. TP中U方法详解

    U方法常用于ThinkPHP里的页面跳转 官方称为url组装, 就是根据某种规则组成一个url地址,这个功能就叫组装. 在ThinkPHP里,系统提供了一个封装的函数来处理url的组装,俗称U方法. ...

  5. Linux kernel memory-faq.txt

    ## Linux kernel memory-faq.txt What is some existing documentation on Linux memory management? Ulric ...

  6. LTTng

    Waiting for dig... http://frederic-wou.net/lttng/

  7. 代码静态分析工具-splint的学习与使用[转]

    代码静态分析工具--splint的学习与使用[转] 引言 最近在项目中使用了静态程序分析工具PC-Lint,体会到它在项目实施中带给开发人员的方便.PC-Lint是一款针对C/C++语言.window ...

  8. The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_60\bin;C:\Windows\Sun\Jav

    启动项目自动结束,查看日志发现 [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache To ...

  9. 使用vuex实现父组件调用子组件方法

    曲线救国. 核心原理就是父子共用一个vuex对象,且看代码: 父组件parent.vue <template> <div class="wrap"> < ...

  10. Shiro_认证思路分析

    [认证] 也就是登录. 1.获取当前的subject,调用SecurityUtils.getSubject() 2.测试当前的用户是否已经被认证,即是否登录.调用subject的isAuthentic ...