HDU 3342 Legal or not (shortest topology topology sort?)

Source: Internet
Author: User

Legal or not

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 6580 Accepted Submission (s): 3088


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 20 11 22 20 11 00 0

Sample Outputyesno

Author[email protected]

Sourcehdoj Monthly contest–2010.03.06

Recommendlcy | We have a carefully selected several similar problems for you:1285 2647 3333 3339 3341 Look at some of the other, topological sort? Scare me, but there is a simple solution Floyd run it all the way. If a road mat[i][j] and Mat[j][i] are not inf, it means no.
#include <algorithm>#include<stdio.h>#include<iostream>using namespacestd;#defineN 112345678#defineM 111#defineINF 0x3f3f3f3fintn,m,a,b,x,y,t;intMat[m][m];voidinit () { for(inti =0; i < M; i++)     for(intj =0; J < M; J + +) Mat[i][j]=INF;}intMain () { while(Cin>>n>>m &&N) {init (); BOOLFlag =true;  while(m--) {scanf ("%d%d", &x, &y); Mat[x][y]=1; }         for(intK =0; K < n; k++)             for(inti =0; I < n; i++)            if(Mat[i][k]! = INF)//Adding this question will save you a lot of time! Plus 93MS without 748MS.             for(intj =0; J < N; J + +)                if(Mat[i][j] > Mat[i][k] +Mat[k][j]) mat[i][j]= Mat[i][k] +Mat[k][j];  for(inti =0; I < n; i++)             for(intj =0; J < N; J + +)                if(I! =j)if(Mat[i][j]! = INF && mat[j][i]! =INF) {Flag=false;  Break; }        if(!flag) puts ("NO"); Elseputs ("YES"); }    return 0;}

After Floyd Timeout can try to add this code

 for (int0; k < n; k++)              for (int0; i < n; i++)             if // Adding this question will save you a lot of time!  Added 93MS without 748MSfor            (int0; j < N; j + +)

HDU 3342 Legal or not (shortest topology topology sort?)

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.