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

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. 题意:这题题意好蛋疼啊!!!一个一个给边,一个一个的判断,先判断是否有环。然后再判断是否确定序列。最后前两者都不满足,则此序列不确定。
收获:1.知道只有有环时才k<n。2.入度为0的点不唯一,则此序列不确定。3.入度很重要。
下面是能AC代码,但是 0 0会有问题,不知道为什么?还请各位指教。
#include <cstdio>
#include <iostream>
//#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0); const int maxn=;
struct Edge
{
int u, v, next;
};
Edge edge[maxn];
int head[maxn];
int num, n,m;
void init_edge()
{
num = ;
memset(head, -, sizeof(head));
}
void addedge(int u, int v)
{
edge[num].u = u;
edge[num].v = v;
edge[num].next = head[u];
head[u] = num++;
}
int ans;
int topo[maxn];
int in[maxn];
int topsort()
{
queue<int> q;
int indeg[];
for(int i = ; i < +n; i++)
{
indeg[i] = in[i];
if(indeg[i] == ) q.push(i);
}
int k = ;
int flag=;
while(!q.empty())
{
if(q.size()>)
flag = ;
int u = q.front();
q.pop();
topo[k++] = u;
for(int i = head[u]; i != -; i = edge[i].next)
{
int v = edge[i].v;
indeg[v]--;
if(indeg[v]==)
q.push(v);
}
}
if(k < n)
return ;
if(flag)
return -;
else
return ;
}
char s[];
int main()
{
while(~scanf("%d%d", &n, &m))
{
if(n== && m==)
break;
init_edge();
memset(in, , sizeof(in));
int flag2 = ;
int flag3 = ;
for(int j = ; j <= m; j++)
{
scanf("%s",s);
if(!flag2&&!flag3)
{
in[s[]]++;
addedge(s[],s[]);
int res = topsort();
if(res == )
{
printf("Inconsistency found after %d relations.\n", j);
flag2 = ;
}
if(res == )
{
printf("Sorted sequence determined after %d relations: ", j);
for(int i=; i < n; i++)
printf("%c", topo[i]);
printf(".\n");
flag3 = ;
}
}
}
if(!flag2&&!flag3)
puts("Sorted sequence cannot be determined."); //puts("QAQ");
}
return ;
}

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

  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. POJ1094 Sorting It All Out —— 拓扑排序

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

  4. ACM: poj 1094 Sorting It All Out - 拓扑排序

    poj 1094 Sorting It All Out Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & ...

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

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

  6. POJ- 1094 Sorting It All Out---拓扑排序是否唯一的判断

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

  7. POJ1094 Sorting It All Out LUOGU 排序

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

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

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

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

  10. POJ 1094 Sorting It All Out (拓扑排序) - from lanshui_Yang

    Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...

随机推荐

  1. makefile例子《一》

    一.例子 (1)makefile和src源文件不在同一目录下 (2)把.o生成到指定目录下 文件结构目录 ----inc      //放头文件 ----lib //放所需要的.a或者.so文件 -- ...

  2. Android应用切换皮肤功能实现

    原文地址:http://www.eoeandroid.com/thread-318159-1-1.html 现在大多数android应用都支持切换皮肤的功能.比如千千静听,墨迹天气等等.本文介绍两种切 ...

  3. 如何搞定前端资源服务跨域问题之nginx篇

    问题描述 1.首先让我们先看一张图 2.从图中,我们可以很清楚的看到当http请求的站点访问https的资源的时候会报出“Cross-Origin”跨域的问题.为什么会出现这样的错误,这是因为涉及到“ ...

  4. Centos 7 python升级(2.7.5-》2.7.11)

    1.安装升级GCC yum install gcc* openssl openssl-devel ncurses-devel.x86_64  bzip2-devel sqlite-devel pyth ...

  5. HTTP协议4之缓存--转

    HTTP协议提供了非常强大的缓存机制, 了解这些缓存机制,对提高网站的性能非常有帮助. 缓存的概念 缓存这个东西真的是无处不在, 有浏览器端的缓存, 有服务器端的缓存,有代理服务器的缓存, 有ASP. ...

  6. 细讲encodeURI和encodeURIComponent以及escape的区别与应用

    首先,我们都知道这三个东西都是用来编码的 先来说encodeURI()和encodeURIComponent() 这两个是在转换url时候用来编码解码用的. 有编码就会有解码, 解码就是decodeU ...

  7. CSS---------------之文本颜色

    CSS2支持如下名字的颜色 注意点: 你的浏览器有可能支持更多名字,但是在实际用的过程中尽量少使用名字的,因为各个浏览器对颜色的会存在差异:查看颜色可以参考 对于更多地颜色,你可以使用代表红,绿,蓝三 ...

  8. 在SQL SERVER中批量替换字符串

    update [表名] set [字段名]=replace([字段名],'被替换原内容','替换后内容')

  9. EasyuiCombobox三级联动

    有许多刚接触Easyui中Combobox控件的朋友可能都会遇到的问题:如何将Combobox做成三级联动? 先本人有个三级联动的案例给大家参考参考,经测试能通过.注意Combobox绑定的数据是Js ...

  10. iOS-设计模式之Block

    Block是代码块, Block定义 返回值 (^ 块名)(参数1,参数2…); 在定义Block的时候可以使用typedef 重命名一下. typedef void(^blockName)(NSSt ...