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. 感觉有变良好的第一次电面——yahoo北京测试实习生

    一个月之前投的岗位.都已经忘了.昨天突然接到电话说今天下午3点电面. 立马又开始忐忑起来,整理了下项目啊,推荐系统相关知识啥的,跑到欧巴桑寝室去电面电面. 3点很准时的电话来了,是个女面试官. 一上来 ...

  2. Linux部分常用命令学习(二)

    1.wc:wc命令是一个统计的工具,主要用来显示文件所包含的行.字和字节数.wc命令是word count的缩写. 命令格式:wc [选项参数] [文件] 选项参数: -c 统计字节数 -l 统计行数 ...

  3. HihoCoder 1183 : 连通性一·割边与割点(模板)

    连通性一·割边与割点 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 还记得上次小Hi和小Ho学校被黑客攻击的事情么,那一次攻击最后造成了学校网络数据的丢失.为了避免再次 ...

  4. bzoj 1220 跳蚤

    Written with StackEdit. Description \(Z\)城市居住着很多只跳蚤.在\(Z\)城市周六生活频道有一个娱乐节目.一只跳蚤将被请上一个高空钢丝的正中央.钢丝很长,可以 ...

  5. HDFS的工作原理扫扫盲

    问题导读: 1.什么是分布式文件系统? 2.怎样分离元数据和数据? 3.HDFS的原理是什么? Hadoop分布式文件系统(HDFS)是一种被设计成适合运行在通用硬件上的分布式文件系统.HDFS是一个 ...

  6. C#进阶之路(七)反射的应用

    反射在C#中的应用还是很多的,但它对代码的性能有一定影响. 反射的性能: 使用反射来调用类型或者触发方法,或者访问一个字段或者属性时clr 需要做更多的工作:校验参数,检查权限等等,所以速度是非常慢的 ...

  7. VS下QT的自定义槽函数修改方法

    通过几天的摸索,基本发现了两个VS的槽函数的修改方法 一种是UI是通过UI 设计师拖出来的,类似VB的方法,通过拖的方法,按钮的代码是系统自动生成的,在UI.h的头文件下,这个时候,实现槽函数有个固定 ...

  8. gitlab的本地搭建和部署使用

    公司现在的代码管理是在公司的服务器上部署了gitlab,这样既方便协同开发,有可以很好的保护代码的安全性. 那么我们就来研究一下 如何给自己的服务器上部署gitlab吧! 学习源头:https://w ...

  9. Linux下查询一个包是32位还是64位

    Linux下查询一个包是32位还是64位 [root@localhost ~]# rpm -qa  --queryformat %-{name}-%{version}-%{release}-%{arc ...

  10. PS相关技术

    PS相关长时间不用就忘记了,做个笔记,记录下来 (1)复制图层,可以将图层复制到另外的图层里去,这样,多个图层就可以编辑了 (2)通过建立选区,可以选择右键,通过剪切的图层,通过复制的图层将图片抠出来 ...