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

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

 
分析:
有向图判环:拓扑排序
无向图判环:并查集
 
本题需要进行m次的拓扑排序
拓扑排序判环:拓扑之后,如果存在没有入队的点,那么该点一定是环上的点
拓扑路径的唯一性确定:队中某一时刻存在两个元素,则至少有两条不同的拓扑路径
 
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
int mon1[]= {,,,,,,,,,,,,};
int mon2[]= {,,,,,,,,,,,,};
int dir[][]= {{,},{,-},{,},{-,}}; int getval()
{
int ret();
char c;
while((c=getchar())==' '||c=='\n'||c=='\r');
ret=c-'';
while((c=getchar())!=' '&&c!='\n'&&c!='\r')
ret=ret*+c-'';
return ret;
} #define max_v 55
int indgree[max_v];
int temp[max_v];
int G[max_v][max_v];
int tp[max_v];
int n,m;
queue<int> q;
int tpsort()
{
while(!q.empty())
q.pop();
for(int i=;i<=n;i++)
{
indgree[i]=temp[i];
if(indgree[i]==)
q.push(i);
} int c=,p;
int flag=;
while(!q.empty())
{
if(q.size()>)
flag=;
p=q.front();
q.pop();
tp[++c]=p;
for(int i=;i<=n;i++)
{
if(G[p][i])
{
indgree[i]--;
if(indgree[i]==)
q.push(i);
}
}
}
/*
拓扑完之后,存在没有入队的点,那么该点就一定是环上的
*/
if(c!=n)//存在环
return ;
else if(flag)//能拓扑但存在多条路
return ;
return -;//能拓扑且存在唯一拓扑路径
}
int main()
{
int x,y;
char c1,c2;
while(~scanf("%d %d",&n,&m))
{
if(n==&&m==)
break;
memset(G,,sizeof(G));
memset(temp,,sizeof(temp));
memset(tp,,sizeof(tp));
int flag1=,index1=;//是否有环及环的位置
int flag2=,index2=;//能否拓扑和拓扑的位置
for(int i=;i<=m;i++)
{
getchar();
scanf("%c<%c",&c1,&c2);
x=c1-'A'+;
y=c2-'A'+;
if(flag1==&&flag2==)
{
if(G[y][x])//环的一种情况
{
flag1=;
index1=i;
continue;
}
if(G[x][y]==)//预防重边
{
G[x][y]=;
temp[y]++;
}
int k=tpsort();
if(k==)//存在环
{
flag1=;
index1=i;
continue;
}else if(k==-)//存在唯一拓扑路径
{
flag2=;
index2=i;
}
}
}
if(flag1==&&flag2==)
{
printf("Sorted sequence cannot be determined.\n");
}else if(flag1)
{
printf("Inconsistency found after %d relations.\n",index1);
}else if(flag2)
{
printf("Sorted sequence determined after %d relations: ",index2);
for(int i=;i<=n;i++)
{
printf("%c",tp[i]+'A'-);
}
printf(".\n");//!!!注意还有个点...
}
}
return ;
}

POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)的更多相关文章

  1. Legal or Not(拓扑排序判环)

    http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Others)   ...

  2. LightOJ1003---Drunk(拓扑排序判环)

    One of my friends is always drunk. So, sometimes I get a bit confused whether he is drunk or not. So ...

  3. HDU1811 拓扑排序判环+并查集

    HDU Rank of Tetris 题目:http://acm.hdu.edu.cn/showproblem.php?pid=1811 题意:中文问题就不解释题意了. 这道题其实就是一个拓扑排序判圈 ...

  4. [bzoj3012][luogu3065][USACO12DEC][第一!First!] (trie+拓扑排序判环)

    题目描述 Bessie has been playing with strings again. She found that by changing the order of the alphabe ...

  5. Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)

    Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megaby ...

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

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

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

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

  8. poj 1094 Sorting It All Out 拓补排序

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

  9. HDU 3342 Legal or Not(有向图判环 拓扑排序)

    Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

随机推荐

  1. RBAC 介绍 (权限)

    RBAC是什么? RBAC是基于角色的访问控制(Role-Based Access Control )在RBAC中,权限与角色相关联,用户通过成为适当角色的成员而得到这些角色的权限.这就极大地简化了权 ...

  2. es6 export与export default 的区别

    相同点: 均可用于导出常量.函数.文件.模块等 不同点: 1.在一个文件中export可以有多个,但export default 只能有一个: export var firstName = 'Mich ...

  3. 自定义适用于手机和平板电脑的 Dynamics 365(二):窗体自定义项

    适用于手机的 Dynamics 365 和 适用于平板电脑的 Dynamics 365 使用窗体作为 Web 应用. 窗体在应用程序中的显示方式为移动体验进行了优化. 下图显示了从 Web 应用程序到 ...

  4. AOP编程 - 淘宝京东网络处理

    现象描述 当我们打开京东 app 进入首页,如果当前是没有网络的状态,里面的按钮点击是没有反应的.只有当我们打开网络的情况下,点击按钮才能跳转页面,按照我们一般人写代码的逻辑应该是这个样子: /** ...

  5. [Objective-C] Block实现回调和简单的学习思考

    初识Block的时候,总觉得其很可怕,因为看不懂其运行原理,所以用起来总是觉得不安全.关于Block的语法,等我把手里的资料全部看完,整理好再发出来.这次先看看用Block怎么实现回调. 新博客:wo ...

  6. [Android] 针对生成的图片文件在系统Gallery不显示的处理

    之前遇到过一个问题,就是发现我在程序中生成一个新的 Bitmap 之后,当我打开系统的 Gallery 查看时,并没有看到新生成的图像.然而打开文件浏览器,找到保存 Bitmap 所在的文件夹下,还能 ...

  7. LintCode题解之Search Range in Binary Search Tree

    1.题目描述 2.问题分析 首先将二叉查找树使用中序遍历的方式将元素放入一个vector,然后在vector 中截取符合条件的数字. 3.代码 /** * Definition of TreeNode ...

  8. 【SPL标准库专题(9)】 Datastructures:SplObjectStorage

    PHP SPL SplObjectStorage是用来存储一组对象的,特别是当你需要唯一标识对象的时候. PHP SPL SplObjectStorage类实现了Countable,Iterator, ...

  9. C#多线程的用法9-Semaphore

    Semaphore:可理解为允许线程执行信号的池子,池子中放入多少个信号就允许多少线程同时执行. private static void MultiThreadSynergicWithSemaphor ...

  10. Azure 托管镜像和非托管镜像对比

    目前中国区 Azure 也已经可以使用命令制作托管镜像了.但对于托管镜像和非托管镜像,就像托管磁盘和非托管磁盘一样,很多人可能一开始无法理解.这里就此进行了一个简单对比: 通过对比测试,这里总结了这两 ...