Sorting It All Out

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
 
描述
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 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.
输出
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.

样例输入
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
样例输出
Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.
/**
拓扑排序实现步骤:
①、在有向图中找到没有前驱节点的顶点并输出
②、删除该点和该点所到的下一个点之间的线
③、重复①、②操作
**/
 /**
分析:
Ⅰ、数据 n 表示有 n 个字母, 且字母由A开始到 char('A' + n - 1)
Ⅱ、 数据 m 表示有 m 个输入, 用来判断是否能够确定前 n 个字符的关系
Ⅲ、如果确定其前 n 个字母的先后顺序就输出,同时对后面的数据不做判断
Ⅳ、如果能够确定有环的存在也可以不用对后面的数据进行判断 (输出 Inconsistency...)
**/

核心代码:

 int topo_sort () {
int flag = , temp [], c = , Q[], in_num, pos;
for (int i = ; i <= n; ++ i) {
temp [i] = my_in [i];
}
for (int i = ; i <= n; ++ i) {
in_num = ;
for (int j = ; j <= n; ++ j) {
if (!temp [j]) {
pos = j;
in_num ++;
}
}
if (!in_num) return ; // 环
if (in_num > ) flag = -; // 不可能有序,但有可能有环,所以不能return Q [c ++] = pos;
temp [pos] = -;
for (int j = ; j <= n; ++ j) {
if (my_map [pos][j] == ) {
temp [j] --;
}
}
}
return flag;
}

C/C++代码实现(AC):

 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring> using namespace std; int n, m, my_map [][], my_in [], Q[], c; int topo_sort () {
int flag = , temp [], num_in, pos;
c = ;
for (int i = ; i <= n; ++ i) {
temp [i] = my_in [i];
} for (int i = ; i <= n; ++ i) {
num_in = ;
for (int j = ; j <= n; ++ j) {
if (!temp [j]) {
++ num_in;
pos = j;
}
}
if (!num_in) return ; // 环
if (num_in > ) flag = -; // 不能确定 temp [pos] = -;
Q [c ++] = pos;
for (int j = ; j <= n; ++ j) {
if (my_map [pos][j] == ) {
temp [j] --;
}
}
}
return flag;
} int main () {
while (scanf ("%d%d", &n, &m), n != || m != ) {
int flag = , a1, b1;
char a, b, d;
memset (my_map, , sizeof (my_map));
memset (my_in, , sizeof (my_in)); for (int i = ; i <= m; ++ i) {
getchar ();
scanf ("%c%c%c", &a, &d, &b);
if (!flag) continue; a1 = int (a - 'A' + );
b1 = int (b - 'A' + );
my_map [a1][b1] = ;
my_in [b1] ++; int x = topo_sort ();
if (x == ) {
printf("Sorted sequence determined after %d relations: ",i);
for (int i = ; i < c; ++ i) {
printf ("%c", 'A' + Q [i] - );
}
printf (".\n");
flag = ;
} else if (x == ) {
printf("Inconsistency found after %d relations.\n",i);
flag = ;
}
} if (flag) {
printf("Sorted sequence cannot be determined.\n");
}
}
}

nyoj 349 (poj 1094) (拓扑排序)的更多相关文章

  1. nyoj 349&Poj 1094 Sorting It All Out——————【拓扑应用】

    Sorting It All Out 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 An ascending sorted sequence of distinct ...

  2. Sorting It All Out POJ - 1094 拓扑排序

    题意:给N个字母,和M个偏序关系 求一个可确定的全序,可确定是指没有其他的可能例如A>B D>B 那么有ADB DAB两种,这就是不可确定的其中,M个偏序关系可以看做是一个一个按时间给出的 ...

  3. POJ 1094 拓扑排序

    Description:      规定对于一个只有大写字母的字符串是有大小顺序的.如ABCD.即A<B.B<C.C<D.那么问题来了.现在第一行给你n, m代表序列里只会出现前n的 ...

  4. poj 3687(拓扑排序)

    http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...

  5. POJ 3249 拓扑排序+DP

    貌似是道水题.TLE了几次.把所有的输入输出改成scanf 和 printf ,有吧队列改成了数组模拟.然后就AC 了.2333333.... Description: MR.DOG 在找工作的过程中 ...

  6. poj 3249 拓扑排序 and 动态规划

    思路:我们首先来一遍拓扑排序,将点按先后顺序排列于一维数组中,然后扫描一遍数组,将每个点的出边所连接的点进行更新,即可得到最优解. #include<iostream> #include& ...

  7. poj 2585 拓扑排序

    这题主要在于建图.对9个2*2的小块,第i块如果出现了不等于i的数字,那么一定是在i之后被brought的.可以从i到该数字建一条边. 图建好后,进行一次拓扑排序,判段是否存在环.若存在环,那么就是B ...

  8. Poj(3687),拓扑排序,

    题目链接:http://poj.org/problem?id=3687 题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号 ...

  9. POJ 1128 拓扑排序 + 深搜

    /* (⊙v⊙)嗯 貌似是一个建图 拓扑+深搜的过程.至于为什么要深搜嘛..一个月前敲得题现在全部推了重敲,于是明白了.因为题意要求如果有多个可能的解的话. * 就要输出字典序最小的那个.所以可以对2 ...

随机推荐

  1. php函数分为哪两种?

    PHP的真正威力源自于它的函数.函数分为内置函数和自定义函数. 内置函数 所谓PHP内置函数,就是在php程序的库里面已经定义了的函数,比如echo,mysql_connect,include_onc ...

  2. Python 命令行之旅:使用 docopt 实现 git 命令

    作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...

  3. 有些需要禁用的PHP危险函数(disable_functions)

    phpinfo() 功能描述:输出 PHP 环境信息以及相关的模块.WEB 环境等信息. 危险等级:中 passthru() 功能描述:允许执行一个外部程序并回显输出,类似于 exec(). 危险等级 ...

  4. Spring Cloud Feign初接触

    最近想使用下Feign,然后简单了解了一下,简单的搭了个demo. 首先简单介绍一下Feign,它是一个Http请求客户端,类似HttpClient,具体里面实现还没去看,知道它是一个请求客户端就行, ...

  5. dig-基本使用

    dig:Domain Information Groper,是一个DNS查询工具 1:使用google的域名服务器:查询特定域名的A记录 [root@localhost ~]# dig @8.8.8. ...

  6. WinDbg命令系统

    WinDbg命令系统 WinDbug三种命令 WinDbug是一个强大的调试器,大部分很多功能都是通过命令来实现的,命令在命令窗口中输入,主要分为以下三类: 标准命令 标准命令提供了调试器的基本功能, ...

  7. 题解【洛谷】CF134A

    题解 CF134A [Average Numbers] 这题就是简单的模拟. 只不过要优化一下常数什么的 思路: 为了不浪费时间总是取平均数,直接用一个 S 储存总和,每次都减去 a_i​ 再除以 n ...

  8. [Pandas]利用Pandas处理excel数据

    Python 处理excel的第三包有很多,比如XlsxWriter.xlrd&xlwt.OpenPyXL.Microsoft Excel API等,最后综合考虑选用了Pandas. Pand ...

  9. Ubuntu18.04 安装在VMware 14中无法全屏问题解决

    现象:在安装完Ubuntu18.04后发现在虚拟机中不能全屏,安装Vmware Tools后还是无法解决,修改分辨率亦不成功. 原因:WAYLAND限制 解决方法:取消ubuntu中的显示设备WAYL ...

  10. Data Deduplication Workflow Part 1

    Data deduplication provides a new approach to store data and eliminate duplicate data in chunk level ...