The application of shallow Shang flutter sort in Oi
by Medalplus
"Definition of topological sorting"
Example:
There are many disciplines in the university need to learn, and some disciplines need to learn other disciplines to learn, for example, must first learn math and then learn calculus ... This is the relationship of a topological order
"Application of topological sequencing"
For the above topics, it can be assumed that if learning a needs to learn B, from B to a side to a, and then to the entire graph to find a topological sequence, this is a learning scheme
Obviously, the topology sort should be from 0, then 1, then 2 ... To find
"Implementation of topology sequencing"
It is easy to think of an algorithm, brute force enumeration of each point, and then find the point connected to it, delete the edge, and put the point in the degree of 1, again looking for, know that can not be found until
The bare algorithm is O (n2)
We find that topological sequencing results in topological ordering, which can be modified with queues, while enumeration edges are time consuming, so the adjacency table is used to store
Time complexity O (n+e)
"Topological sort in the application of Oi"
Example 1. Depressed journalist
"problem description"
You are a journalist at a sports newspaper, and you have a difficult task: There are N football teams in the soccer game, now give you some results of the game, you need to give your team rankings, from 1 to N.
Here is some information for you:
(1) no draw;
(3) for all who meet 1≤a<b≤n, team A will be able to beat Team B.
gives you some of the results of the game and asks for a ranking.
"input format"
First line enter n (1≤n≤5000), which indicates the number of teams, numbered 1 through N.
The second line is input M (1≤m≤100000), which indicates the number of match fields given. Next m line, two integers per line xi,yi, indicates that Team XI can beat the team Yi.
"Output Format"
The output contains n rows, the first n lines describe the team's rankings, and the number of I is the team number that is ranked I.
Analysis
We can build a model, assuming that each team is a point, then if a can beat B then a to B has a straight edge.
Because of the ranking, it is obvious that the admission is 0 first, followed by second, third ....
Did you find it? This is the template for topological sorting!
we use O (n+e) code, see Appendix 1
Example 2. A strange Dream
Title Description Description
Aiden was caught in a strange dream: he was trapped in a small house with buttons on the wall and a screen showing some information. The screen said that to press all the buttons to go out, and give some information, indicating that a button can only be pressed after the other button, and the button is not mentioned can be pressed at any time. But Aiden found that the information on the screen seems to be contradictory, please help to judge.
Enter a description input Description
The first line, two number n,m, indicates that there is a number of 1 ... n these n buttons, the screen has m message.
The next M-line, two number Ai,bi per line, indicates that the BI button is pressed after the AI. The information given may be duplicated and guaranteed to be ai≠bi.
Outputs description Output Description
If the button can be pressed all, the output "O (∩_∩) o".
If not, the first line outputs "t_t", and the second line outputs the number of buttons that cannot confirm the order of the press because of conflicting information. The output does not include quotation marks.
Sample input to sample
3 3
1 2
2 3
3 2
Sample output Sample Outputs
t_t
2
Data Size & Hint
For 30% of data, ensure 0
For 50% of data, ensure 0
For 70% of data, ensure 0
For 100% of data, ensure 0
Analysis
First of all, it is not difficult to find the topic is the topological sort (0.0), each button is abstracted 10% points, a before B from a to the side of a line to B, and then the topological sort, statistics how many points have not been swept to
Time complexity O (n+m), no tle
Code See Appendix 2
Summary
Appendix 1.
1#include <iostream>2#include <cstdio>3#include <queue>4 using namespacestd;5 6 Const intmaxn=5001;7 Const intmaxm=1000001;8 9 int inch[MAXN];Ten intn,m,u,v; One A structedge{ - intnext,to; - }G[MAXM]; the - inthead[maxn],cnt; - - voidAddedge (intUintv) { +cnt++; -g[cnt].to=v; +g[cnt].next=Head[u]; Ahead[u]=CNT; at } - -queue<int>Q; - voidTop_sort () { - intu,i,j; - for(i=1; i<=n;i++) in if(!inch[i]) q.push (i); - while(!Q.empty ()) { tou=Q.front (); +cout<<u<<Endl; - Q.pop (); the for(j=head[u];j;j=g[j].next) * { $ inch[g[j].to]--;Panax Notoginseng if(inch[g[j].to]==0) - Q.push (g[j].to); the } + } A } the + intMain () { -Freopen ("rank.in","R", stdin); $Freopen ("Rank.out","W", stdout); $Cin>>n>>m; - inti; - for(i=1; i<=m;i++){ thescanf"%d%d",&u,&v); - Addedge (u,v);Wuyi inch[v]++; the } - Top_sort (); Wu return 0; -}
Appendix 2.
#include <iostream>#include<cstdio>#include<queue>using namespacestd;Const intmaxn=25005;structedge{intNext,to;} G[MAXN];inthead[maxn],cnt;intNminch[MAXN];intinq;voidAddedge (intUintv) {CNT++; G[cnt].to=v; G[cnt].next=Head[u]; Head[u]=CNT;}voidTopsort () {Queue<int>Q; intI,ahead; for(i=1; i<=n;i++) if(!inch[i]) q.push (i); while(!Q.empty ()) {Ahead=Q.front (); Q.pop (); Inq++; for(i=head[ahead];i;i=G[i].next) { inch[g[i].to]--; if(inch[g[i].to]==0) Q.push (g[i].to); } } if(inq==n) cout<<"o (∩_∩) o"; Elsecout<<"t_t"<<endl<<n-inq;}intMain () {CIN>>n>>m; intu,v; for(intI=1; i<=m;i++) {scanf ("%d%d",&u,&v); Addedge (U,V); inch[v]++; } topsort (); return 0;}
The application of shallow Shang flutter sort in Oi