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 ;
}

最新文章

  1. Python mock
  2. 自定义加载loading view动画组件的使用。
  3. Joomla软件功能介绍与开源程序大比拼Joomla,wordpress,Drupal哪个好?
  4. iptables基础命令详解
  5. 二叉树的遍历(递归,迭代,Morris遍历)
  6. OpenCV成长之路 01、图像的读写与显示
  7. 多线程、多进程、协程、缓存(memcache、redis)
  8. #使用while循环输入1 2 3 4 5 6 8 9 10
  9. DB天气app冲刺第四天
  10. c#4.0新特性之协变与逆变
  11. 确认(confirm 消息对话框)
  12. HDU 3721 Building Roads (2010 Asia Tianjin Regional Contest) - from lanshui_Yang
  13. .net asp mvc 如何从后端返回数据对象
  14. mybatis——分页插件
  15. ThreadLocal 原理及一些实现
  16. BZOJ 4806 - 4809 象棋四题
  17. Python+Selenium笔记(十四)鼠标与键盘事件
  18. Jena RDF API
  19. Python__random库基本介绍
  20. 在asp.net一般应用程序中使用session

热门文章

  1. Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependen
  2. 串行&amp;并行&amp;并发,同步&amp;异步
  3. 041_go语言中的panic
  4. 铁大树洞APP视频讲解和原型演示
  5. Linux学习笔记之ubuntu如何在vi中写入中文注释
  6. vue 项目运行报错
  7. Linux探测工具BCC(可观测性)
  8. Java中的File类,递归是什么?
  9. JS实例-全选练习
  10. C# WebAPI项目,不支持HttpPut请求!!!