zoukankan      html  css  js  c++  java
  • puk2367 拓扑排序

    Description

    The system of Martians' blood relations is confusing enough. Actually, Martians bud when they want and where they want. They gather together in different groups, so that a Martian can have one parent as well as ten. Nobody will be surprised by a hundred of children. Martians have got used to this and their style of life seems to them natural. 
    And in the Planetary Council the confusing genealogical system leads to some embarrassment. There meet the worthiest of Martians, and therefore in order to offend nobody in all of the discussions it is used first to give the floor to the old Martians, than to the younger ones and only than to the most young childless assessors. However, the maintenance of this order really is not a trivial task. Not always Martian knows all of his parents (and there's nothing to tell about his grandparents!). But if by a mistake first speak a grandson and only than his young appearing great-grandfather, this is a real scandal. 
    Your task is to write a program, which would define once and for all, an order that would guarantee that every member of the Council takes the floor earlier than each of his descendants.

    Input

    The first line of the standard input contains an only number N, 1 <= N <= 100 — a number of members of the Martian Planetary Council. According to the centuries-old tradition members of the Council are enumerated with the natural numbers from 1 up to N. Further, there are exactly N lines, moreover, the I-th line contains a list of I-th member's children. The list of children is a sequence of serial numbers of children in a arbitrary order separated by spaces. The list of children may be empty. The list (even if it is empty) ends with 0.

    Output

    The standard output should contain in its only line a sequence of speakers' numbers, separated by spaces. If several sequences satisfy the conditions of the problem, you are to write to the standard output any of them. At least one such sequence always exists.

    Sample Input

    5
    0
    4 5 1 0
    1 0
    5 3 0
    3 0
    

    Sample Output

    2 4 5 3 1
    /*
    拓扑排序 :由某个集合上的一个偏序打得到该集合上的一个全序。 
    直观的说,偏序指集合中仅有布冯成员可比较,全序则指集合中全部成员之间都可以比较 。
    对有向图进行拓扑排序:
    1)在有向图中选一个没有前驱的定点且输出
    2)从图中伤处该顶点和所有以他结尾的弧 
    */
    #include<stdio.h>
    # define N 200
    int indegree[N];
    //统计每个节点的入度,通过图的邻接矩阵 ,如果两个节点有关系 ,对应位置的数值就是 1 
    void Findindegree(int (*y)[N],int c){
    	int i,j;
    	for(i=1;i<=c;i++) indegree[i]=0;
    	for(i=1;i<=c;i++){
    		for(j=1;j<=c;j++){
    			 indegree[j]=indegree[j]+y[i][j];
    		}
    	}
    //	for(i=1;i<=c;i++) printf("=%d=",indegree[i]);
    //	printf("
    ");
    } 
    int TopSort(int c,int (*y)[N]){
    	int o,p,count,x=0,k;
    	int z[N]={0};
    	//入度为零就进栈,这里用数组表示栈 
    	for(o=1;o<=c;o++){
    		if(indegree[o]==0){
    			z[x]=o;
    			x++;
    	//		printf("-----%d-----%d
    ",x,z[x]);
    		}
    	}
        count = 0;  //统计输出的定点 
    	while(x!=0){
    	//	for(o=1;o<=c;o++) printf("---%d---",indegree[o]);
    		o=z[--x];
    		printf(" %d ",o);  //输出栈顶元素 
    //		printf("=-----%d-----=
     
    ",o);
    		count++;
    		//对输出的元素的子节点的入度进行更新,同时判断入度为零就进栈 
    		for(k=1;k<=c;k++){
    			if(y[o][k]){
    			    indegree[k]--;
    			    if(indegree[k]==0) z[x++] = k;
    		//	    printf("---22222--%d-----%d
    ",x,z[x]);
    			}
    			
    		}
    	}
    	 if(count < c) return 0;
    	 else return 1;	
    }
    int main()
    {
    	int i,j,a,b,sum;
    	int x[N][N]={{0}};
    	scanf("%d",&a);  //数据量 
    	for(i=1;i<=a;i++){
    		do{               //获取数据,以 0 结束 
    		scanf("%d",&b);
    		if(b==0) break;
    		x[i][b] = 1 ;
    		}while(1);
    	}
    /*	for(i=1;i<=a;i++){
    		for(j=1;j<=a;j++){
    	    	printf("= %d =",x[i][j]);
    		} 
    		printf("
    ");
    	}*/
    	Findindegree(x,a);
    	sum=TopSort(a,x);
    	return 0;
    }
    
    

      

      

    你一定会喜欢那个因为喜欢她而发光的自己!
    个人博客:http://www.yanghelong.top
  • 相关阅读:
    【2020】iOS证书(.p12)和描述文件(.mobileprovision)一键申请流程
    苹果开发者账号正在调查原因解答
    【2020最新】苹果iOS开发者到期续费流程
    【2020】联系苹果客服电话入口及步骤
    苹果电脑Transporter App及Windows上传ipa工具Appuploader
    一文学会苹果TestFlight上架详细流程
    iOS APP打包上线App Store七个步骤详解(2020版)
    技术分享 | mysql 表数据校验
    MySQL 自带的4个系统数据库的说明
    linux定时任务crontab怎样执行root命令
  • 原文地址:https://www.cnblogs.com/zzu-general/p/8018362.html
Copyright © 2011-2022 走看看