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

  1. 4 6
  2. A<B
  3. A<C
  4. B<C
  5. C<D
  6. B<D
  7. A<B
  8. 3 2
  9. A<B
  10. B<A
  11. 26 1
  12. A<Z
  13. 0 0

Sample Output

  1. Sorted sequence determined after 4 relations: ABCD.
  2. Inconsistency found after 2 relations.
  3. Sorted sequence cannot be determined.

Source

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

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

解题代码:

  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[]; //链表保存连接关系
  10. Node *tm_node;
  11. bool vis[];
  12. int count[], tm_count[]; //保存节点入度数据,前者为备份数据,后者为运算数据
  13. int n, m, num;
  14. char deal[];
  15. const int A = 'A';
  16. int result[], pos; //保存结果
  17.  
  18. int TopSort(){
  19. int i, j, cnt;
  20. bool flag = false;
  21. pos = cnt = ;// cnt为入度为0的节点数,pos为保存结果的下标
  22. j = -; //入度为0的字母
  23. for (i = ; i < n; i ++) //寻找入度为0的位置
  24. if(tm_count[i] == && vis[i]){
  25. cnt ++;
  26. j = i;
  27. }
  28. while (~j){
  29. if(cnt > ) //如果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 = -;
  36. cnt = ;
  37. for (i = ; i < n; i ++)
  38. if(tm_count[i] == && vis[i]){
  39. cnt ++;
  40. j = i;
  41. }
  42. }
  43. if(!flag && pos != num) // 图中有环,数据矛盾
  44. return -;
  45. if(!flag && pos == num) //图中为一个有序序列,是否是最终结果还需进一步判断
  46. return ;
  47. if(flag && pos == num) //图中有多个有序序列,还需进一步判断
  48. return ;
  49. if(flag && pos != num) //图中有环,数据矛盾
  50. return -;
  51. }
  52.  
  53. void init(){
  54. num = ;
  55. memset(vis, , sizeof(vis));
  56. memset(Link, , sizeof(Link));
  57. memset(count, , sizeof(count));
  58. memset(tm_count, , sizeof(tm_count));
  59. }
  60.  
  61. void input(){
  62. int d1, d2;
  63. scanf("%s", deal);
  64. d1 = deal[] - A;
  65. d2 = deal[] - 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] == ){
  77. num ++;
  78. vis[d1] = true;
  79. }
  80. if(vis[d2] == ){
  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 = ; i <= m; i ++){
  94. if(had_ans){ //如果已有结果,则无须处理后续数据
  95. scanf("%s", deal);
  96. continue;
  97. }
  98. input(); //数据处理
  99. for(j = ; j < n; j ++)
  100. tm_count[j] = count[j];
  101. value = TopSort(); //拓扑排序
  102. if (value == -){
  103. printf ("Inconsistency found after %d relations.\n", i);
  104. had_ans = true;
  105. }
  106. else if(value == && num == n){ //数据量以满足要求,且能排序
  107. printf ("Sorted sequence determined after %d relations: ", i);
  108. for (j = ; j < n; j ++)
  109. printf ("%c", result[j] + A);
  110. printf(".\n");
  111. had_ans = true;
  112. }
  113. }
  114. if (value == || (value == && n != num)) // 无法排序
  115. printf("Sorted sequence cannot be determined.\n");
  116. }
  117. return ;
  118. }

