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. PHP - 接口&抽象类

    什么时候使用抽象类什么时候使用接口? .如果要创建一个模型,这个模型将由一些紧密相关的对象采用,就可以使用抽象类.如果要创建将由一些不相关对象采用的功能,就使用接口. .如果必须从多个来源继承行为,就 ...

  2. STM32 控制步进电机 28BYJ-48

    STM32 控制步进电机 28BYJ-48  http://blog.chinaunix.net/uid-12664992-id-300272.html 步进电机驱动最简化的逻辑: //四相八拍:A- ...

  3. js ajax调用请求

    <pre name="code" class="html"> function getAppList(env){ var data = {}; da ...

  4. 通过 Spring RestTemplate 调用带请求体的 Delete 方法(Delete With Request Body)

    Spring 框架的RestTemplate 类定义了一些我们在通过 java 代码调用 Rest 服务时经常需要用到的方法,使得我们通过 java 调用 rest 服务时更加方便.简单.但是 Res ...

  5. poj-3895-Cycles of Lanes 简单DFS

    题目链接: http://poj.org/problem?id=3895 题目意思: 在无向连通图中图中找一个经过边数最多的环. 解题思路: 从任意一点直接DFS,不用回溯,注意构成环的话至少有3条边 ...

  6. OCP-1Z0-042-V12.39-47题

    47.Which two database operations can be performed at the mount stage of database startup? 题目解析: A和E在 ...

  7. 细节!重点!易错点!--面试java基础篇(二)

    今天来给大家分享一下java的重点易错点第二部分,也是各位同学面试需要准备的,欢迎大家交流指正. 1.字符串创建与存储机制:当创建一个字符串时,首先会在常量池中查找是否已经有相同的字符串被定义,其判断 ...

  8. delphi中覆盖最大化消息(覆盖WM_GETMINMAXINFO消息)

    unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs; ...

  9. DM6446开发攻略——u-boot-1.3.4移植(1)

    http://zjbintsystem.blog.51cto.com/964211/282387转载   UBOOT的版本更新速度比较快,截止今天,稳定正式的版本是u-boot-2009.11-rc2 ...

  10. VHDL TestBench 测试终止时自动结束仿真——assert方法

    可在结束仿真位置添加如下代码: assert false report "Simulation is finished!" severity Failure; 则在Modelsim ...