POJ 1094 sorting It all out topology sort

Source: Internet
Author: User
Tags uppercase letter

sorting It all OutTime limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld &%llu< /c8>

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, 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 containing-positive 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.



Set of templates
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include < queue> #include <vector> #include <iomanip> #include <math.h> #include <map>using namespace STD; #define FIN freopen ("Input.txt", "R", stdin), #define FOUT freopen ("Output.txt", "w", stdout); #define INF 0x3f3f 3f3f#define Lson l,m,rt<<1#define Rson m+1,r,rt<<1|1typedef long long ll;const int MAXN=1e5+5;struct EDG e{int v,nxt;} E[maxn*2];int head[maxn],erear;int in[maxn],p[maxn],sz;int temp[maxn];int n,m,sum;int flag,ed;bool con;void edge_init   () {erear=0;   memset (head,-1,sizeof (Head));   memset (In,0,sizeof (in));   sum=0; con=0;}   void Edge_add (int u,int v) {e[erear].v=v;   E[erear].nxt=head[u]; head[u]=erear++;}        int Topsort () {sz=0;        queue<int>q; for (int i=0;i<n;i++) {if (!        In[i]) Q.push (i);        } bool sign=0; while (!     Q.empty ()) {if (Q.size () >=2) sign=1;       int U=q.front ();            Q.pop ();            P[sz++]=u;                for (int i=head[u];~i;i=e[i].nxt) {int v=e[i].v;                in[v]--; if (!            IN[V]) Q.push (v);            }} if (Sz<n) {//printf ("invalid\n");        return 1;            }else if (sign) {//printf ("multi ans\n");        return 2;        }else{return 3;            }/*for (int i=0;i<sz;i++) {if (i==0) printf ("%d", p[i]);        else printf ("%d", p[i]); } printf ("\ n"); */}int main () {//fin while (~scanf ("%d%d", &n,&m)) {if (n==0&&m==0) b        Reak;        Edge_init ();        flag=2;        Char op[10];            for (int i=1;i<=m;i++) {scanf ("%s", op);            if (con) continue;            int u= (int) op[0]-' A ';            int v= (int) op[2]-' A ';                if (op[1]== ' > ') {edge_add (u,v);            in[v]++;      } else{          Edge_add (V,u);            in[u]++;            } memcpy (Temp,in,sizeof (in));            Flag=topsort ();            memcpy (in,temp,sizeof (temp));            Sum=i;                if (flag!=2) {ed=i;            Con=true;                }} if (flag==3) {printf ("Sorted sequence determined after%d relations:", ed);                   for (int i=sz-1; i>=0;-I.) printf ("%c", p[i]+ ' A ');            printf (". \ n");            } else if (flag==1) {printf ("inconsistency found after%d relations.\n", ed);            } else{printf ("Sorted sequence cannot be determined.\n"); }} return 0;}

  



POJ 1094 sorting It all out topology sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.