Poj 2367 Genealogical tree(拓扑排序)
题目:火星人的血缘关系,简单拓扑排序。很久没用邻接表了,这里复习一下。
import java.util.Scanner;
class edge {
int val;
edge next;
}
public class Main {
static int n;
static int MAXV = 1001;
static edge head[] = new edge[MAXV];
static int in[];
static boolean vis[];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
n = sc.nextInt();
for (int i = 0; i <= n; i++) {
head[i] = new edge();
}
in = new int[MAXV];
vis = new boolean[MAXV];
for (int i = 1; i <= n; i++) {
int a;
while ((a = sc.nextInt()) != 0) {
edge t = new edge();
t.val = a;
t.next = head[i].next;
head[i].next = t;
in[a]++;
}
}
int[] ans=new int[n+1];
String s = "";
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (in[j] == 0 && !vis[j]) {
vis[j] = true;
s += j+" ";
ans[i]=j;
edge t = head[j].next;
while (t != null) {
in[t.val]--;
t = t.next;
}
break;
}
}
}
for(int i=1;i<n;i++){
System.out.print(ans[i]+" ");
}
System.out.println(ans[n]);
}
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
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 拓扑题解
一条标准的拓扑题解. 我这里的做法就是: 保存单亲节点作为邻接表的邻接点,这样就非常方便能够查找到那些点是没有单亲的节点,那么就能够输出该节点了. 详细实现的方法有非常多种的,比方记录每一个节点的入度 ...
- 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 (拓扑排序)
火星人的血缘关系很奇怪,一个人可以有很多父亲,当然一个人也可以有很多孩子.有些时候分不清辈分会产生一些尴尬.所以写个程序来让n个人排序,长辈排在晚辈前面. 输入:N 代表n个人 1~n 接下来n行 第 ...
- 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. 这是一个特别裸的拓扑排序的一个题目,拓扑排序我 ...
随机推荐
- php数组函数-array_reduce()
array_reduce()函数发送数组中的值到用户自定义函数,并返回一个字符串. 注:如果数组是空的或则初始化值未传递,该函数返回NULL array_reduce(array,myfunction ...
- poj 1905 Expanding Rods(木杆的膨胀)【数学计算+二分枚举】
...
- 语义web基础知识学习
最近找了一本书,<语义web技术基础>----Pascal Hitzler等编著的.因为最近在看关于自然语言处理的一些东西,就顺带翻了一下,做一点学习笔记,方便以后查看. 本书的作者 Pa ...
- 算法总结之 将单链表的每K个节点之间逆序
给定一个单链表的表头节点head,实现一个调整单链表的函数,是的每k个节点之间逆序,如果最后不够k个节点一组,则不调整最后几个节点 思路: 如果k的值小于2,不调整.k<1 没有意义,k==1代 ...
- Qt QThread 线程创建,线程同步,线程通信 实例
1. 继承QThread, 实现run()方法, 即可创建线程. 2. 实例1 代码 myThread.h #ifndef MYTHREAD_H #define MYTHREAD_H #includ ...
- numpy加权平均
import numpy as np a = np.arange(15).reshape(3,5) a array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], ...
- DB2导入导出方法总结
1.导出 DEL 文本文件 export to D:\T_TESTTABLE20121220.del of del select * from T_TESTTABLE 2.导出 ixf 文本文件.i ...
- 关于html的一些杂技
html预定义字符指的是 :< > html实体指的是 $amp 等 php中htmlspeciachar()就是讲html预定义字符转换成html实体. 浏览器渲染时,会将html实 ...
- "阿拉伯""伊斯兰""穆斯林"三个概念怎么分?
伊斯兰.阿拉伯.穆斯林这三个概念到底有什么不同?要言君将用五分钟给您概述这三个概念,并厘清其边界,说明其交集,帮您迅速构建"阿拉伯.伊斯兰.穆斯林"知识结构概图.相信您得沉思一下费 ...
- (链接)xcode7,ios9 部分兼容设置
xcode7,ios9 部分兼容设置 http://www.cnblogs.com/loganv/p/4647833.html