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. hdu5772-String problem(最大权闭合子图问题)

    解析: 多校标答 第一类:Pij 表示第i个点和第j个点组合的点,那么Pij的权值等于w[i][j]+w[j][i](表示得分)第二类:原串中的n个点每个点拆出一个点,第i个点权值为 –a[s[i]] ...

  2. python3-day1(文件操作)

    index: str.fomat() open file str.replace 一.新款str.fomat() 1.>>> '12'.zfill(5) '00012' 2.> ...

  3. jQuery Pagination Ajax分页插件中文详解(摘)

    jQuery Pagination Ajax分页插件中文详解 by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxin ...

  4. jqGrid源代码分析(一)

    废话少说.先上grid.base.js 整体结构图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3B5MTk4ODEyMDE=/font/5a6L5L2 ...

  5. tiny210(s5pv210)移植u-boot(基于 2014.4 版本号)——NAND 启动

    我们知道 s5pv210启动方式有非常多种,sd卡和nand flash 启动就是当中的两种,前面我们实现的都是基于sd卡启动,这节我们開始实现从nand flash 启动: 从 NAND 启动 u- ...

  6. Android 加速Gradle构建项目

    1. 升级gradle 进入项目文件夹$project/gradle/wrapper/gradle-wrapper.properties, 使用最新的gradle. 修改替换为最新的 distribu ...

  7. js 分割循环

    var str ='1,2,3'; var arr = str.split(","); var array1 =[]; var array2 =[]; for(i=0,l=arr. ...

  8. ScriptManager局部刷新

    ScriptManager和UpdatePanel控件联合使用可以实现页面异步局部更新的效果.其中的UpdatePanel就是设置页面中异步局部更新区域,它必须依赖于ScriptManager存在,因 ...

  9. Ubuntu eclipse 命令补全失效 (转载)

    我的eclipse 3.4,从ibm网站上下载解压后使用.发觉自动补全功能(alt + /)失效. 解决的办法: 1.(eclipse)window --> preferences --> ...

  10. ios禁用多按钮同时点下的效果

    只需要把那些不能同时点下的按钮或者视图设置一下即可. [view setExclusiveTouch:YES]; 避免view上多个button同时按下,则可设置每个button的setExclusi ...