puk2367 拓扑排序
2024-09-01 09:40:01
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.
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("\n");
}
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\n",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-----=\n \n",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\n",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("\n");
}*/
Findindegree(x,a);
sum=TopSort(a,x);
return 0;
}
最新文章
- Swift3.0语言教程查找字符集和子字符串
- schedule() 和 scheduleAtFixedRate() 区别
- Android 多线程----AsyncTask异步任务详解
- 14.python笔记之paramiko
- 前序/中序--->;后序
- “我爱淘”冲刺阶段Scrum站立会议7
- C#实现发送邮件——核心部分代码
- CRC校验码
- 265行JavaScript代码的第一人称3D H5游戏Demo【个人总结1】
- ubuntu 安装maven提示出错 The program &;#39;mvn&;#39; can be found in the following packages
- hdu4521 小明系列的问题——小明序列(LIS变种 (段树+单点更新解决方案))
- iOS 单元測试之XCTest具体解释(一)
- 从零开始的JS生活(三)——内置对象
- OC学习14——谓词
- Spark编程指南分享
- <;20180930>;故障解决记录Cisco RV系列低端路由
- python+selenium+unittest 实现自动化测试
- vant - 头部 - header【Layout 布局】【Icon 图标】
- JVM building
- lisp base