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.
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<cmath>
const int maxn=1e5+;
typedef long long ll;
using namespace std;
vector<int>vec[];
int n,m;
int du[];
int chu[];
int du1[];
int flag;
vector<int>ans1;
void Tpsort()
{
priority_queue<int,vector<int>,greater<int> >q;
priority_queue<int>q1; int s=;
for(int t=;t<n;t++)
{
if(chu[t]==&&du[t]==)
{
s=;
}
}
for(int t=;t<n;t++)
{
du1[t]=du[t];
}
for(int t=;t<n;t++)
{
if(du1[t]==)
{
q.push(t);
q1.push(t);
}
}
vector<int>ans,ans2;
while(!q.empty())
{
int now=q.top();
int now2=q1.top();
q.pop();
q1.pop();
ans.push_back(now);
ans2.push_back(now2);
for(int t=;t<vec[now].size();t++)
{
int next=vec[now][t];
du1[next]--;
if(du1[next]==)
{
q.push(next);
q1.push(next);
}
}
}
if(ans.size()!=n)
{
flag=;
}
// cout<<ans.size()<<" "<<s<<endl; if(ans.size()==n&&s==)
{
int sss=;
for(int t=;t<ans.size();t++)
{
if(ans[t]!=ans2[t])
{
sss=;
}
}
if(sss==)
{
flag=;
for(int t=;t<ans.size();t++)
{
ans1.push_back(ans[t]);
}
} } }
int main()
{ while(cin>>n>>m)
{
if(n==&&m==)
{
break;
}
for(int t=;t<n;t++)
{
vec[t].clear();
}
char str[];
memset(du,,sizeof(du));
memset(chu,,sizeof(chu));
flag=;
int ss=;
int k;
ans1.clear();
for(int t=;t<=m;t++)
{
scanf("%s",str);
vec[str[]-'A'].push_back(str[]-'A');
du[str[]-'A']++;
chu[str[]-'A']++;
if(ss)
{
continue;
}
Tpsort();
if(flag==)
{
printf("Inconsistency found after %d relations.\n",t);
ss=;
}
else if(flag==)
{
ss=;
printf("Sorted sequence determined after %d relations: ",t);
for(int j=;j<n;j++)
{
printf("%c",ans1[j]+'A');
}
printf(".\n"); }
}
if(ss==)
puts("Sorted sequence cannot be determined."); }
return ;
}

Sorting It All Out (拓扑排序+思维)的更多相关文章

  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. [poj1094]Sorting It All Out_拓扑排序

    Sorting It All Out poj-1094 题目大意:给出一些字符串之间的大小关系,问能否得到一个唯一的字符串序列,满足权值随下标递增. 注释:最多26个字母,均为大写. 想法:显然,很容 ...

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

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

  5. ZOJ 4124 拓扑排序+思维dfs

    ZOJ - 4124Median 题目大意:有n个元素,给出m对a>b的关系,问哪个元素可能是第(n+1)/2个元素,可能的元素位置相应输出1,反之输出0 省赛都过去两周了,现在才补这题,这题感 ...

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

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

  7. CF思维联系--CodeForces -214C (拓扑排序+思维+贪心)

    ACM思维题训练集合 Furik and Rubik love playing computer games. Furik has recently found a new game that gre ...

  8. [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 ...

  9. nyoj349 poj1094 Sorting It All Out(拓扑排序)

    nyoj349   http://acm.nyist.net/JudgeOnline/problem.php?pid=349poj1094   http://poj.org/problem?id=10 ...

随机推荐

  1. 如何打印完整的MYSQL带参数SQL日志信息

    在mysql的jdbc中开启sql分析,如下: jdbc.url=jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=U ...

  2. 几行python代码实现钉钉自动打卡,网友:终于告别缺勤了

    前言 众所周知因为疫情的原因大家都没有办法上学和上班,“钉钉”这个app起到了重大的作用.学校为了学生成绩开启“钉钉”之路.老师也成一个“合格”的主播,感谢XXX童鞋的礼物.666扣起来 老师为了营造 ...

  3. 基于.NetCore3.1系列 —— 日志记录之日志核心要素揭秘

    一.前言 在上一篇中,我们已经了解了内置系统的默认配置和自定义配置的方式,在学习了配置的基础上,我们进一步的对日志在程序中是如何使用的深入了解学习.所以在这一篇中,主要是对日志记录的核心机制进行学习说 ...

  4. Docker 阿里镜像

    Docker 配置阿里镜像 Dokcer 拉取镜像非常慢,配置阿里镜像加速. 步骤 首先注册阿里云,找到 "容器镜像服务" --> "镜像加速器" ,复制 ...

  5. 算法面试题:一个List<Student>,要求删除里面的男生,不用Linq和Lamda,求各种解,并说明优缺点!

    算法面试题:一个List,要求删除里面的男生,不用Linq和Lamda,求各种解,并说明优缺点! 解题思路 这是群里某位小伙伴去面试碰到的面试题,从题目本身来看,面试官应该是要考察面试者对泛型 Lis ...

  6. 极简 Node.js 入门 - 2.1 Path

    极简 Node.js 入门系列教程:https://www.yuque.com/sunluyong/node 本文更佳阅读体验:https://www.yuque.com/sunluyong/node ...

  7. Es6扩展运算符--三点运算符(...)--展开语法(Spread syntax)

    0.看文档呀 关于拓展运算符更详细的解释见 > MDN展开语法 关于剩余参数更详细的解释见 >MDN剩余参数 关于解构赋值更详细的解释见 >MDN解构赋值 直接看上面的文档更好 1. ...

  8. springboot中RedisTemplate的使用

    springboot中RedisTemplate的使用 参考 了解 Redis 并在 Spring Boot 项目中使用 Redis--以IBM为学习模板 springboot之使用redistemp ...

  9. day4 列表 字典 元组

      元组  不能修改里面的数据       字典是无序的集合  通过键名来访问元素       列表是有有序的  通过下标来访问    可以进行修改       列表  []   是python中使用 ...

  10. 图论算法(五)最小生成树Prim算法

    最小生成树\(Prim\)算法 我们通常求最小生成树有两种常见的算法--\(Prim\)和\(Kruskal\)算法,今天先总结最小生成树概念和比较简单的\(Prim\)算法 Part 1:最小生成树 ...