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

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

思路:就是利用拓扑排序,当条件不断增加时,依次判断,看是否能排好,同时要注意是否有环,注意在条件不足不能判断出先后顺序的情况下,依然要判断是否有环;
其实有一点有疑问,就是在前面条件充足,可以排好序之后即没有对后续的进行判断了,但是如果后面的条件使得前面的成环呢?貌似样例没有这个,可以直接ac
#include <iostream>
#include <cstring> using namespace std;
int in[30]; //入度
int out[30]; //输出序列
int map[30][30]; //有向图 int TopoSort(int n){
int ct = 0, cpin[30];
int flag = 1;
for(int i = 0; i < n; i++){
cpin[i] = in[i];
}
while(ct < n){
int p = -1, m = 0;
for(int i = 0; i < n; i++){
if(cpin[i] == 0){
p = i;
m++;
}
}
if(m == 0){
return -1; //有环
}
if(m > 1){
// return 0; //无法判断
flag = 0; //得出不能判断只有还有检测,因为可能后面可能有环
}
out[ct++] = p;
cpin[p] = -1;
for(int i = 0; i < n; i++){
if(map[p][i]){
cpin[i]--;
}
}
}
return flag; //排好序了
} int main(){
std::ios::sync_with_stdio(false);
int n, m;
char x, y;
char ch;
while(cin >> n >> m && (n != 0 && m != 0)){
memset(in, 0, sizeof(in));
memset(map, 0, sizeof(map));
int flag = 1;
for(int i = 0; i < m; i++){
cin >> x >> ch >> y;
if(flag == 0)
continue; //注意就算前面已经判断出结果,也要输入完,但不需要判断了
if(map[x - 'A'][y - 'A'] == 0){
map[x - 'A'][y - 'A'] = 1;
in[y - 'A']++;
int s = TopoSort(n);
if(s == -1){
cout << "Inconsistency found after ";
cout << i + 1;
cout << " relations." << endl;
flag = 0;
}
if(s == 1){
cout << "Sorted sequence determined after ";
cout << i + 1;
cout << " relations: ";
for(int i = 0; i < n; i++){
cout << char(out[i] + 'A');
}
cout << "." << endl;
flag = 0;
}
}
}
if(flag){
cout << "Sorted sequence cannot be determined." << endl;
}
}
return 0;
}

  

27-拓扑排序-poj1094的更多相关文章

  1. [poj1094]Sorting It All Out_拓扑排序

    Sorting It All Out poj-1094 题目大意:给出一些字符串之间的大小关系,问能否得到一个唯一的字符串序列,满足权值随下标递增. 注释:最多26个字母,均为大写. 想法:显然,很容 ...

  2. nyoj349 poj1094 Sorting It All Out(拓扑排序)

    nyoj349   http://acm.nyist.net/JudgeOnline/problem.php?pid=349poj1094   http://poj.org/problem?id=10 ...

  3. POJ1094 拓扑排序

    问题:POJ1094   本题考查拓扑排序算法   拓扑排序:   1)找到入度为0的点,加入已排序列表末尾: 2)删除该点,更新入度数组.   循环1)2)直到 1. 所有点都被删除,则找到一个拓扑 ...

  4. POJ1094——拓扑排序和它的唯一性

    比较模板的topological-sort题,关键在于每个元素都严格存在唯一的大小关系,而一般的拓扑排序只给出一个可能解,这就需要每趟排序的过程中监视它是不是总坚持一条唯一的路径. 算法导论里面的拓扑 ...

  5. POJ1094 Sorting It All Out —— 拓扑排序

    题目链接:http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Tot ...

  6. poj1094拓扑排序

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29539   Accepted: 10 ...

  7. POJ1094 字母排序(拓扑排序)

    该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列.是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序列有序,并依次输出: 2.该序列不能判断是否有序: 3.该序列字母次 ...

  8. [NOIP2015模拟10.27] 挑竹签 解题报告(拓扑排序)

    Description 挑竹签——小时候的游戏夏夜,早苗和诹访子在月光下玩起了挑竹签这一经典的游戏.挑竹签,就是在桌上摆上一把竹签,每次从最上层挑走一根竹签.如果动了其他的竹签,就要换对手来挑.在所有 ...

  9. ACM/ICPC 之 拓扑排序范例(POJ1094-POJ2585)

    两道拓扑排序问题的范例,用拓扑排序解决的实质是一个单向关系问题 POJ1094(ZOJ1060)-Sortng It All Out 题意简单,但需要考虑的地方很多,因此很容易将code写繁琐了,会给 ...

  10. UVA196-Spreadsheet(拓扑排序)

    Spreadsheet In 1979, Dan Bricklin and Bob Frankston wrote VisiCalc, the first spreadsheet applicatio ...

随机推荐

  1. XE7/10诡异报错brcc32错误

    重新编译工程时,报错:     之前没遇到过,解决方法: 重新设置下Application Icon,再build,问题解决.

  2. zend studio 提升开发效率的快捷键及可视化订制相关设置

    Zend studio快捷键使用 F3 快速跳转到当前所指的函数,常量,方法,类的定义处,相当常用.当然还可以用Ctrl+鼠标左键 shift+end 此行第一个到最后一个 shift+home 此行 ...

  3. bzoj 1500 维修序列

    Written with StackEdit. Description 请写一个程序,要求维护一个数列,支持以下 \(6\) 种操作: 请注意,格式栏 中的下划线' _ '表示实际输入文件中的空格 I ...

  4. [Luogu1527][国家集训队]矩阵乘法

    luogu 题意 给你一个\(N*N\)的矩阵,每次询问一个子矩形的第K小数.(居然连修改都不带的) \(N\le500,Q\le60000\) sol 整体二分+二维树状数组裸题. 复杂度是\(O( ...

  5. excel oracle字段命名(大写下划线分词)转 驼峰命名

    干货: (帕斯卡) =LEFT(C251,1)&MID(SUBSTITUTE(PROPER(C251),"_",""),2,100) (驼峰) =LOW ...

  6. Redis 字符串(String)

    Redis 字符串(String) Redis 字符串数据类型的相关命令用于管理 redis 字符串值,基本语法如下: 语法 redis 127.0.0.1:6379> COMMAND KEY_ ...

  7. log框架集成

    使用slf4j,slf4j相当于一个接口,我们面对接口编程,方便地集成其他的日志框架,我们按照slf4j的标准,日志就会相应地打入日志系统中(log4j 使用slf4j要有两个包1,他本身的api,2 ...

  8. Unity GUI自适应屏幕分辨率(一)布局自适应

    这里我们先谈第一个问题坐标矩阵变化实现布局自适应. 选取基准尺寸 通常你需要选择一个基准的屏幕尺寸,象现在开发的应用也需要跨平台在iOS(iPhone/iPad)/Android都可以运行,我这边选取 ...

  9. 通过html字符串连接组合并调用javascript函数

    ----通过字符串连接并调用javascript函数-- var t_html = $("#Photo").html(); var n_html = "<a id= ...

  10. java从键盘输入若干数,求其最大值,最小值,平均值。等等

    总结:有一定基础的人,应该发现第一个程序可以运行,其实它有个致命的错误.有谁能一眼看出来呢?第二个程序是对的. 这个题目求最大值,最小值,平均值我不会求,不知道这个if判断放在类的外面还是main函数 ...