poj 1094 / zoj 1060 Sorting It All Out的更多相关文章

  1. ACM: poj 1094 Sorting It All Out - 拓扑排序

    poj 1094 Sorting It All Out Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & ...

  2. POJ 1562 && ZOJ 1709 Oil Deposits(简单DFS)

    题目链接 题意 : 问一个m×n的矩形中,有多少个pocket,如果两块油田相连(上下左右或者对角连着也算),就算一个pocket . 思路 : 写好8个方向搜就可以了,每次找的时候可以先把那个点直接 ...

  3. POJ 3076 / ZOJ 3122 Sudoku(DLX)

    Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...

  4. poj 3100 (zoj 2818)||ZOJ 2829 ||ZOJ 1938 (poj 2249)

    水题三题: 1.给你B和N,求个整数A使得A^n最接近B 2. 输出第N个能被3或者5整除的数 3.给你整数n和k,让你求组合数c(n,k) 1.poj 3100 (zoj 2818) Root of ...

  5. POJ 1094 (传递闭包 + 拓扑排序)

    题目链接: POJ 1094 题目大意:有 1 ~ N 个大写字母,且从 A 开始依次 N 个.再给你 M 个小于的关系,比如 A < B ,让你判断三种可能: 1.在第 i 个关系罗列之后,是 ...

  6. poj 1094 Sorting It All Out (拓扑排序)

    http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  7. POJ 1094 Sorting It All Out 拓扑排序 难度:0

    http://poj.org/problem?id=1094 #include <cstdio> #include <cstring> #include <vector& ...

  8. poj 1094 Sorting It All Out(图论)

    http://poj.org/problem?id=1094 这一题,看了个大牛的解题报告,思路变得非常的清晰: 1,先利用floyd_warshall算法求出图的传递闭包 2,再判断是不是存在唯一的 ...

  9. poj 1094 Sorting It All Out 解题报告

    题目链接:http://poj.org/problem?id=1094 题目意思:给出 n 个待排序的字母 和 m 种关系,问需要读到第 几 行可以确定这些字母的排列顺序或者有矛盾的地方,又或者虽然具 ...

随机推荐

  1. Wing IDE 怎样设置 python版本号

    机器上同一时候装了Python3和Python2,使用Wing IDE, 由于Python2和3是有非常大的差别的,所以时不时的须要更改IDE使用的Python版本号.以下介绍方法: 1.打开Edit ...

  2. Android 6.0 开发人员对系统权限的使用与练习(Permissions Best Practices)

    Permissions Best Practices 在安装的过程中,用户非常easy忽略权限请求. 假设一个用户相应用感觉沮丧或者操心泄漏个人信息,那么这些用户就会不用他或者卸载它. 怎样规避这个问 ...

  3. web forms page和control的生命周期life cycle交互,以及page生命周期中每个event中需要做什么事情

    只有 page_load和page_init这些可以autoeventwireup RenderControl只提供override public override void RenderContro ...

  4. js中callback执行

    <!DOCTYPE HTML> <html> <head> <meta charset="GBK" /> <title> ...

  5. ThinkPHP5.0框架开发--第7章 TP5.0数据库操作

    ThinkPHP5.0框架开发--第7章 TP5.0数据库操作 第7章 TP5.0数据库操作 ===================================================== ...

  6. TabPage判断重复添加Page

    ..... ........ ...........代码如下: bool isPag = true; foreach (TabPage page in tbpDynamicMenu.TabPages) ...

  7. LeetCode 1. Two Sum (c++ stl map)

    题目:https://leetcode.com/problems/two-sum/description/ stl map代码: class Solution { public: vector< ...

  8. Codeforces 845C. Two TVs 思路:简单贪心算法

    题目: 题目原文链接:http://codeforces.com/contest/845/problem/C 题意:现在我们有一个电视清单,有两个电视,电视清单上有每一个节目的开始时间和结束时间. 电 ...

  9. 搭建python3环境

    将Python2.7通过控制面板卸载,然后从网站下载python的安装包,安装即可. 安装beatifulsoup4库.在命令行下, pip install beautifulsoup4 千万不能在p ...

  10. confluence6.0.3安装文档

    一.Atlassian Confluence 6.0.3安装文档包含内容 1.wiki的安装步骤: 2.旧系统迁移中碰到的无法编辑和问题和解决方案: 3.wiki源码安装包.连接mysql用的jar包 ...