Legal or not
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 5885 Accepted Submission (s): 2726
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 INPUT3 0 Sample Outputyesno
Author[email protected]
Test instructions: Input data n,m, indicating that there are n people next m line, each line input x, y means X is the master of y;
If A is B's Master B is master of C, then A is the master of C.
If a is the master of B, the Master of B and a is not legally output no, if the legal output Yes
1, if the input point is not dependent on the point (looping) output No
2. The number of nodes that do not depend on vertices in the final result is less than n does not conform to test instructions
#include <stdio.h> #include <string.h> #include <queue>using namespace Std;int n,m;int map[110][110]; int vis[110];void Getmap () {int i,j,a,b; memset (vis,0,sizeof (VIS)); memset (map,0,sizeof (map)); for (i=1;i<=m;i++) {scanf ("%d%d", &a,&b); if (!map[a][b]) {vis[b]++; Map[a][b]=1; }}}void Tuopu () {int i,j,sum=0; int ok=0; queue<int>q; while (!q.empty ()) Q.pop (); for (i=0;i<n;i++) {if (vis[i]==0) {sum++; Q.push (i); }} if (sum==0) ok=1;//There is no vertex-dependent node (looping) else {int u,ans=0 in the start diagram; while (!q.empty ()) {U=q.front (); ans++; Q.pop (); for (i=0;i<n;i++) {if (Map[u][i]) {vis[i]--; if (vis[i]==0) Q.push (i); }}} if (Ans<n)The number of nodes that are not dependent on vertices after the final sort is less than n ok=1;//that the ring does not conform to test instructions} if (ok==0) printf ("yes\n"); else printf ("no\n");} int main () {int i,j; while (scanf ("%d%d", &n,&m), n|m) {getmap (); Tuopu (); } return 0;}
Hdoj 3342 Legal or not "topological sort"