HDU 3342 legal or not (topological sorting of Directed Graphs)

Source: Internet
Author: User
Legal or not

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)

Total submission (s): 700 accepted submission (s): 295

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, should "holy cows" like
HH, HH, AC, ZT, KP, BF, qinz and so on chat on-line to exchange their
Ideas. When someone has questions, export warm-hearted cows like lost
Will come to help. Then the one being helped will call lost "master ",
And lost will have a nice "Prentice". By and by, there are using pairs
Of "master and Prentice". But then problem occurs: there are too Enabled
Masters and too tables prentices, how can we know whether it is legal or
Not?

We all know a master can have reset 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
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
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.
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 Input
3 20 11 22 20 11 00 0
 

 

Sample output
YESNO
 

Solution:

Topological sorting of Directed Graphs.

# Include <iostream> <br/> using namespace STD; <br/> # define maxvertexnum 100 <br/> typedef int vertextype; <br/> typedef struct node // edge table node <br/>{< br/> int adjvex; // adjacent point field <br/> struct node * next; // Chain Domain <br/> // to indicate the right on the edge, add a data domain <br/>} edgenode; <br/> typedef struct vnode // vertex table node <br/> {<br/> vertextype vertex; // vertex field <br/> edgenode * firstedge; // edge header pointer <br/> int count; <br/>} vertexnode; <br/> typedef vertexnode Djlist [maxvertexnum]; // adjlist is an adjacent table Type <br/> typedef struct <br/>{< br/> adjlist; // adjacent table <br/> int N, E; // Number of vertices and edges <br/>} algraph; // This type is not required for simple applications, you can directly use the adjlist type <br/> bool createalgraph (algraph * g) <br/>{< br/> int I, j, k; <br/> edgenode * s; <br/> CIN> G-> N> G-> E; <br/> If (G-> N = 0) <br/> return false; <br/> for (I = 0; I <G-> N; I ++) <br/> {<br/> G-> adjlist [I]. vertex = I; <br/> G-> adjlist [I]. firstedge = nu Ll; <br/>}< br/> for (k = 0; k <G-> E; k ++) <br/>{< br/> CIN> j> I; <br/> S = (edgenode *) malloc (sizeof (edgenode )); <br/> S-> adjvex = J; <br/> S-> next = G-> adjlist [I]. firstedge; <br/> G-> adjlist [I]. firstedge = s; <br/>}< br/> return true; <br/>}< br/> void topsort (algraph * g) <br/>{< br/> int I, j, k = 0; <br/> int st [maxvertexnum], Top =-1; <br/> edgenode * P; <br/> for (I = 0; I <G-> N; I ++) <br/> G-> adjlist [I]. cou Nt = 0; <br/> for (I = 0; I <G-> N; I ++) <br/>{< br/> P = G-> adjlist [I]. firstedge; <br/> while (P! = NULL) <br/>{< br/> G-> adjlist [p-> adjvex]. count ++; <br/> P = p-> next; <br/>}< br/> for (I = 0; I <G-> N; I ++) <br/>{< br/> If (G-> adjlist [I]. count = 0) <br/>{< br/> top ++; <br/> st [Top] = I; <br/>}< br/> while (top>-1) <br/>{< br/> I = sT [Top]; <br/> top --; <br/> K ++; <br/> // printf ("% d", I ); <br/> P = G-> adjlist [I]. firstedge; <br/> while (P! = NULL) <br/>{< br/> J = p-> adjvex; <br/> G-> adjlist [J]. count --; <br/> If (G-> adjlist [J]. count = 0) <br/>{< br/> top ++; <br/> st [Top] = J; <br/>}< br/> P = p-> next; <br/>}< br/> If (k <G-> N) <br/> cout <"no" <Endl; <br/> else <br/> cout <"yes" <Endl; <br/>}< br/> int main () <br/>{< br/> algraph g; <br/> while (createalgraph (& G )) <br/> topsort (& G); <br/> return 0; <br/>}

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.