Legal or Not
Legal or Not
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 15 Accepted Submission(s) : 13
Problem Description
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.
Input
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.
Output
For each test case, print in one line the judgement of the messy relationship. If it is legal, output "YES", otherwise "NO".
Sample Input
3 2
0 1
1 2
2 2
0 1
1 0
0 0
0 1
1 2
2 2
0 1
1 0
0 0
Sample Output
YES
NO
Author
QiuQiu@NJFU
Source
HDOJ Monthly Contest – 2010.03.06
题目大意:判断能否形成拓扑排序,能够的输出YES,否则输出NO。需要注意的是N个点是从0~N-1.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #define MAXN 1005 5 int InD[105]; 6 int Edge[105][105]; 7 void MakeSet(int Len) 8 { 9 int i; 10 for(i=0;i<=Len;i++) 11 InD[i]=0; 12 return; 13 } 14 int ToPoSort(int n,int* ret) 15 { 16 int i,j,k; 17 for(j=0;j<n;j++) 18 { 19 for(i=0;i<n;i++) 20 if(InD[i]==0) 21 { 22 InD[i]--; 23 ret[j]=i; 24 for(k=0;k<n;k++) 25 if(Edge[i][k]==1) 26 InD[k]--; 27 break; 28 } 29 if(i>=n)break; 30 } 31 if(j==n) 32 return 1; 33 else 34 return 0; 35 } 36 37 int main() 38 { 39 int N,M,i,a,b,ID[105]; 40 while(scanf("%d%d",&N,&M)!=EOF) 41 { 42 if(N==0&&M==0)return; 43 MakeSet(N); 44 memset(Edge,0,sizeof(Edge)); 45 memset(ID,0,sizeof(ID)); 46 for(i=0;i<M;i++) 47 { 48 scanf("%d%d",&a,&b); 49 if(Edge[a][b]==0) 50 { 51 Edge[a][b]=1; 52 InD[b]++; 53 } 54 } 55 if(ToPoSort(N,ID)) 56 printf("YES\n"); 57 else 58 printf("NO\n"); 59 } 60 return 0; 61 }
修改:2015.2.28
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <map> 5 using namespace std; 6 #define MAX 100010 7 int InD[MAX];/*InD[i]记录点i的入度*/ 8 int First[MAX];/*First[i]头结点的第一条边的编号*/ 9 struct edge 10 { 11 int TO;/*点*/ 12 int Next;/*下一条边的编号*/ 13 int Vlaue; /*权值*/ 14 }ID[3*MAX]; /*边表,无向图的边数记得多弄些*/ 15 int SIGN; /*链表的边数,链表的边数=无向图边数*2=有向图边数*/ 16 void Add_E(int x,int y)/*添加点+更新入度操作*/ 17 { 18 ID[SIGN].TO=y; 19 InD[y]++; 20 ID[SIGN].Next=First[x]; 21 First[x]=SIGN++; 22 } 23 int Jude(int x,int y)/*查找与X是否与Y相连,是0,否1*/ 24 { 25 int i; 26 for(i=First[x];i!=0;i=ID[i].Next) //查找与该点相关的点 27 { 28 if(ID[i].TO==y)return 0; 29 } 30 return 1; 31 } 32 int ToPoSort(int N,int Num[])/*能够排序返回1,否则返回0*/ 33 {/*N为点的个数(1~N),Num[]用来存储排序后的结果*/ 34 int i,j,k; 35 for(j=0;j<N;j++) 36 { 37 for(i=0;i<N;i++) 38 { 39 if(InD[i]==0) 40 { 41 InD[i]--; 42 Num[j]=i; 43 for(k=First[i];k!=0;k=ID[k].Next) 44 { 45 InD[ID[k].TO]--; 46 } 47 break; 48 } 49 } 50 if(i>N-1)break; 51 } 52 if(j==N)return 1; 53 else return 0; 54 } 55 int main() 56 { 57 int M,N,i; 58 int Num[MAX]; 59 while(scanf("%d%d",&N,&M)!=EOF) 60 { 61 int a,b; 62 if(M==0&&N==0)break; 63 for(i=0;i<=N;i++){First[i]=0;InD[i]=0;} 64 for(i=0,SIGN=1;i<M;i++) 65 { 66 scanf("%d%d",&a,&b); 67 if(Jude(a,b)) /*判断重边*/ 68 { 69 Add_E(a,b); 70 } 71 } 72 if(ToPoSort(N,Num)) printf("YES\n"); 73 else printf("NO\n"); 74 } 75 return 0; 76 }
转载请备注:
**************************************
* 作者: Wurq
* 博客: https://www.cnblogs.com/Wurq/
* Gitee: https://gitee.com/wurq
**************************************
**************************************
* 作者: Wurq
* 博客: https://www.cnblogs.com/Wurq/
* Gitee: https://gitee.com/wurq
**************************************