博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
拓扑排序
阅读量:6692 次
发布时间:2019-06-25

本文共 3174 字,大约阅读时间需要 10 分钟。

       拓扑排序时应用于有向图的,这个用邻接矩阵来实现比较麻烦。虽然我比较喜欢用邻接矩阵,但是这里还是用C++邻接表吧。

       怎么用邻接表呢,这里推荐用STL里的vector

       头文件  

1 #include
2 using namespace std;

           定义

struct Edge{    int nextNodel    int cost;};vector 
edge[N];

         基本操作

edge[i].clear()  //清空链表结点edge[i].push_back( temp )  //将结点加入单链表edge[i].erase( edge[1].begin()+i,edge[1].begin()+i+1);//(vector.begin()+第一个要删除的元素编号,vector.begin()+最后一个要删除的元素编号+1;

 

这里再补充STL中常用的栈跟队列的应用

#include
stack
S;S.push(i); //进栈 int x = S.top(); //取栈顶元素S.pop(); //出栈S.empty(); //判空

 

#include
queue
Q;Q.push(x) ; //将元素放入队尾x = Q.front(); //读取队头元素,将其值赋值给xQ.pop(); //队头元素弹出Q.empty(); //判断队列是否为空,若返回值为true代表队列为空

 Legal or Not

题目描述

ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that just like a big family. Every day,many "holy cows" like HH, hh, AC, ZT, lcc, BF, Qinz and so on chat on-line to exchange their ideas. When someone has questions, many warm-hearted cows like Lost will come to help. Then the one being helped will call Lost "master", and Lost will have a nice "prentice". By and by, there are many pairs of "master and prentice". But then problem occurs: there are too many masters and too many prentices, how can we know whether it is legal or not? 
We all know a master can have many prentices and a prentice may have a lot of masters too, it's legal. Nevertheless,some cows are not so honest, they hold illegal relationship. Take HH and 3xian for instant, HH is 3xian's master and, at the same time, 3xian is HH's master,which is quite illegal! To avoid this,please help us to judge whether their relationship is legal or not. 
Please note that the "master and prentice" relation is transitive. It means that if A is B's master ans B is C's master, then A is C's master.

输入描述:

The input consists of several test cases. For each case, the first line contains two integers, N (members to be tested) and M (relationships to be tested)(2 <= N, M <= 100). Then M lines follow, each contains a pair of (x, y) which means x is y's master and y is x's prentice. The input is terminated by N = 0.       TO MAKE IT SIMPLE, we give every one a number (0, 1, 2,..., N-1). We use their numbers instead of their names.

输出描述:

For each test case, print in one line the judgement of the messy relationship.       If it is legal, output "YES", otherwise "NO".
示例1

输入

3 20 11 22 20 11 00 0

输出

YESNO

这道题大意是判断师徒关系是否正确,徒弟越多越厉害。就要避免有一些人刷师徒关系,比如说A是B的徒弟,B又是A的徒弟,这就不合法了。这就可以转换为判断是否是一个有向无环图的问题了。如果是有向无环图那么该关系是合法的,若存在环则不合法。

那拓扑排序是如何判断有没有环的呢?如果所有的结点尚未完全被删去时就出现找不到入度为0的结点的情况,说明剩余的结点形成一个环路。

 

1 #include
2 3 #include
4 #include
5 using namespace std; 6 7 vector
edge[501]; //邻接链表,因为边不存在权值,只需保存与其邻接的结点编号即可 8 queue
Q; //保存入度为0的结点的队列 9 int inDegree[501]; //统计每个结点的入度10 11 int main()12 {13 int n,m;14 int i;15 int a,b;16 int cnt;17 int newP;18 while( scanf("%d%d",&n,&m)!=EOF)19 {20 if(n==0 &&m==0) break;21 22 cnt = 0;23 for( i=0; i

 

转载于:https://www.cnblogs.com/yuxiaoba/p/8449092.html

你可能感兴趣的文章
Maven中基于POM.xml的Profile来动态切换配置信息
查看>>
Easyloggingpp的使用
查看>>
java十五个常用类学习及方法举例
查看>>
Jvm(36),class文件结构----访问标志
查看>>
服务高可用:幂等性设计
查看>>
ant 重置(修改)DatePicker MonthPicker Cascader 的值
查看>>
eShopOnContainers 知多少[3]:Identity microservice
查看>>
WPF自定义TextBox及ScrollViewer
查看>>
go基础系列:简介
查看>>
聚合支付系统设计(二)
查看>>
centos7 安装php7
查看>>
使用java的Calendar工具类获取到本月的第一天起始时间和最后一天结束时间。
查看>>
Docker中安装WordPress
查看>>
oracle goldengate的两种用法
查看>>
Racket里的方括号
查看>>
【强化学习】用pandas 与 numpy 分别实现 q-learning, saras, saras(lambda)算法
查看>>
C#后台解析 json 动态解析 通用(Dictionary)
查看>>
使用UrlRewriter进行Url重写的完整解决方案[转]
查看>>
在代码里访问HTC Diamond的倾斜传感器
查看>>
tcp和udp能否发送0字节的数据包
查看>>