puk2367 拓扑排序
Description
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
Output
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;
}
puk2367 拓扑排序的更多相关文章
- 算法与数据结构(七) AOV网的拓扑排序
今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...
- 有向无环图的应用—AOV网 和 拓扑排序
有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...
- 【BZOJ-2938】病毒 Trie图 + 拓扑排序
2938: [Poi2000]病毒 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 609 Solved: 318[Submit][Status][Di ...
- BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)
题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...
- 图——拓扑排序(uva10305)
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- Java排序算法——拓扑排序
package graph; import java.util.LinkedList; import java.util.Queue; import thinkinjava.net.mindview. ...
- poj 3687(拓扑排序)
http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...
- 拓扑排序 - 并查集 - Rank of Tetris
Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球 ...
- *HDU1285 拓扑排序
确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
随机推荐
- MATLAB中均值、方差、均方差的计算方法
MATLAB中均值.方差.均方差的计算方法 1. 均值 数学定义: Matlab函数:mean >>X=[1,2,3] >>mean(X)=2 如果X是一个矩阵,则其均 ...
- 理解vue与MVVM三要素
MVVM到底是什么,跟Jquery有什么区别? MVVM理解,跟MVC区别 Model View Controller,一般是用户操作view视图按钮,触发controller内方法,cotrolle ...
- MySQL 5.7二进制日志
简介 二进制日志是MySQL服务器用来记录数据修改事件的,比如INSERT.UPDATE.DELETE等会导致数据发生变化的语句,SELECT语句不会被记录在内.MySQL必须先执行完一条语句才能知道 ...
- Intel HEX格式
来来 !! come baby ! 只强调一点这篇文章有checksum的算法,是我最喜欢地!! 参考:https://blog.csdn.net/extlife/article/details/ ...
- Solon集成(02)- 轻松吃下小馒头 Dubbo
Solon详解系列文章: Solon详解(一)- 快速入门 Solon详解(二)- Solon的核心 Solon详解(三)- Solon的web开发 Solon详解(四)- Solon的事务传播机制 ...
- [学习笔记] Treap
想必大家都知道一种叫做二叉搜索树这东西吧,那么我们知道,在某些特殊情况下,二叉搜索树会退化成一条链,而且如果出题人成心想卡你的话也很简单,分分钟把你(n log n)的期望卡成.那么我们该如何避免这种 ...
- vue3.x版本新建项目相关知识和注意事项
前言你前提应该懂下面基础知识:下载node.js 下好后自带npm 命令 终端查看命令 npm -v 即可看到安装版本安装淘宝镜像:npm install -g cnpm --registry=htt ...
- MVC与MVVM理解
MVC MVC是一种软件架构模式,也有人叫做设计模式 M: Model 数据模型(专门用来操作数据,数据的CRUD) V:View 视图(对于前端来说,就是页面) C:Controller 控制器(是 ...
- vue+element ui 关闭弹窗前清空form表单的值
this.$refs['disposeConfigsform'].resetFields();
- 2017年 实验五 B2B模拟实验
实验五 B2B模拟实验 [实验目的] ⑴.掌握B2B中供应商的供求信息发布.阿里商铺开设和订单交易等过程. ⑵.掌握B2B中采购商的采购信息的发布.交易洽谈.网上支付和收货等过程. [实验条件] ⑴ ...