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.
    题目大意:给你两个数n和m , n 表示26个大写英文子母中的前 n 个字母, m 表示以下m个形如:A < B 的表达式。按照这 m 个表达式给出的顺序,每给出一个表达式(假设序号为k ,1 <= k <= m),就以这前k个表达式为条件,判断以下三种情况:
1、前n个大写英文字母 能 按拓扑序排好 ,并且 只有一种 排列方式。注意:此时k 可能小于 n !!这时输出:Sorted sequence determined after xxx relations: yyy...y. 
2、前n个大写英文字母 能 按拓扑序排好 ,但有 不止一种 排列方式。注意:此时k 必须等于 n !!这时输出:Sorted sequence cannot be determined. 
3、如果不能完成拓扑序,注意:此时k 可能小于 n !!就输出:Sorted sequence cannot be determined.
    解题思路:每给出一个表达式,就以这个表达式以及这个表达式以前的表达式为条件,进行拓扑排序。
    请看代码:
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<vector>
#define mem(a , b) memset(a , b , sizeof(a))
using namespace std ;
const int MAXN = 100 ;
int ind[MAXN] ;
int idtmp[MAXN] ;
char ans[MAXN] ;
vector<int> G[MAXN] ;
int n , m ;
void chu()
{
mem(ind , 0) ;
mem(idtmp , 0) ;
mem(ans , 0) ;
int i ;
for(i = 0 ; i <= n ; i ++)
G[i].clear() ;
}
int topo()
{
int i ;
mem(idtmp , 0) ;
for(i = 0 ; i < n ; i ++)
{
idtmp[i] = ind[i] ;
}
int k = 0 ;
int sumd0 ;
int u , v ;
bool flag1 , flag2 , flag3 ;
flag2 = false ;
flag3 = true ;
for(k = 0 ; k < n ; k ++)
{
sumd0 = 0 ;
for(i = 0 ; i < n ; i ++)
{
if(idtmp[i] == 0)
{
sumd0 ++ ;
u = i ;
}
}
if(sumd0 > 0)
{
ans[k] = u + 'A';
idtmp[u] -- ;
for(int j = 0 ; j < G[u].size() ; j ++)
{
v = G[u][j] ;
idtmp[v] -- ;
}
if(sumd0 > 1)
{
flag2 = true ;
}
}
else
{
flag3 = false ;
break ;
}
}
if(!flag3)
{
return 3 ;
}
else
{
if(flag2)
{
return 2 ;
}
else
{
return 1 ;
}
}
}
void init()
{
chu() ;
int i ;
char a , op , b ;
bool f = false ;
for(i = 0 ; i < m ; i ++)
{
cin >> a >> op >> b ;
if(f)
continue ;
int ta , tb ;
ta = a - 'A' ;
tb = b - 'A' ;
G[ta].push_back(tb) ;
ind[tb] ++ ;
int pan ;
pan = topo() ;
if(pan == 3)
{
f = true ;
printf("Inconsistency found after %d relations.\n" , i + 1) ;
}
else if(pan == 1)
{
ans[n] = '\0' ;
f = true ;
printf("Sorted sequence determined after %d relations: %s.\n" , i + 1 , ans) ;
}
else if(pan == 2 && i == m - 1)
{
puts("Sorted sequence cannot be determined.") ;
}
}
}
int main()
{
while (scanf("%d%d" , &n , &m) != EOF)
{
if(n == 0 && m == 0)
break ;
init() ;
}
return 0 ;
}
												

POJ 1094 Sorting It All Out (拓扑排序) - from lanshui_Yang的更多相关文章

  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 Sorting It All Out (拓扑排序)

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

  3. [ACM_模拟] POJ 1094 Sorting It All Out (拓扑排序+Floyd算法 判断关系是否矛盾或统一)

    Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...

  4. poj 1094 Sorting It All Out_拓扑排序

    题意:是否唯一确定顺序,根据情况输出 #include <iostream> #include<cstdio> #include<cstring> #include ...

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

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

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

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

  7. POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)

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

  8. [ACM] POJ 1094 Sorting It All Out (拓扑排序)

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

  9. POJ 1094:Sorting It All Out拓扑排序之我在这里挖了一个大大的坑

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

随机推荐

  1. Find the Duplicate Number 解答

    Question Given an array nums containing n + 1 integers where each integer is between 1 and n (inclus ...

  2. 关于qt学习的一点小记录(1)

    今日为了应付学校作业要求 决定现学qt来制作界面 毕竟c++不像在这方面c#可以那么方便 qt主要依靠信号.槽来实现类似winform中的消息 鉴于要尽快做完,故而没有细看qt 只是大概了解了下界面的 ...

  3. 虚拟机环境中安装ubuntu下的mysql-cluster7.3.2(单点服务器)

      部署环境: 系统:ubuntu-12.04.2 LTS -server-i386.iso Cluster:mysql-cluster-gpl-7.3.2-linux-glibc23-i686.ta ...

  4. 用Less循环生成样式

    需求是这样的,我要给一个轮播图设置不同的背景图,由于每张图片的背景图路劲都不一样,所以需要对每个单独的元素自定义图片路径.然后想到Less语法有mixin机制,就这样实现了一个递归function,然 ...

  5. What is NetApp's Cluster File System?

    Data ONTAP GX: A Scalable Storage Cluster www.usenix.org/event/fast07/tech/full_papers/eisler/eisler ...

  6. BZOJ 2648/2716(SJY把件-KD_Tree)[Template:KD_Tree]

    2648: SJY把件 Time Limit: 20 Sec  Memory Limit: 128 MB Submit: 1180  Solved: 391 [id=2648" style= ...

  7. thinkphp框架的路径问题 - 总结

    thinkphp框架的路径问题 - 总结 (2011-06-21 11:01:28) 转载▼ 标签: thinkphp 框架 路径 杂谈 分类: Php TP中有不少路径的便捷使用方法,比如模板中使用 ...

  8. 10. 混淆矩阵、总体分类精度、Kappa系数

    一.前言 表征分类精度的指标有很多,其中最常用的就是利用混淆矩阵.总体分类精度以及Kappa系数. 其中混淆矩阵能够很清楚的看到每个地物正确分类的个数以及被错分的类别和个数.但是,混淆矩阵并不能一眼就 ...

  9. 在IIS Express中调试时无法读取配置文件

    在IIS Express中调试代码时,如果出现“无法读取配置文件”的问题(如图),这种情况是IIS Express的“applicationhost.config”配置文件中的映射关系出了问题[ps: ...

  10. .NET AOP的实现

    一.AOP实现初步 AOP将软件系统分为两个部分:核心关注点和横切关注点.核心关注点更多的是Domain Logic,关注的是系统核心的业务:而横切关注点虽与核心的业务实现无关,但它却是一种更Comm ...