题目:火星人的血缘关系,简单拓扑排序。很久没用邻接表了,这里复习一下。

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(拓扑排序)的更多相关文章

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

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

  2. POJ 2367 Genealogical tree 拓扑题解

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

  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

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

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

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

  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. 20165101刘天野 2017-2018-2 《Java程序设计》第8周学习总结

    #20165101刘天野 2017-2018-2 <Java程序设计>第8周学习总结 教材学习内容总结 第十二章Java多线程机制 一.进程与线程 1.1.任务调动 大部分操作系统(如Wi ...

  2. windows7下手工搭建Apache2.2 php5.3 Mysql5.5开发环境

    Apache2.2(apache_2.2.2-win32-x86-no_ssl)php5.3.5(php-5.3.5-Win32-VC6-x86,请注意选择VC6版本,否则无法加载php5apache ...

  3. Ubuntu: 无法使用su命令

    Ubuntu 无法使用su命令解决方案 在Ubuntu上编译Qt环境时发现无法使用su命令切换到root用户,通过网上查找发现解决方案如下: xt@xt-ubuntu:~$ su密码: su:认证失败 ...

  4. 【51nod1519】拆方块[Codeforces](dp)

    题目传送门:1519 拆方块 首先,我们可以发现,如果第i堆方块被消除,只有三种情况: 1.第i-1堆方块全部被消除: 2.第i+1堆方块全部被消除:(因为两侧的方块能够保护这一堆方块在两侧不暴露) ...

  5. Python ssh 远程执行shell命令

    工具 python paramiko 远程执行命令 import paramiko ssh = paramiko.SSHClient() key = paramiko.AutoAddPolicy() ...

  6. 将datagridview数据保为xml或txt文件

    using System.IOpublic void SaveFile()        {            //实例化一个保存文件对话框            SaveFileDialog s ...

  7. POJ1741 经典树分治

    题意:有一棵树,每条边有一个距离,求dis(u,v)<=k的点的对数 题解:树分治,对于一颗树上的两点,要么在同一颗子树上,要么在不同子树上,要么一个点是根,另一个在某一子树上,对于第一种情况我 ...

  8. POJ3468 splay

    线段树/树状数组裸题,用splay写 splay也是基本操作pushup pushdown 话说我就是找不到全一点的模板,我自己写又全是bug,导致代码风格一直变来变去= = 关键是建树和区间操作(区 ...

  9. ural 2022 Riding a Toad

    2022. Riding a Toad Time limit: 1.0 secondMemory limit: 64 MB A tribe of leafmen live in the old for ...

  10. @angular/cli项目构建--http(2)

    客户端GET设置参数查询: search() { const params = new HttpParams() .set('userName', this.userName) .set('fullN ...