HDU 3342 Legal or not topological sort judgment ring. Now I, in addition to the water problem, what can I do

Source: Internet
Author: User

Legal or notTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 4960 Accepted Submission (s): 2270


Problem Descriptionacm-diy is a large QQ group where many excellent acmers get together. It's so harmonious this just like a big family. Every Day,many "holy cows" like HH, hh, AC, ZT, LCC, BF, Qinz and so on chats on-line to exchange their ideas. When someone had questions, many warm-hearted cows like Lost would come to help. Then the one being helped would call Lost "master", and Lost would have a nice "Prentice". By and by, there is many pairs of "Master and Prentice". But then problem Occurs:there was too many masters and too many prentices, how can we know whether it's legal or not?

We all know a master can has many prentices and a Prentice may has a lot of masters too, it ' s legal. Nevertheless,some cows is not so honest, they hold illegal relationship. Take HH and 3xian for instant, HH was 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 are legal or not.

Please note that the ' Master and Prentice ' relation is transitive. It means 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-integers, N (members to is 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 are 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 on 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
There's nothing to talk about. It will be the topological template problem Direct code:
#include <stdio.h> #include <string.h> #define MAX 110int Graph[max][max], in[max];bool Visited[max]; int Main () {int m,n;while (~scanf ("%d%d", &n,&m) && N) {memset (graph); graph,0,sizeof (memset), False,sizeof (visited)); Memset (in,0,sizeof ()); for (int i = 0; i < m; ++i) {int x, y; scanf ("%d%d", &x,&y); I F (graph[x][y] = = 0) {Graph[x][y] = 1; in[y] + +;}} int count = 0, for (int i = 0, i < n; ++i) {for (int j = 0; J < N; ++j) {if (!visited[j] && in[j] = = 0) {count + +; Visited[j] = true; for (int k = 0; k < n; ++k) {if (graph[j][k] = = 1) {Graph[j][k] = 0; in[k]--;}} break; }}}if (Count < N) {puts ("NO");} Else{puts ("YES");}} return 0;}

with June

HDU 3342 Legal or not topological sort judgment ring. Now I, in addition to the water problem, what can I do

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.