Sorting It All Out
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 26911   Accepted: 9285

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

field=source&key=East+Central+North+America+2001" style="text-decoration:none">East Central North America 2001

AC代码:

#include<iostream>
#include<vector>
#include<stack>
#include<algorithm>
#include<cstring>
#include<stdio.h>
using namespace std;
int n,m;
int in[30],tmp_in[30];
int f1,f2;
int str[30];
vector <int> G[30];
void top_sort(){ //拓扑排序
f1=0; //没有环
f2=1; //唯一排序
for(int i=0;i<n;i++)
tmp_in[i]=in[i];
stack <int> s;
while(!s.empty())
s.pop();
for(int i=0;i<n;i++)
if(tmp_in[i]==0)
s.push(i);
int counter=0;
while(!s.empty()){
if(s.size()>=2){
f2=0;
}
int tmp=s.top();
s.pop();
str[counter++]=tmp;
for(int i=0;i<G[tmp].size();i++){
tmp_in[G[tmp][i]]--;
if(!tmp_in[G[tmp][i]])
s.push(G[tmp][i]);
}
}
if(counter<n)
f1=1;
}
int main(){
while(cin>>n>>m&&(n!=0||m!=0)){
memset(in,0,sizeof(in));
int i;
for(i=0;i<n;i++)
if(!G[i].empty())
G[i].clear(); for(i=0;i<m;i++){
char ch[5]; cin>>ch;
G[ch[0]-'A'].push_back(ch[2]-'A');
in[ch[2]-'A']++;
top_sort();
if(f1){
cout<<"Inconsistency found after "<<i+1<<" relations."<<endl;
for(int j=i+1;j<m;j++) //注意这里要输入剩下的信息才干退出
cin>>ch;
break;
}
else if(f2){
cout<<"Sorted sequence determined after "<<i+1<<" relations: ";
for(int j=0;j<n;j++)
cout<<char (str[j]+'A');
cout<<'.'<<endl;
for(int j=i+1;j<m;j++) //注意这里要输入剩下的信息才干退出
cin>>ch;
break;
}
}
if(i>=m)
cout<<"Sorted sequence cannot be determined."<<endl;
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

poj 1094的更多相关文章

  1. ACM: poj 1094 Sorting It All Out - 拓扑排序

    poj 1094 Sorting It All Out Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & ...

  2. POJ 1094 (传递闭包 + 拓扑排序)

    题目链接: POJ 1094 题目大意:有 1 ~ N 个大写字母,且从 A 开始依次 N 个.再给你 M 个小于的关系,比如 A < B ,让你判断三种可能: 1.在第 i 个关系罗列之后,是 ...

  3. poj 1094(拓扑排序)

    http://poj.org/problem?id=1094 题意:给你m个字母,有n个判断语句.求在哪个语句就可以判断出这个是不是一个环,或者在哪个语句可以判断出这些字母的排序规则,或者就是不能确定 ...

  4. POJ 1094 Sorting It All Out 拓扑排序 难度:0

    http://poj.org/problem?id=1094 #include <cstdio> #include <cstring> #include <vector& ...

  5. POJ 1094 (TopoSort)

    http://poj.org/problem?id=1094 题意:该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列.是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序 ...

  6. poj 1094 Sorting It All Out (拓扑排序)

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

  7. poj 1094 Sorting It All Out(图论)

    http://poj.org/problem?id=1094 这一题,看了个大牛的解题报告,思路变得非常的清晰: 1,先利用floyd_warshall算法求出图的传递闭包 2,再判断是不是存在唯一的 ...

  8. poj 1094 Sorting It All Out 解题报告

    题目链接:http://poj.org/problem?id=1094 题目意思:给出 n 个待排序的字母 和 m 种关系,问需要读到第 几 行可以确定这些字母的排列顺序或者有矛盾的地方,又或者虽然具 ...

  9. POJ 1094 Sorting It All Out【拓扑排序】

    题目链接: http://poj.org/problem?id=1094 题意: 给定前n个字母的大小关系,问你是否 根据前xxx个关系得到上升序列 所有关系都无法确定唯一的一个序列 第xxx个关系导 ...

  10. 拓扑排序 +Floyd(poj 1094)

    题目:Sorting It All Out 题意:字母表前n个字母,有m组他们中的大小关系,判断n个字母是否构成唯一序列: 1.Sorted sequence determined after xxx ...

随机推荐

  1. NHibernate - HQL - 添加和更改

    添加: /// <summary> /// 等待乙方做出回应 A /// </summary> private void button2_Click_1(object send ...

  2. Notepad++中如何设置自动换行以及行宽

    view-->word wrap; setting->preference-->vertical edge settings; Notepad++中如何设置自动换行以及行宽 http ...

  3. API通用设计原则

    什么是好的API? ·        完备(Be Complete) 对确定重点支持的用户场景具有完备的功能支持.就是说,用户通过对一组API的调用能够完成预期的功能. ·        不冗余(Be ...

  4. 基于visual Studio2013解决面试题之1409基数排序

     题目

  5. 参考storm中的RotatingMap实现key超时处理

    storm0.8.1以后的RotatingMap完全可以独立于storm用来实现hashmap的key超时删除,并调用回调函数 RotatingMap.java: import java.util.H ...

  6. 禁止页面复制功能 js禁止复制 禁用页面右键菜单

    <body oncontextmenu="return false">禁用网页右键菜单,但是仍然可以使用快捷键复制. js代码禁用复制功能: <script  t ...

  7. Java中int类型和tyte[]之间转换及byte[]合并

    JAVA基于位移的 int类型和tyte[]之间转换 [java] view plaincopy /** * 基于位移的int转化成byte[] * @param int number * @retu ...

  8. 改动导航栏上返回button上的字,比如把back改动为返回

    改动导航栏上返回button上的字,比如把back改动为返回 注意:这个须要在跳转之前到视图控制器中写,而不是在跳转之后到控制器中写 UIBarButtonItem *backIetm = [[UIB ...

  9. qt之正则表达式

    原地址:http://blog.csdn.net/phay/article/details/7304455 QRegExp是Qt的正则表达式类.Qt中有两个不同类的正则表达式.第一类为元字符.它表示一 ...

  10. 九度OJ 1065 输出梯形 (模拟)

    题目1065:输出梯形 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3745 解决:2043 题目描写叙述: 输入一个高度h.输出一个高为h.上底边为h的梯形. 输入: 一个整数h(1& ...