火星人的血缘关系很奇怪,一个人可以有很多父亲,当然一个人也可以有很多孩子。
有些时候分不清辈分会产生一些尴尬。所以写个程序来让n个人排序,长辈排在晚辈前面。

输入:N 代表n个人 1~n 接下来n行 第i行表示第i个人的孩纸,无序排列,可能为空。0代表一行输入结束。

(大概我的智商真的不合适,否则怎么这么久了连个拓扑排序都写不好,T了三次。。)

代码:

/********************************************
Problem: 2367 User:
Memory: 700K Time: 32MS
Language: G++ Result: Accepted
********************************************/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector> using namespace std; const int N = 105;
int n;
vector<int> G[N];
int RD[N];
int vis[N]; void topsort()
{
for (int j = 1; j <= n; ++j)
for (int i = 1; i <= n; ++i) {
if (!vis[i] && RD[i] == 0) { if (j != 1) printf(" ");
printf("%d", i);
for (int k = 0; k < G[i].size(); ++k)
--RD[G[i][k]];
vis[i] = 1;
break;
}
}
printf("\n");
} int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
int temp;
while (scanf("%d", &temp) == 1) {
if (temp == 0) break;
G[i].push_back(temp);
++RD[temp];
}
}
topsort();
return 0;
}

  

好吧,改了一下第一次的代码也A了,输入的问题= =#

/****************************************
Problem: 2367 User:
Memory: 736K Time: 0MS
Language: G++ Result: Accepted
****************************************/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector> using namespace std; const int N = 105; int mp[N][N]; // mp[i][j] i is j's son
int vis[N];
int n; void read(int i)
{
char ch;
int ans = 0; while (scanf("%d", &ch), ch) {
mp[ch][i] = 1;
++mp[ch][0];
}
} void tpsort()
{
int i, j;
for (j = 1; j <= n; ++j) {
for (i = 1; i <= n; ++i) {
if (!vis[i] && mp[i][0] == 0) {
if (j != 1) printf(" ");
printf("%d", i);
vis[i] = 1; for (int k = 1; k <= n; ++k) {
if (!vis[k] && mp[k][i] == 1) {
mp[k][i] = 0;
--mp[k][0];
}
}
break;
}
}
}
printf("\n");
} int main()
{
scanf("%d", &n);
getchar();
memset(mp, 0, sizeof mp);
memset(vis, 0, sizeof vis);
for (int i = 1; i <= n; ++i) {
read(i);
}
tpsort();
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 拓扑题解

    一条标准的拓扑题解. 我这里的做法就是: 保存单亲节点作为邻接表的邻接点,这样就非常方便能够查找到那些点是没有单亲的节点,那么就能够输出该节点了. 详细实现的方法有非常多种的,比方记录每一个节点的入度 ...

  4. poj 2367 Genealogical tree

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

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

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

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

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

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

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

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

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

  9. POJ 2367 (裸拓扑排序)

    http://poj.org/problem?id=2367 题意:给你n个数,从第一个数到第n个数,每一行的数字代表排在这个行数的后面的数字,直到0. 这是一个特别裸的拓扑排序的一个题目,拓扑排序我 ...

随机推荐

  1. oracle 行转列问题

    select id, name, ),),)) "imp_value", ),), )) "click_value" from (SELECT a.id, a. ...

  2. 3.bit-map

    适用范围:可进行数据的快速查找,判重,删除,一般来说数据范围是int的10倍以下 基本原理及要点:使用bit数组来表示某些元素是否存在,比如8位电话号码 扩展:bloom filter可以看做是对bi ...

  3. Windows 2008 故障转移群集介绍

    转载:http://dufei.blog.51cto.com/382644/902026 今天有客户问起Windows 群集的相关内容,毕竟Windows Server2008所支持的群集技术和Win ...

  4. Tomacat服务器的安装和配置

    一, Tomcat服务器的下载地址(Apache Tomcat的官网):  http://tomcat.apache.org/download-70.cgi 这里为了稳定性安装的版本为7.0. 截止目 ...

  5. about backbone

    http://presentation.tiddr.de/backbone-presentation/introduction/index.html#Further%20issues http://m ...

  6. easyui源码翻译1.32--Form(表单)

    前言 使用$.fn.form.defaults重写默认值对象下载该插件翻译源码 form提供了各种方法来操作执行表单字段,比如:ajax提交, load, clear等等.当提交表单的时候可以调用va ...

  7. Spring cloud项目实践(一)

    链接地址:http://sail-y.github.io/2016/03/21/Spring-cloud%E9%A1%B9%E7%9B%AE%E5%AE%9E%E8%B7%B5/ 什么是Spring ...

  8. Oracle中的阻塞锁SQL(阻塞在哪个数据上)

        SELECT (   '节点 '           || a.inst_id           || ' session '           || a.sid           || ...

  9. hdu4734F(x)(dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=4734 各种不细心啊  居然算的所有和最大值会小于1024... 第二次做数位DP  不是太熟 #include ...

  10. EF双向一对一中的坑

    EF版本 6.0 在项目中双向一对一关系是普遍存在的,如果不仔细检查,并不容易发现这个坑 下面新建两个类(假设这两个类是一对一的关系)对应实体都设置为可延迟加载 映射关系为: 再建一个数据访问类: 运 ...