Nyoj 349&poj 1094 sorting It all out

Source: Internet
Author: User

Sorting It all out time limit:MS | Memory limit:65535 KB Difficulty:3
Describe
An
ascending sorted sequence of distinct values are one in which some form of a less-than operator are used to order The elements 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. 



Topic: give you n points, give you M-bar to represent the size of the relationship. Ask you if you have a conflict (with a loop) after you join on the first few sides or you can determine the relationship, or you cannot determine the relationship.
Problem-solving ideas: The first time to join a side, the use of Floyd transmission closure, and then determine whether to form a ring. If there is no ring to determine whether the unique size relationship can be determined, here is an important criterion for determining if all nodes are equal to n-1, then the topological sort records the path.

#include <bits/stdc++.h>using namespace Std;int Map[50][50],indegree[50],outdegree[50];char S_ord[50];bool                Floyd (int n) {for (int k=0;k<n;k++) {//transitive closure for (int. i=0;i<n;i++) {for (int j=0;j<n;j++) {            if (Map[i][k]&&map[k][j]) map[i][j]=1;    }}} for (int i=0;i<n;i++)//Determine if ring if (Map[i][i]) return 1; return 0;}    BOOL Calcu_is_ord (int n) {//calculation is currently ordered memset (indegree,0,sizeof (Indegree));    memset (outdegree,0,sizeof (Outdegree));                for (int i=0;i<n;i++) {for (int j=0;j<n;j++) {if (Map[i][j]) {indegree[j]++;            outdegree[i]++; }}} for (int i=0;i<n;i++) {if (indegree[i]+outdegree[i]!=n-1) {/*) if all nodes satisfy the in-degree plus-out degree equals the total number of nodes minus one, the instructions are ordered. Because if ordered, will inevitably have the degree of 0~n-1, corresponding to the degree of n-1~0. So as long as all the nodes are n-1, it means that they are in order.        */return 0; }} return 1;} void Topo_sort (int n) {//topological sort order int Que_[50],vis[50],top=0,cnt=0,u;        for (int i=0;i<n;i++) {if (indegree[i]==0) {que_[++top]=i;    }} memset (Vis,0,sizeof (VIS));        while (top) {u=que_[top--];        Vis[u]=1;        s_ord[cnt++]=u+ ' A ';            for (int i=0;i<n;i++) {if (!vis[i]&&map[u][i]) {indegree[i]--;            } if (!vis[i]&&indegree[i]==0) {que_[++top]=i; }}} s_ord[cnt++]= ' ";}    int main () {int n,m;    Char str[10];        while (scanf ("%d%d", &n,&m)!=eof&& (n+m)) {memset (map,0,sizeof (MAP));  int flag_cir=0,flag_ord=0;            Record the formation of a ring or an ordered for (int i=1;i<=m;i++) {scanf ("%s", str) on the first set of relationship inputs;            map[str[0]-' A '][str[2]-' a ']=1; if (flag_cir| |            Flag_ord) continue;            if (Floyd (n)) {flag_cir=i;continue;}        else if (Calcu_is_ord (n)) {topo_sort (n); flag_ord=i;continue;} } if (FLAG_CIR) printf ("inconsistency foundAfter%d relations.\n ", FLAG_CIR);        else if (Flag_ord) {printf ("Sorted sequence determined after%d relations:%s.\n", Flag_ord,s_ord);        }else{printf ("Sorted sequence cannot be determined.\n"); }} return 0;}

  

Nyoj 349&poj 1094 sorting It all out

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.