Problem descriptionacm-DIY is a large QQ group where your excellent acmers get together. it is so harmonious that just like a big family. every day, daily "holy cows" like HH, HH, AC, ZT, KP, BF, qinz and so on chat on-line to exchange their ideas. when someone has questions, warm-hearted cows like lost will come to help. then the one being helped will call lost "master", and lost will have a n Ice "Prentice ". by and by, there are using pairs of "master and Prentice ". but then problem occurs: there are too processing masters and too processing prentices, how can we know whether it is legal or not?
We all know a master can have tables 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 3 Xian for instant, HH is 3xian's master and, at the same time, 3 Xian 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.
Inputthe 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.
Outputfor each test case, print in one line the judgement of the messy relationship.
If it is legal, output "yes", otherwise "no ".
Sample input3 20 11 22 20 11 00 0 sample outputyesno is an application of topological sorting. Determine whether the link is a directed acyclic graph. The Code is as follows:
# Include <stdio. h> # include <string. h> int indegree [101], map [101] [101]; // indegree [] records the inbound degrees of each node. Int s [101], top; // a node with a degree of 0 stored in stack s. Void tuopu (int n) {int I, j, Top = 0, T, Count = 0; memset (S, 0, sizeof (s); for (I = 0; I <n; I ++) {If (0 = indegree [I]) {s [top ++] = I ;}} while (top> 0) {T = s [Top-1]; top --; count ++; For (j = 0; j <n; j ++) {If (Map [T] [J]) {indegree [J] --; If (0 = indegree [J]) {s [top ++] = J ;}}} if (count <n) printf ("NO \ n "); // if the value of count is less than N, else printf ("Yes \ n");} int main () {int n, m, I, j, A, B; while (scanf ("% d", & N, & M) & N & M) {memset (MAP, 0, Si Zeof (MAP); memset (indegree, 0, sizeof (indegree); for (I = 0; I <m; I ++) {scanf ("% d", & A, & B); If (! Map [a] [B]) {map [a] [B] = 1; indegree [B] ++ ;}} tuopu (n) ;}return 0 ;}