火星人的血缘关系很奇怪,一个人可以有很多父亲,当然一个人也可以有很多孩子。
有些时候分不清辈分会产生一些尴尬。所以写个程序来让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. ANGULAR 2 FOR REACT DEVELOPERS

    Now that Angular 2 is in beta, the time has come for us to drop everything and learn something new, ...

  2. Windows Phone 8 开发环境搭建

    原地址:http://blog.csdn.net/md521/article/details/11015139 Windows Phone 8将采用与Windows 8相同的NT内核,这就意味着WP8 ...

  3. iOS 并发:NSOperation 与调度队列入门(1)

    一直以来,并发都被视为 iOS 开发中的「洪水猛兽」.许多开发者都将其视为危险地带,唯恐避之而不及.更有谣传认为,多线程代码应该尽力避免.笔者同意,如果你对并发的了解不够深入,就容易造成危险.但是,危 ...

  4. Linux巡检

    # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # hostn ...

  5. Jmeter 日志设置---如何设置java协议中被测jar的日志?

    先转载一下Jmeter的日志设置: Jmeter运行出现问题可以通过调整jmeter的日志级别定位问题,但运行测试时建议关闭jmeter日志,jmeter打印日志耗费系统性能. Jmeter日志默认存 ...

  6. 严重: The web application [] registered the JDBC driver 错误

    近日发现启动tomcat的时候报如下警告: -- :: org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc 严重: The ...

  7. Python sh库学习 上篇

    官方文档有句话"allows you to call any program",并且:helps you write shell scripts in Python by givi ...

  8. 【POJ】2823 Sliding Window

    单调队列. /* 2823 */ #include <iostream> #include <sstream> #include <string> #include ...

  9. Oracle Form Developer: Folder FRM-99999 Error 14212

    Question: 做FOLDER文件夹功能,打开FORM错误提示: FRM-99999:出现1412错误.有关该错误的详细信息,请参阅发行说明文件(relnotes) Answer: 原因是FOLD ...

  10. apache和tomcat

    Apache 和 Tomcat 都是web网络服务器,两者既有联系又有区别,在进行HTML.PHP.JSP.Perl等开发过程中,需要准确掌握其各自特点,选择最佳的服务器配置. Apache是web服 ...