Poj 2367 Genealogical tree(拓扑排序)
2014-03-15 14:58
302 查看
题目:火星人的血缘关系,简单拓扑排序。很久没用邻接表了,这里复习一下。
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 ); } } }
相关文章推荐
- activity的使用(十二):启动录音
- 字符串处理--表达式
- C++返回数组指针
- 容器(vector)、数组、new创建的动态数组,你到底用哪一个(执行效率分析)
- Android详细的对话框AlertDialog.Builder使用方法 对话框风格的activity
- 容器(vector)、数组、new创建的动态数组,你到底用哪一个(执行效率分析)
- java线程 之 资源同步(1)
- [置顶] Android ExpandableListView 可展开的ListView
- 把数组排成最小的数
- PowerShell 配置文件以及别名
- SEO新手别低估百度的智商
- 求解最小生成树的算法 prim算法(附模板)
- [SPFA预处理+DP]1003: [ZJOI2006]物流运输trans
- 触控操作新体验 云智汇M11记录仪首测
- zoj3203 Light Bulb(三分)
- 最长回文子序列
- POJ 3264 线段树 基础入门
- VS2013 中生成lib和dll库文件
- 李开复:算法的力量
- andrioid 创建等待框