Legal or not
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 6742 Accepted Submission (s): 3180
Problem Description
Acm-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.
Input
The 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.
Output
For each test case, print on one line the judgement of the messy relationship.
If It is legal, output "YES", otherwise "NO".
Sample Input
3 2
0 1
1 2
2 2
0 1
1 0
0 0
Sample Output
YES
NO
Problem: The general meaning of the question is: A to B to ask, B is a master, A is B's apprentice, a person can have more than one apprentice, can also have a number of masters, But it cannot be the master and apprentice of the same person (not lawfully) or the master of a person in the whole circle, and also his apprentice (for example, A is the master of B, The Apprentice of A is B, or the master of a in a large circle, and B is the Master of C).
A is the disciple of C) (all illegal)
The main meaning is to let the judge whether there is a ring (if you can do a topological sort, it means no ring, otherwise there is a ring)
Code: (using an array to simulate relationships)
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include < String> #include <stack>using namespace Std;const int Maxx=105;int map[maxx][maxx];///record the relationship between the two int coun[maxx] ;////Record entry degree int n,m;int find () {int ans; for (int k=0; k<n; k++) {ans=n;///each round to see if there is a point of 0 for (int i=0; i<n; i++) {if (CO un[i]==0) {coun[i]--; Ans=i; Break }} if (Ans==n)///If there are no points with an entry level of 0 there is a ring return of 0; for (int j=0; j<n; J + +) if (Map[ans][j]) coun[j]--;///The other point-in degrees with the 0-in point minus one} return 1;} int main () {while (~SCANF ("%d%d", &n,&m)} {if (n==0&&m==0) break; memset (map,0,sizeof (map)); memset (coun,0,sizeof (Coun)); for (int i=0; i<m; i++) {int u,v; scanf ("%d%d", &u,&v); if (!map[u][v])///There may be duplicate relationship removal {map[u][v]=1; coun[v]++; In Degrees Plus one}} int flag=find (); if (flag)///If topological sorting is possible, then the ring-free printf ("yes\n") is represented; else printf ("no\n"); } return 0;}
Code Listing 2: The adjacency table is used.
Topological sequencing to determine if there is a ring (HDU 3342) #include <iostream> #include <algorithm> #include <cstdio> #include < Cstring> #include <string>using namespace std;const int maxx=505;struct arode{int to; struct Arode *next;}; Arode *list[maxx];//adjacency table int coun[maxx];///input degree char output[maxx];///output table is not available at this time (without output sort order) int n,m;void topsort () {Arode * Temp int i; int top=-1; BOOL Bycile=false; for (int i=n-1;i>=0;i--) {if (coun[i]==0) {coun[i]=top; Top=i; }} int pos=0; for (int i=0;i<n;i++) {if (top==-1) {bycile=true; Break } int j=top; Top=coun[top]; pos+=sprintf (Output+pos, "%d", j+1);///The Order of output, at this time not used temp=list[j]; while (temp!=null) {int k=temp->to; if (--coun[k]==0) {coun[k]=top; Top=k; } temp=temp->next; }} if (Bycile) printf ("no\n"); else printf ("yes\n");} int main () {while (scanf ("%d%d", &n,&m) && (n+m)) {Arode *temp; memset (coun,0,sizeof (Coun)); memset (output,0,sizeof (output)); memset (list,0,sizeof (List)); int i,j; int u,v; for (i=0;i<m;i++) {scanf ("%d%d", &u,&v); coun[v]++; Temp=new Arode; temp->to=v; temp->next=null; if (list[u]==null) list[u]=temp; else {temp->next=list[u]; List[u]=temp; }}///topological sort function topsort (); Frees the space occupied by the adjacency table for (j=0;j<n;j++) {temp=list[i]; while (temp!=null) {list[i]=temp->next; Delete temp; Temp =list[i]; }}} return 0;}
HDU 3342 Legal or not (topological sort)