码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1094 / zoj 1060 Sorting It All Out

时间:2014-07-19 15:03:58      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   strong   

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 26876   Accepted: 9271

Description

An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D implies that A < B, B < C and C < D. in this problem, we will give you a set of relations of the form A < B and ask you to determine whether a sorted order has been specified or not.

Input

Input consists of multiple problem instances. Each instance starts with a line containing two positive integers n and m. the first value indicated the number of objects to sort, where 2 <= n <= 26. The objects to be sorted will be the first n characters of the uppercase alphabet. The second value m indicates the number of relations of the form A < B which will be given in this problem instance. Next will be m lines, each containing one such relation consisting of three characters: an uppercase letter, the character "<" and a second uppercase letter. No letter will 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, output consists of one line. This line should be 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 is determined or an inconsistency is found, whichever comes first, 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.

Source

[Submit]   [Go Back]   [Status]   [Discuss]

 

解题思路:这题是一个拓扑排序问题,不过过程比较复杂,要处理的情况也比较多!首先题目条件和要求要很清楚,这样分析问题思路就会清晰!

解题代码:

 

bubuko.com,布布扣
  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <iostream>
  4 using namespace std;
  5 struct Node{            //与之相连的节点结构体 
  6     int to;
  7     struct Node *next;
  8 };
  9 Node *Link[27]; //链表保存连接关系 
 10 Node *tm_node;
 11 bool vis[27];
 12 int count[27], tm_count[27]; //保存节点入度数据,前者为备份数据,后者为运算数据 
 13 int n, m, num;
 14 char deal[3];
 15 const int A = A;
 16 int result[27], pos; //保存结果 
 17 
 18 int TopSort(){
 19     int i, j, cnt;
 20     bool flag = false;
 21     pos = cnt = 0;// cnt为入度为0的节点数,pos为保存结果的下标 
 22     j = -1; //入度为0的字母 
 23     for (i = 0; i < n; i ++) //寻找入度为0的位置 
 24         if(tm_count[i] == 0 && vis[i]){
 25             cnt ++;
 26             j = i;
 27         }
 28     while (~j){
 29         if(cnt > 1) //如果cnt > 1 则表示有多个入度为0的节点,也就是说可能无法排序 
 30             flag = true; //不直接return是因为还有可能图中有环,导致数据矛盾 
 31         for (tm_node = Link[j]; tm_node != NULL; tm_node = tm_node ->next)
 32             tm_count[tm_node ->to] --;
 33         result[pos ++] = j;
 34         tm_count[j] --;
 35         j = -1;
 36         cnt = 0;
 37         for (i = 0; i < n; i ++)
 38             if(tm_count[i] == 0 && vis[i]){
 39                 cnt ++;
 40                 j = i;
 41             }
 42     }
 43     if(!flag && pos != num) // 图中有环,数据矛盾 
 44         return -1;
 45     if(!flag && pos == num) //图中为一个有序序列,是否是最终结果还需进一步判断 
 46         return 0;
 47     if(flag && pos == num)  //图中有多个有序序列,还需进一步判断 
 48         return 1;
 49     if(flag && pos != num)  //图中有环,数据矛盾 
 50         return -1;
 51 }
 52 
 53 void init(){
 54     num = 0;
 55     memset(vis, 0, sizeof(vis));
 56     memset(Link, 0, sizeof(Link));
 57     memset(count, 0, sizeof(count));
 58     memset(tm_count, 0, sizeof(tm_count));
 59 }
 60 
 61 void input(){
 62     int d1, d2;
 63     scanf("%s", deal);
 64     d1 = deal[0] - A;
 65     d2 = deal[2] - A;
 66     tm_node = new Node;
 67     tm_node ->next = NULL;
 68     tm_node ->to = d2;
 69     count[d2] ++;
 70     if(Link[d1] == NULL)
 71         Link[d1] = tm_node;
 72     else{
 73         tm_node ->next = Link[d1];
 74         Link[d1] = tm_node;
 75     }
 76     if(vis[d1] == 0){
 77         num ++;
 78         vis[d1] = true;
 79     }
 80     if(vis[d2] == 0){
 81         num ++;
 82         vis[d2] = true;
 83     }
 84 }
 85 
 86 int main(){
 87     int i, j;
 88     int value;
 89     bool had_ans;
 90     while(~scanf("%d%d", &n, &m) && (n && m)){
 91         init(); //初始化 
 92         had_ans = false; //是否已经得出结果 
 93         for(i = 1; i <= m; i ++){
 94             if(had_ans){  //如果已有结果,则无须处理后续数据 
 95                 scanf("%s", deal);
 96                 continue;
 97             }
 98             input(); //数据处理 
 99             for(j = 0; j < n; j ++)
100                 tm_count[j] = count[j]; 
101             value = TopSort(); //拓扑排序 
102             if (value == -1){ 
103                 printf ("Inconsistency found after %d relations.\n", i);
104                 had_ans = true;
105             }
106             else if(value == 0 && num == n){ //数据量以满足要求,且能排序 
107                 printf ("Sorted sequence determined after %d relations: ", i);
108                     for (j = 0; j < n; j ++)
109                         printf ("%c", result[j] + A);
110                 printf(".\n");
111                 had_ans = true;
112             }
113         }
114         if (value == 1 || (value == 0 && n != num)) // 无法排序
115             printf("Sorted sequence cannot be determined.\n");
116     }
117     return 0;
118 }
View Code

 

 

 

poj 1094 / zoj 1060 Sorting It All Out,布布扣,bubuko.com

poj 1094 / zoj 1060 Sorting It All Out

标签:des   style   blog   http   color   strong   

原文地址:http://www.cnblogs.com/shengshouzhaixing/p/3854175.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!