HDU 3342 Legal or Not (有向图的拓扑排序)

来源:互联网 发布:桌椅板凳淘宝 编辑:程序博客网 时间:2024/05/29 17:01

Legal or Not

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

Problem Description
ACM-DIYis a large QQ group where many excellent acmers get together. It is soharmonious that just like a big family. Every day,many "holy cows" likeHH, hh, AC, ZT, lcc, BF, Qinz and so on chat on-line to exchange theirideas. When someone has questions, many warm-hearted cows like Lostwill 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 pairsof "master and prentice". But then problem occurs: there are too manymasters and too many prentices, how can we know whether it is legal ornot?

We all know a master can have many prentices and a prenticemay have a lot of masters too, it's legal. Nevertheless,some cows arenot so honest, they hold illegal relationship. Take HH and 3xian forinstant, HH is 3xian's master and, at the same time, 3xian is HH'smaster,which is quite illegal! To avoid this,please help us to judgewhether their relationship is legal or not.

Please note thatthe "master and prentice" relation is transitive. It means that if A isB's master ans B is C's master, then A is C's master.
 

 

Input
Theinput consists of several test cases. For each case, the first linecontains two integers, N (members to be tested) and M (relationships tobe tested)(2 <= N, M <= 100). Then M lines follow, each containsa pair of (x, y) which means x is y's master and y is x's prentice. Theinput 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
 

解题思路:

      有向图的拓扑排序。

原创粉丝点击