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. iOS开发请您把握现在 — 面向未来学习

    iOS开发请您把握现在 — 面向未来学习 这一篇文章,如果你是一名iOS开发正好也处于开发晋升瓶颈迷茫期,不妨停下你的脚步,花五分钟看看,兴许有你需要的!文章结尾有彩蛋 群里常见的唱哀 iOS现在到底 ...

  2. MOOC python笔记(三) 序列容器:字符串、列表、元组

    容器概念 容器是Python中的重要概念,分为有序与无序. 有序容器也称为序列类型容器,如:字符串.列表. 通用序列容器操作 容器连接+ 加号可以把两个序列连接成一个更大的容器,相加后两个序列的值并不 ...

  3. MFC::Visual studio ? 对应VC6-14

    VC6VC7: Visual studio.netVC7.1: Visual studio 2003VC8: Visual studio 2005VC9: Visual studio 2008VC10 ...

  4. spring cloud alibaba 简介

    ### Spring Cloud Alibaba [官方github地址](https://github.com/alibaba/spring-cloud-alibaba) Spring Cloud ...

  5. Rsync服务常见问题

    转---Rsync 故障排查整理 Rsync服务常见问题汇总讲解: 1. 客户端的错误现象:No route to host rsync服务端开启的iptables**防火墙** [root@nfs0 ...

  6. Open vSwitch系列之一 Open vSwitch诞生

    Open vSwitch系列之一 Open vSwitch诞生 Open vSwitch系列之二 安装指定版本ovs 2006年,SDN诞生于美国GENI项目资助的斯坦福大学Clean Slate课题 ...

  7. 基础安全术语科普(五)——crypter

    crypter(加壳):使病毒逃过反病毒软件检测的技术 UD类加壳病毒:只有%50至%25的软件能检测出是病毒. FUD类加壳病毒:完全不会被检测出来. 加壳技术有两个主要组件: 1.client — ...

  8. 使用 statcounter 统计 Hexo 博客访问量

    介绍 statcounter是一个提供网站访问统计服务的网站: StatCounter is a simple but powerful real-time web analytics service ...

  9. SpringBoot与MybatisPlus3.X整合之动态表名 SQL 解析器(七)

    pom.xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId& ...

  10. WARNING: The host 'WeiLei' could not be looked up with resolveip.

    [root@WeiLei data]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysq ...