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. Java 学习笔记 两大集合框架Map和Collection

    两大框架图解 Collection接口 由第一张图,我们可以知道,Collection接口的子接口有三种,分别是List接口,Set接口和Queue接口 List接口 允许有重复的元素,元素按照添加的 ...

  2. Acer宏碁笔记本触摸板失效解决方法

    打开windows设置,找到鼠标设置 之后,选择触摸板设置,将其开启 PS: 由于我是安装完驱动之后,才发现有这个触摸板设置的 如果找不到这个触摸板设置的哈,应该就是驱动安装完之后就有了 驱动的话去官 ...

  3. 【代码笔记】Web-CSS-CSS Display

    一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  4. freemark使用总结

    1.下拉框中使用三元表达式: <option value="1类" ${(bean.col5!?string="1类")?string('selected ...

  5. 推荐一款好用的office转换PDF工具

    北京博信施科技有限公司是一家专业从事数据格式转换.数据处理领域研发软件产品和解决方案实施的技术型公司.在当今信息时代,PDF文档格式是在Internet上进行电子文档发行和数字化信息传播的理想文档格式 ...

  6. 39.Odoo产品分析 (四) – 工具板块(8) – 生产力(1)

    查看Odoo产品分析系列--目录 生产力相当于一个即时贴或便签.用便签或待办事项处理个人的任务.  安装生产力模块,得到"便签"主菜单:  创建一个便签,该表单对应note.not ...

  7. iOS----------对单元格取余

    if (indexPath.row % 2 == 0) { cell.backgroundColor = [UIColor magentaColor]; }else{ cell.backgroundC ...

  8. Android为TV端助力 集成第三方播放器,实现全屏播放

    下面这Demo链接:Android实现全屏播放,各种格式支持直播,点播,不收费!

  9. UE3客户端加入DS过程

    拉起DS进程 客户端将比赛地图及相关参数发送给ZoneSvr请求开赛,收到消息后,ZoneSvr会分配一个ip和端口号,并与客户端发过来的地图及其他参数,来构建一个命令行来拉起一个DS进程, DS启动 ...

  10. WordCount

    一.Gitee地址:https://gitee.com/zjgss99/WordCount 二.项目分析: 对程序设计语言源文件统计字符数.单词数.行数,统计结果以指定格式输出到默认文件中,以及其他扩 ...