hdu 3342 Legal or Not(拓扑)

来源:互联网 发布:怎样用淘宝客推广 编辑:程序博客网 时间:2024/06/02 16:16

Legal or Not

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5781    Accepted Submission(s): 2672


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 20 11 22 20 11 00 0
 

Sample Output
YESNO
 

Author
QiuQiu@NJFU
 

Source

HDOJ Monthly Contest – 2010.03.06 

拓扑排序判断环: 
开始我用每次排序进队之后删除那条边,最后判断还存不存在边,wa。 
然后我把改成判断排序完每个点的入度,不全为0就存在环,依然wa。 
最后我有判断每次出队的个数是不是等于点的总数,还是wa。 
最后找了半天代码终于发现题目要求是输出是YES,NO,我写成Yes,No,,我又把前边写的代码输出都改了,于是,,
都AC了,,,,额,,,,,此处省略10000字,,,血淋淋的教训,,,读题啊!!!!!! 

#include<stdio.h>#include<string.h>#include<queue>using namespace std;#define M 510int n,m;int mp[M][M],du[M];void topo(){int i,j,count=0;queue <int> q;for(i=0;i<n;i++){if(du[i]==0)q.push(i);}while(!q.empty()){int tmp=q.front();q.pop(); count++;//判断出队的点的个数 for(i=0;i<n;i++){if(mp[tmp][i]==1){if(--du[i]==0){q.push(i);}}}}if(count!=n) printf("NO\n");else printf("YES\n"); } int main(){int i,a,b;while (scanf("%d%d",&n,&m),n+m){ memset(mp,0,sizeof(mp));memset(du,0,sizeof(du));for(i=0;i<m;i++){scanf("%d%d",&a,&b);if(!mp[a][b]){//有重边呦 mp[a][b]=1;du[b]++;}}topo();}return 0;}


0 0
原创粉丝点击