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. 03-注释与API文档

    1.注释:Comment分类: 单行注释:// 多行注释:/* */ 文档注释:/** */作用: ① 对所写的程序进行解释说明,增强可读性.方便自己,方便别人 ② 调试所写的代码特点: ①单行注释和 ...

  2. “随手记”开发记录day20

    练习软件的展示,尽量将软件全方面的展示给大众,希望不要像上次一样有许多遗漏的地方,让其他团队以为我们的软件没有完善的功能.

  3. Web For Pentester 学习笔记 - XSS篇

    XSS学习还是比较抽象,主要最近授权测的某基金里OA的XSS真的实在是太多了,感觉都可以做一个大合集了,加上最近看到大佬的博客,所以这里我也写一个简单的小靶场手册,顺带着也帮助自己把所有XSS的方式给 ...

  4. 高级搜索树-AVL树

    目录 平衡因子 AVL树节点和AVL树的定义 失衡调整 插入和删除操作 完整源码 AVL树是平衡二叉搜索树中的一种,在渐进意义下,AVL树可以将高度始终控制在O(log n) 以内,以保证每次查找.插 ...

  5. 设计模式实战系列之@Builder和建造者模式

    前言 备受争议的Lombok,有的人喜欢它让代码更整洁,有的人不喜欢它,巴拉巴拉一堆原因.在我看来Lombok唯一的缺点可能就是需要安装插件了,但是对于业务开发的项目来说,它的优点远远超过缺点. 我们 ...

  6. 调试备忘录-J-Link RTT的使用(原理 + 教程 + 应用 + 代码)

    MCU:STM32F407VE MDK:5.29 IAR:8.32 目录--点击可快速直达 目录 写在前面 什么是RTT? RTT的工作原理 RTT的性能 快速使用教程 高级使用教程 附上测试代码 2 ...

  7. python去除 数据的 重复行

    原文链接:https://www.cnblogs.com/loren880898/p/11303672.html

  8. 5. JsonFactory工厂而已,还蛮有料,这是我没想到的

    少年易学老难成,一寸光阴不可轻.本文已被 https://www.yourbatman.cn 收录,里面一并有Spring技术栈.MyBatis.JVM.中间件等小而美的专栏供以免费学习.关注公众号[ ...

  9. three.js 制作逻辑转体游戏(下)

    上一篇已经对绕非定轴转动有所了解,这篇郭先生继续说一说逻辑转体游戏的制作,这部分我们同样会遇到一些小问题,首先是根据数据渲染陷阱和目标区域,然后是对可以转动的判定,最后是获胜的判定. 1. 根据数据渲 ...

  10. Jmeter 常用函数(22)- 详解 __intSum

    如果你想查看更多 Jmeter 常用函数可以在这篇文章找找哦 https://www.cnblogs.com/poloyy/p/13291704.htm 作用 计算两个或多个整数值的和 语法格式 ${ ...