Legal or notTime
limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 6069 Accepted Submission (s): 2818
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
Author[email protected]
Sourcehdoj Monthly contest–2010.03.06
The meaning is very simple, is to ask whether the graph can be topological sort, can output "YES", otherwise output "NO".
accepted//31ms1660k//hdu3342 (topological sort template) #include <cstdio> #include <cstring>const int N = 120;int Map[n][n], Indegree[n];int Topo (int N) {for (int i=0; i<n; i++) {int ans=n; for (int j=0; j<n; J + +) {if (indegree[j]==0) {//finds a point indegree[j]--with a degree of 0; Ans=j; Break }} if (Ans==n)//ans does not have an update stating that there are no nodes with an entry level of 0, that is, the topological sort cannot be returned directly 0 return 0; for (int j=0; j<n; J + +) if (map[ans][j]>0)//The entry of points connected to the 0 nodes selected above is reduced by 1 indegree[j]--; } return 1;} int main () {int m,n,p,q,flag; while (~SCANF ("%d%d", &n,&m) && n && m) {memset (map,0,sizeof (map)); memset (indegree,0,sizeof (Indegree)); for (int i=0; i<m; i++) {scanf ("%d%d", &p,&q); if (map[p][q]==0) {//must be judged here, otherwise it will affect the value of map[p][q]=1; indegree[q]++; }} flag=topo (n); printf ("%s\n", flag?) YES ":" NO "); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 3342 Legal or not