The core of the topology sequencing is the point at which each 0 is found, entering the output queue, and then reducing the node-in connection to this point by 1
Repeat for the above operation.
Because each point on the ring is 1 without 0, it cannot be updated.
That is, the topology is sorted once, and if it is a DAG, all the points are queued exactly once.
If there is a ring, then there must be no queue of points.
Examples:
Legal or not
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
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
Test instructions: Test instructions: Give a diagram that does not necessarily connect, judge whether there is a ring in the graph
Solution:
Topological ordering of the rings.
1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cmath>5#include <cstring>6#include <map>7#include <queue>8#include <vector>9 #defineMoD 10000Ten #defineINF 336860180 One #definePI 3.1415926 A #definell Long Long - using namespacestd; - Const intn=1e6+ +; the intn,m,x,y,cnt;//CNT statistics Enqueue points - structnode{ - intU,v,c,ne; - }e[n]; + intH[n],tot,du[n]; - voidAddintUintVintc) + { Adu[v]++; atTot++;e[tot]= (node) {u,v,c,h[u]};h[u]=tot; - } -queue<int>Q; - BOOLTuopu () - { - for(intI=1; i<=n;++i) in if(du[i]==0) Q.push (i); - while(!q.empty ()) to { + intFf=q.front (); Q.pop (); cnt++; - for(intI=h[ff];i;i=e[i].ne) the { * intRr=e[i].v; $du[rr]--;Panax Notoginseng if(du[rr]==0) Q.push (RR); - } the } + if(cnt==n)return 1;//If a bit is not updated, the description has a ring A return 0; the } + intMain () - { $ while(SCANF ("%d%d", &n,&m)! =EOF) $ { - if(n==0&& m==0) Break; -tot=0; cnt=0; the for(intI=1; i<=n;++i) h[i]=du[i]=0; - for(intI=1; i<=m;++i)Wuyi { thescanf"%d%d",&x,&y); -X++,y++;add (x, Y,0); Wu } - if(Tuopu ()) puts ("YES"); About ElsePuts"NO"); $ } - return 0; -}
View Code
(?′?‵?) I L???????
Topological sorting and sentencing