Sorting It All Out
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 40012   Accepted: 14072

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.

P1347 排序

题目描述

一个不同的值的升序排序数列指的是一个从左到右元素依次增大的序列,例如,一个有序的数列A,B,C,D 表示A<B,B<C,C<D。在这道题中,我们将给你一系列形如A<B的关系,并要求你判断是否能够根据这些关系确定这个数列的顺序。

输入输出格式

输入格式:

第一行有两个整数n,m,n表示需要排序的元素数量,2<=n<=26,第1到n个元素将用大写的A,B,C,D....表示。m表示将给出的形如A<B的关系的数量。

接下来有m行,每行有3个字符,分别为一个大写字母,一个<符号,一个大写字母,表示两个元素之间的关系。

输出格式:

若根据前x个关系即可确定这n个元素的顺序yyy..y(如ABC),输出

Sorted sequence determined after xxx relations: yyy...y.

若根据前x个关系即发现存在矛盾(如A<B,B<C,C<A),输出

Inconsistency found after 2 relations.

若根据这m个关系无法确定这n个元素的顺序,输出

Sorted sequence cannot be determined.

(提示:确定n个元素的顺序后即可结束程序,可以不用考虑确定顺序之后出现矛盾的情况)

输入输出样例

输入样例#1:

1:
4 6
A<B
A<C
B<C
C<D
B<D
A<B 2:
3 2
A<B
B<A 3:
26 1
A<Z
输出样例#1:

1:
Sorted sequence determined after 4 relations: ABCD.
2:
Inconsistency found after 2 relations.
3:
Sorted sequence cannot be determined.

由题目描述可知:

  该题 典型的拓扑排序。

  1、在 拓扑排序时,当出现同时可访问两个节点或多个节点时,则信息不完全。

  2、当出现有环时,则有冲突,判断是否有环可以在拓扑排序完时判断是否有点未访问。因为若无环,每个点都可以访问。

不多说了,附上我的AC代码:

 #include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int MAXN=;
int n,m,cas,id; struct edge
{
int v,nx;
}set[MAXN];
int head[],d[],ok[],write[];
queue<int> Q; void Addedge(int u,int v)
{
id++;set[id].v=v;set[id].nx=head[u];
head[u]=id;
} int bfs()
{
cas=;
while(!Q.empty())Q.pop();
int out=,t,u;t=;
for(int i=;i<=n;i++)
if(!ok[i])
{
t++;Q.push(i);
}
if(t>)out=;
while(!Q.empty())
{
u=Q.front();Q.pop();t=;write[++cas]=u;
for(int k=head[u];k>;k=set[k].nx)
{
ok[set[k].v]--;
if(!ok[set[k].v])
{
t++;Q.push(set[k].v);
}
}
if(t>)out=;
}
for(int i=;i<=n;i++)if(ok[i]>){out=-;break;}
return out;
} char print()
{
for(int i=;i<=cas;i++)printf("%c",(char)(write[i]+));
return '.';
} int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(write,,sizeof(write));
memset(d,,sizeof(d));
memset(head,-,sizeof(head));
id=;
int now=,loca;
char a,b,c;
if(n== && m==)break;
for(int i=;i<=m;i++)
{
cin>>a>>b>>c;
if(b=='<')
{
Addedge(a-,c-);d[c-]++;
}
else
{
Addedge(c-,a-);d[a-]++;
}
if(now==)
{
for(int j=;j<=n;j++)ok[j]=d[j];
now=bfs();loca=i;
}
}
if(now==)puts("Sorted sequence cannot be determined.");
else if(now==)printf("Sorted sequence determined after %d relations: ",loca),printf("%c\n",print());
else printf("Inconsistency found after %d relations.\n",loca);
}
return ;
}

记得点赞欧。

P1347 排序

POJ1094 Sorting It All Out LUOGU 排序的更多相关文章

  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. POJ- 1094 Sorting It All Out---拓扑排序是否唯一的判断

    题目链接: https://vjudge.net/problem/POJ-1094 题目大意: 该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列.是典型的拓扑排序,但输出格式上 ...

  4. POJ1094 Sorting It All Out —— 拓扑排序

    题目链接:http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Tot ...

  5. POJ1094 Sorting It All Out(拓扑排序)

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

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

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

  8. PAT A1028 List Sorting (25 分)——排序,字符串输出用printf

    Excel can sort records according to any column. Now you are supposed to imitate this function. Input ...

  9. poj1094 Sorting It All Out【floyd】【传递闭包】【拓扑序】

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

随机推荐

  1. Maven项目POM文件错误,提示“Plugin execution not covered by lifecycle configuration”的解决方案

    一. 问题 Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-depend ...

  2. css隐藏滚动条

    xhtml中隐藏滚动条在用ie6浏览有框架的xhtml页面的时候,默认会水平和垂直滚动条会一起出现,这是ie6的一个bug,在firefox上是正常的,出现的原因是其对XHTML 1.0 transi ...

  3. vue.js引入

    开始学习vue.js,引入vue.vue.js一定要在head里面引入,实际开发中我们可能在body中引入,但是可能存在抖屏现象. 为了避免出现抖屏现象,我们引入vue.js或者jquery.js 最 ...

  4. 在ubuntu16.04中再次体验.net core 2.0

    在上一篇文章中在ubuntu16.04中初次体验.net core 2.0 简单介绍了一下ubuntu中运行.net core 2.0.配置nginx反向代理以及安装supervisor守护进程……本 ...

  5. Djang之cookie和session

    一 会话跟踪 我们需要先了解一下什么是会话!可以把会话理解为客户端与服务器之间的一次会晤,在一次会晤中可能会包含多次请求和响应.例如你给10086打个电话,你就是客户端,而10086服务人员就是服务器 ...

  6. ASP.NET C# 连接 Oracle数据库增删改查,事务

    一.知识介绍 ①ASP.NET 使用的是MVC模式,开发工具Visual studio ,语言C# ②Oracle是比较重型的数据库,这里主要介绍连接数据库,对数据进行具体的使用 ③Visual St ...

  7. 微信小程序基本目录结构学习

    今天我们就以firstdemo为例,介绍一下小程序的基本目录结构.当我们打开一个微信小程序项目后,点击进入“编辑”菜单,我们可以看到有以下5个文件/文件夹):pages文件夹,utils文件夹,全局文 ...

  8. PJSUA2开发文档--第九章 PJSUA2应用程序示例

    9. PJSUA2示例应用程序 9.1 示例应用程序 9.1.1 C++ pjsip-apps/src/samples/pjsua2_demo.cpp 是一个非常简单可用的C++示例应用程序. /* ...

  9. REST风格架构

    一说到rest 大家都耳熟能详,很多人的第一反应就是其是前后端请求后台的一种通信方式,甚至有些人将REST 和RPC 混为一谈,认为两者都是基于HTTP类似的东西.实际上很少人能叙述REST 所提出的 ...

  10. 索引-mysql

    什么是索引 索引的四大作用 (参考百度百科:百度百科-索引) 索引的优缺点 索引分类 什么地方需要建索引 索引优化 什么是索引? 宏观来说,索引是一种检索工具,目录也是一种检索工具,但是两者是有区是别 ...