zoukankan      html  css  js  c++  java
  • 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]);
    		}
    	}
    }
    


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    在HTML网页中嵌入脚本的方式
    纪念品分组(贪心、排序)
    合并果子(STL优先队列)
    铺地毯(取最上层的地毯)
    多项式方程的输出
    BF算法(蛮力匹配)
    数位的处理
    两个数的差
    多项式计算器
    随机数生成器java实现
  • 原文地址:https://www.cnblogs.com/AndyDai/p/4734091.html
Copyright © 2011-2022 走看看