poj 2367 Genealogical tree (拓扑排序)
火星人的血缘关系很奇怪,一个人可以有很多父亲,当然一个人也可以有很多孩子。
有些时候分不清辈分会产生一些尴尬。所以写个程序来让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 (拓扑排序)的更多相关文章
- 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 拓扑题解
一条标准的拓扑题解. 我这里的做法就是: 保存单亲节点作为邻接表的邻接点,这样就非常方便能够查找到那些点是没有单亲的节点,那么就能够输出该节点了. 详细实现的方法有非常多种的,比方记录每一个节点的入度 ...
- poj 2367 Genealogical tree
题目连接 http://poj.org/problem?id=2367 Genealogical tree Description The system of Martians' blood rela ...
- 图论之拓扑排序 poj 2367 Genealogical tree
题目链接 http://poj.org/problem?id=2367 题意就是给定一系列关系,按这些关系拓扑排序. #include<cstdio> #include<cstrin ...
- 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【拓扑排序】
题意:大概意思是--有一个家族聚集在一起,现在由家族里面的人讲话,辈分高的人先讲话.现在给出n,然后再给出n行数 第i行输入的数表示的意思是第i行的子孙是哪些数,然后这些数排在i的后面. 比如样例 5 ...
- POJ 2367 (裸拓扑排序)
http://poj.org/problem?id=2367 题意:给你n个数,从第一个数到第n个数,每一行的数字代表排在这个行数的后面的数字,直到0. 这是一个特别裸的拓扑排序的一个题目,拓扑排序我 ...
随机推荐
- 上传图片+浏览+裁切 Demo(无后台处理部分)
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- PHP完整环境搭建
Linux(CentOS 7)+ Nginx(1.10.2)+ Mysql(5.7.16)+ PHP(7.0.12) 首先安装Linux系统,我以虚拟机安装来做示例,先去下载 VitualBox,这是 ...
- c++ 内存泄露的检查
对于c++的内存泄露检测,除了我们自己手动检查以外,还可以使用c++中的函数来帮助我们检测, 如下代码: #include "stdafx.h" #include <stri ...
- js复制黏贴
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 如何从软硬件层面提升 Android 动画性能?
若是有人问如何解决动画性能不佳的问题,Dan Lew Codes 总会反问:你是否使用了硬件层? 动画放映过程中每帧画面可能都要重绘.如果使用视图层,,渲染过的视图可以存入离屏缓存以待将来重用,而无需 ...
- Moloch
http://www.oschina.net/p/moloch maltego http://www.oschina.net/p/maltego
- HDU Traffic Real Time Query System
题目大意是:对于(n, m)的图,给定边a, b查询从a到b要经过的割点的最少数目. 先tarjan算法求双连通然后缩点,即对于每个割点将周围的每个双连通看成一个点与之相连.然后求解LCA即可,距离d ...
- is present but cannot be translated into a null value due to being declared as a primitive type
解决办法:把基本类型改为对象,譬如此处将pageId的类型由int 改为Integer 2016-10-19 19:36:11.275 DEBUG [http-nio-9999-exec-2][org ...
- 【HDOJ】4516 威威猫系列故事——因式分解
可解的算法太多了,采用的算法是试x的值.注意题目的输入x^3-2x^2不会写成x^3+-2x^2.一直RE在这儿. /* 4516 */ #include <iostream> #incl ...
- NTP 服务器配置
1.yum安装ntp 1 yum install ntp* 2.更新时间 1 ntpdate 202.120.2.101 3.加入任务计划 1 2 crontab -e */10 * * * * nt ...