Link:
http://poj.org/problem?id=1094
Topic:
Sorting It All Out
Time limit:1000ms Memory limit:10000k
Total submissions:21532 accepted:7403
Description
An ascending sorted sequence of distinct values are one in which some form of a less-than operator are used to order the Ele ments from smallest to largest. For example, the sorted sequence A, B, C, D implies that's a < B, b < C and C < D. In this problem, we'll give yo U a set of relations of the form a < B and ask your to determine whether a sorted order has been or not.
Input
Input consists of multiple problem instances. Each instance starts with a line containing two positive integers n and m. The number of indicated To sort, where 2 <= n <= 26. The objects to be sorted would be the the "the" characters of the uppercase alphabet. The second value m indicates the number of relations of the form A < B which'll is given in this problem instance. Next'll be M lines, each containing one such relation consisting of three Characters:an uppercase letter, the character "<" and a second uppercase letter. No letter would be outside the range of the the ' the ' of the ' the ' of the ' letters of the ' alphabet. Values of n = m = 0 indicate end of input.
Output
For each problem instance, output consists of the one line. This line should is one of the following three:
Sorted sequence determined after xxx relations:yyy...y.
Sorted sequence cannot be determined.
Inconsistency found after xxx relations.
where xxx is the number of relations processed in the time either a sorted sequence are determined or an inconsistency is F Ound, whichever comes, and yyy...y is the sorted, ascending sequence.
Sample Input
4 6
A<b
A<c
B<c
C<d
B<d
A<b
3 2
A<b
B<a
26 1
A<z
0 0
Sample Output
Sorted sequence determined after 4 RELATIONS:ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.
Analysis:
This is a typical topological sort, but it changes a bit.
The three types of output from the sample of the topic are:
1. In the first X relationship can be uniquely determined by the sort and output.
2. There are loops (inconsisitency contradictions) found in the X relationship
3. All relationships are not found in the above two cases, the output of the 3rd.
So for a given m relationship, read one by one, each read into a topological sort, and if 1 and 2 are found, then there is no need to consider the following relationships, but continue to read the following relationships (but do not deal with them). If you have finished reading all the relationships and have not seen 1 and 2, then the output is 3.
This column more highlights: http://www.bianceng.cn/Programming/sjjg/
There are two methods of topological ordering, one is the introduction of the algorithm, one is the greedy thought, this problem with greedy thought to do better.
The greedy approach:
1. Find all points with entry of 0, join queue Q
2. Take out a point of the queue Q, starting with this point, all of its end points are reduced by 1. If the process is found to be reduced by 1 and then into 0, add this point to the queue Q.
3. Repeat step 2 until Q is empty.
In this process, if there are multiple points at the same level of 0, the description cannot uniquely determine the relationship.
If the resulting sorted point is less than the total number of points, then the loop is shown.
The topic also needs to note: If the Edge (U,V) has been entered before, then this side will not join.
Code:
#include <cstdio> #include <cstring> #include <vector> #include <queue> using namespace std;
const int N = 105;
int N,m,in[n],temp[n],sort[n],t,pos, num;
Char X, O, Y;
vector<int>g[n];
queue<int>q;
void Init () {memset (in, 0, sizeof (in));
for (int i=0; i<=n; ++i) {g[i].clear ();
} inline bool Find (int U,int v) {for (int i=0; I<g[u].size (), ++i) if (g[u][i]==v) return true;
return false;
int Toposort () {while (!q.empty ()) Q.pop ();
for (int i=0; i<n; ++i) if (in[i]==0) {Q.push (i);
} pos=0;
BOOL Unsure=false;
while (!q.empty ()) {if (Q.size () >1) unsure=true;
int T=q.front ();
Q.pop ();
sort[pos++]=t;
for (int i=0; i<g[t].size (); ++i) {if (--in[g[t][i]]==0) Q.push (g[t][i));
} if (pos<n) return 1; if (UnsuRE) return 2;
return 3;
int main () {int x,y,i,flag,ok,stop; while (~SCANF ("%d%d%*c", &n,&m)) {if (!n| |!
m) break;
Init ();
flag=2;
Ok=false;
For (I=1 i<=m; ++i) {scanf ("%c%c%c%*c", &x,&o,&y); if (OK) continue;
If it has been judged that there is a loop or can be a unique sort, do not process but continue reading x=x-' a ', y=y-' a ';
if (o== ' < ' &&!find (y,x)) {g[y].push_back (x);
++IN[X];
else if (o== ' > ' &&!find (x,y)) {g[x].push_back (y);
++in[y];
//Copy a copy, and so on to restore the in array memcpy (temp, in, sizeof (in));
Flag=toposort ();
memcpy (in, temp, sizeof (temp));
if (flag!=2) {stop=i;
Ok=true; } if (flag==3) {printf ("Sorted SequencE determined after%d relations: ", stop);
for (int i=pos-1; i>=0;-i) printf ("%c", sort[i]+ ' A ');
printf (". \ n");
else if (flag==1) {printf ("inconsistency found after%d relations.\n", stop);
else{printf ("Sorted sequence cannot be determined.\n");
} return 0; }