Topic Links:
Ah haha, choose me
Topic:
Sorting It All Out
Time Limit: 1000MS 

Memory Limit: 10000K 
Total Submissions: 26897 

Accepted: 9281 
Description An ascending sorted sequence of distinct values are one in which some form of a lessthan operator are used to order the Ele ments from smallest to largest. For example, the sorted sequence A, B, C, D implies, 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 you to determine whether a sorted order have been specified or not.Input Input consists of multiple problem instances. Each instance starts with a line containingpositive integers n and m. The first value indicated the number of objects To sort, where 2 <= n <= 26. The objects to be sorted'll be the first n characters of the uppercase alphabet. The second value m indicates the number of relations of the form A < B which'll be 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 first n letters of the alphabet. Values of n = m = 0 indicate end of input.Output For each problem instance, the output consists of 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 at the time either a sorted sequence are determined or an inconsistency is F Ound, whichever comes first, and yyy...y is the sorted, ascending sequence.
Sample Input 4 6a<ba<cb<cc<db<da<b3 2a<bb<a26 1a<z0 0
Sample Output Sorted sequence determined after 4 RELATIONS:ABCD. Inconsistency found after 2 relations. Sorted sequence cannot be determined.
Source field=source&key=east+central+north+america+2001 "style=" Textdecoration:none ">East Central North America 2001 
This topic considers the topological ordering very carefully.
Consider the case of a loop. The looping is the point where the final presence is not 0.
The last num after the count is not equal to N.
The problem is that this topic is not a comprehensive inference of all the information, but based on the previous assumptions can be obtained already ring, or can get the size of n order, then the following will not be inferred ... So use OK1,OK2 two variables to control.
。
The code is:
#include <cstdio> #include <stack> #include <iostream> #include <cstring> #include < algorithm>using namespace Std;const int maxn=26+10;int n,m;int IN[MAXN],COPY[MAXN],MAP[MAXN][MAXN],TEMP[MAXN]; Stack<int>s;int topo () {int flag=0,num=0; while (! S.empty ()) S.pop (); memcpy (Copy,in,sizeof (in)); for (int i=0;i<n;i++) {if (copy[i]==0) s.push (i); } while (! S.empty ()) {if (S.size () >1) flag=1; int Gery=s.top (); S.pop (); Temp[num++]=gery; for (int i=0;i<n;i++) {if (Map[gery][i]) {if (copy[i]==0) S.push (i); }}} if (Num!=n) return 0;//into a loop. is already able to identify the relationship and be able to mark it. If the IF (flag) return 1;//has multiple points with an entry level of 0, it is not yet determined. Continue to enter information. Add conditions to see if you can get the order. Return 2;//order has been determined. able to mark.}int Main () {char STR[MAXN]; int ok1,ok2,u,v,i,is_n; while (~SCANF ("%d%d", &n,&m), n,m) {is_n=0; memset (In,0,sizeof (in)); memset (map,0,sizeof (map)); ok1=ok2=0; for (i=1;i<=m;i++) {scanf ("%s", str); if (!ok1&&!ok2) {u=str[0]' A '; v=str[2]' A '; if (map[u][v]==0) {map[u][v]=1; in[v]++; } int Ans=topo (); if (ans==0) {is_n=i; Ok2=1; } else if (ans==2) {is_n=i; Ok1=1; }}} if (Ok1) {printf ("Sorted sequence determined after%d relations:", is_n); for (int i=0;i<n1;i++) printf ("%c", temp[i]+ ' A '); printf ("%c.\n", temp[n1]+ 'A '); } if (OK2) printf ("Inconsistency found after%d relations.\n", is_n); if (ok1==0&&ok2==0) printf ("Sorted sequence cannot be determined.\n"); } return 0;}
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
Poj1094sorting It All Out