拓扑排序时应用于有向图的,这个用邻接矩阵来实现比较麻烦。虽然我比较喜欢用邻接矩阵,但是这里还是用C++邻接表吧。
怎么用邻接表呢,这里推荐用STL里的vector
头文件
1 #include<vector> 2 using namespace std;
定义
struct Edge{ int nextNodel int cost; }; vector <Edge> 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> stack<int> S; S.push(i); //进栈 int x = S.top(); //取栈顶元素 S.pop(); //出栈 S.empty(); //判空
#include<queue> queue<int> Q; Q.push(x) ; //将元素放入队尾 x = Q.front(); //读取队头元素,将其值赋值给x Q.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.
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 2 0 1 1 2 2 2 0 1 1 0 0 0
输出
YES NO
这道题大意是判断师徒关系是否正确,徒弟越多越厉害。就要避免有一些人刷师徒关系,比如说A是B的徒弟,B又是A的徒弟,这就不合法了。这就可以转换为判断是否是一个有向无环图的问题了。如果是有向无环图那么该关系是合法的,若存在环则不合法。
那拓扑排序是如何判断有没有环的呢?如果所有的结点尚未完全被删去时就出现找不到入度为0的结点的情况,说明剩余的结点形成一个环路。
1 #include<stdio.h> 2 3 #include<queue> 4 #include<vector> 5 using namespace std; 6 7 vector<int> edge[501]; //邻接链表,因为边不存在权值,只需保存与其邻接的结点编号即可 8 queue<int> 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<n; i++) 24 { 25 inDegree[i] = 0; //初始化入度 26 edge[i].clear(); 27 } 28 while( m--) 29 { 30 scanf("%d%d",&a,&b); 31 inDegree[b]++; //出现一条指向b的边,累加结点b的入度 32 edge[a].push_back(b); //将b加入a的邻接链表 33 } 34 while( Q.empty()==false) Q.pop(); //清空队列中所有的元素 35 for( i=0; i<n; i++) 36 { 37 if( inDegree[i]==0 ) Q.push(i); //若结点度数为0,则将其放入队列 38 } 39 while( Q.empty()==false ) 40 { 41 newP = Q.front(); //读出队头结点编号 42 Q.pop(); //弹出队头元素 43 cnt++; //被确定的结点个数加一 44 for( i=0; i<edge[newP].size(); i++) 45 { 46 //将该结点以及以其为弧尾的所有边去除 47 inDegree[edge[newP][i]]--; 48 if( inDegree[edge[newP][i]]==0) 49 { 50 Q.push(edge[newP][i]); 51 } 52 } 53 } 54 if( cnt==n) printf("YES "); //若所有结点都能被确定为拓扑序列, 55 //则原图为有向无环图 56 else printf("NO "); 57 } 58 59 return 0; 60 }