Title Description: https://vjudge.net/problem/UVA-10305
Topic Analysis:
Hate the topic of water, as long as learn toposort will do, probably mean to give you n variables, m unequal relationship represents A<b, ask N number of possible relations; As an example n=3 represents 3 variables we would have two if they were a,b,c now A<b,a<c
Then the output has two kinds of a<b<c or a<c<b (the topic requires the output of any one);
The topic is probably that, so how do we do it, we think about if the variable as a point, the relationship as a forward edge, then get a graph, this diagram has some special properties:
1. If there is a point in the degree (that is, one or more points can reach this point), then this point corresponding to the variable must not be the smallest (good understanding of it).
2. If a point entry is equal to 0 (that is, no point can reach it), then it may be the smallest (think, why), of course, if a point does not have a degree (that is, it can not reach other points), it may be the largest.
3. Under the conditions of test instructions, there can be no ring in the diagram, because if there is a ring description must be from a point to go back to the current point, then there is the point corresponding to the variable is less than its own (certainly not possible), such as 3<3 is obviously not possible (this is why there is no map and the ring diagram is not toposort ).
What is the use of these properties? Some readers find that it is not possible to determine unequal relationships by graphs. Because the topic requires the output of a possible unequal relationship, we can now find a possible minimum number, and make it the first number to output. So we know the smallest possible variable, So how do we keep looking for the second one, and we think that the smallest find, the first point that he can reach is probably the second-smallest variable, then we just need to delete the point and the side that is connected to him (actually only the side that points to the other point, because the penetration is 0). Just repeat this operation all the time, You can complete the solution. In fact, the determination of the unequal relationship has a very good (between cattle a bull c) name, that is, topological sorting (ie, toposort).
The above mentioned that only the direction of the non-circular graph has toposort, now give an explanation, because Toposort is the graph (accurate is the direction of the graph), arranged into a linear relationship of the process, then if there is a ring, there is a point in the degree must not be 0, to the end is the cycle of death ( See the following code to know) ... Can actually be understood by the size of the real number of the relationship because there is no one to make himself smaller than himself, so there must not be a ring ah, then there is no map of what is the situation? No direction graph is the point from any side can reach each other, then this is not the ring (A-B,B-A==A-A).
So here's how the code is written (and finally the point is ...) ...
First we need to save the diagram, with the matrix, the adjacency table is OK, because the topic up to 100 variables, that is, a maximum of 100 points, with the matrix will be good to write more ...
The use of an ans array means that there may be unequal relationships, with indefree representing the degree of a point (that is, how many points can go to this point).
So let's look at the code implementation ....
The Ugly Code:
#include <bits/stdc++.h>Const intMAXN =101;using namespacestd;intn,m;BOOLG[MAXN][MAXN];intINDEGREE[MAXN];intANS[MAXN];voidToposort () {//Core Codequeue<int> q;//can use the stack, the impact is not small, can also be handwritten intCnt=0;//It means a few variables are lined up. for(intI=0; i<n;i++) if(indegree[i]==0) Q.push (i);//according to the analysis described first to find the degree of zero, put in the queue to prepare for processing while(!Q.empty ()) { intTmp=q.front ();//find the one that is currently the smallest possibleQ.pop ();//Throw awayans[cnt++]=tmp;//put it in the ANS array. for(intI=0; i<n;i++)//where the Soul is if(g[tmp][i]==1&& (--indegree[i]==0) ) Q.push (i); //Delete the operation of the point and edge, but actually do not need to delete, because there is a queue, the order in the queue is a possible order, delete the point and then put into the degree of 0, instead of directly put in } //Outputcout<<ans[0]+1; for(intI=1; i<cnt;i++) cout<<" "<<ans[i]+1; cout<<Endl;}intMain () {Ios::sync_with_stdio (false); while(cin>>n>>m&& (m| | N)) {//input does not explainmemset (G,0,sizeof(G)); memset (Indegree,0,sizeof(Indegree));//Initialize intb; for(intI=0; i<m;i++) {cin>>a>>b; G[a-1][b-1]=true;//Matrix Storage Diagramindegree[b-1]++;//in degrees (no point can reach B then the entry of point B is increased by 1)} toposort (); } return 0;}
Just here, slipped away, do not understand to ask qq624626089 ...
UVA10305 ordering Tasks (--toposort) Kahn algorithm