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. HihoCoder 1097 Prim算法

    1097 : 最小生成树一·Prim算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 最近,小Hi很喜欢玩的一款游戏模拟城市开放出了新Mod,在这个Mod中,玩家可以 ...

  2. Fillder手机抓包的使用

    1.Fillder下载地址: http://www.onlinedown.net/soft/73207.htm 2.网络设置 手机和电脑需链接网络相同 3.fillder设置 3.1打开fillder ...

  3. 把UIImage保存到照片库和沙盒中

    1.保存到iOS照片库需要引入QuartzCore.framework框架,具体代码如下: .h文件 #import <QuartzCore/QuartzCore.h> UIImageVi ...

  4. Cocos2d-x -自己定义动作 圆周运动

    原文地址:http://blog.csdn.net/u012945598/article/details/17605409 在之前的文章中我们以前讲过Cocos2d-x中的各种动作的用法,我们先来简单 ...

  5. compoer 全局和单个项目切换源 composer update killed

    演示地址: https://blog.csdn.net/hpugym/article/details/72588393 composer update killed https://cloud.ten ...

  6. jinja2的一些用法

    1.split用法以及乘法运算 {% set user_list=l.users.split(',') %} <tr> <td>{{ l.name }}</td> ...

  7. Java-Maven-Runoob:Maven NetBeans

    ylbtech-Java-Maven-Runoob:Maven NetBeans 1.返回顶部 1. Maven NetBeans NetBeans 6.7 及更新的版本已经内置了 Maven.对于以 ...

  8. logging模块讲解

    logging模块 很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式 ...

  9. from表单

    构建一个表单 假设你想在你的网站上创建一个简单的表单,以获得用户的名字.你需要类似这样的模板: 1 2 3 4 5 <form action="/your-name/" me ...

  10. C#简单操作XML

    类文件: class OperatorXML { /// <summary> /// 确定资源文件路径,Resource为自己创建的目录 /// </summary> priv ...