Sorting It All Out
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 33184   Accepted: 11545

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.

Source


因为要求几个relation,所以没加一个进行一次topoSort
一旦遇到有环或者唯一有序就sign=1,后面的关系一定要读完(后果自负),如果最后sign==0说明没确定顺序
要打印方案,可以用char  topo[]来保存,然而输出topo[]比一个个输出慢了16MS,并且还忘初始化了WA好几次
//
// main.cpp
// poj1094
//
// Created by Candy on 9/11/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=;
int n,m,ind[N],ch[N][N];
char tmp[],topo[N];
int q[N];
int topoSort(){
int flag=;//only sorted
int indt[N];
for(int i=;i<=n;i++) indt[i]=ind[i];
for(int i=;i<=n;i++){//printf("%d %d %d \n",i,ind[i],ch[i][0]);
int zero=,u=;
for(int j=;j<=n;j++) if(indt[j]==) zero++,u=j;
if(zero==) return ;//circle
if(zero>) flag=;//no sorted
indt[u]--;
topo[i-]=(char)u+'A'-;
q[i-]=u;
for(int j=;j<=ch[u][];j++)
indt[ch[u][j]]--;
}
return flag;
}
int main(int argc, const char * argv[]) {
while(cin>>n>>m){
if(n==&&m==) break;
memset(ch,,sizeof(ch));
memset(ind,,sizeof(ind));
memset(topo,,sizeof(topo));
int sign=; for(int i=;i<=m;i++){
scanf("%s",tmp);
if(sign) continue;
int x=tmp[]-'A'+,y=tmp[]-'A'+;
ind[y]++;
ch[x][++ch[x][]]=y;
int flag=topoSort();
if(flag==){sign=;printf("Inconsistency found after %d relations.\n",i);}
if(flag==){
sign=;printf("Sorted sequence determined after %d relations: %s.\n",i,topo);
// printf("Sorted sequence determined after %d relations: ",i);
// for(int j=0;j<n;j++)
// printf("%c",q[j]+'A'-1);
// printf(".\n");
// sign=1;
}
}
if(!sign) printf("Sorted sequence cannot be determined.\n");
}
return ;
}
 

POJ1094[有向环 拓扑排序]的更多相关文章

  1. HDU 3342 Legal or Not(有向图判环 拓扑排序)

    Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  2. [LOJ2114][HNOI2015]-菜肴制作-拓扑排序+贪心

    <题面> 一个蒟蒻的痛苦一天 在今天的节目集训中,麦蒙将带领大家学习9种错误的解题策略 $15\%$算法(看两个就往下走吧) 1> puts("Impossible!&qu ...

  3. [LeetCode] 207. 课程表(拓扑排序,BFS)

    题目 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1] 给定课程总量 ...

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

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

  5. POJ1094 拓扑排序

    问题:POJ1094   本题考查拓扑排序算法   拓扑排序:   1)找到入度为0的点,加入已排序列表末尾: 2)删除该点,更新入度数组.   循环1)2)直到 1. 所有点都被删除,则找到一个拓扑 ...

  6. [poj1094]Sorting It All Out_拓扑排序

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

  7. 有向无环图的应用—AOV网 和 拓扑排序

    有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...

  8. POJ 1094 Sorting It All Out (拓扑排序,判断序列是否唯一,图是否有环)

    题意:给出n个字符,m对关系,让你输出三种情况:     1.若到第k行时,能判断出唯一的拓扑序列,则输出:         Sorted sequence determined after k re ...

  9. hdoj 4324 Triangle LOVE【拓扑排序判断是否存在环】

    Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

随机推荐

  1. 从0开始学angularjs-笔记03

    大家好,今天上班第一天,可能大家都不是很想上班吧,我也是一样啦---不想上班就来继续写我的angualrjs教程,造福大家吧!! 今天的主要讲解部分有以下几点:1.演示一个完整的项目结构  2.$sc ...

  2. Extjs 使用fileupload插件上传文件 带进度条显示

    一.首先我们看看官方给出的插件的解释: 一个文件上传表单项具有自定义的样式,并且可以控制按钮的文本和 像文本表单的空文本类似的其他特性. 它使用一个隐藏的文件输入元素,并在用户选择文件后 在form提 ...

  3. sharepoint列表如何进行随机取几条记录?

    sharepoint列表如何进行随机取记录?由于itemid是不连续.可能存在删除添加等操作导致 我们可以采用随机取第几条记录.把记录集合取出来.产生随机第几条数.这里关键是如何产生不重复的随机数 方 ...

  4. Oracle EXP-00091的解决方法

    [sql] EXP-00091: 正在导出有问题的统计信息.www.2cto.com . . 正在导出表                      WF_GENERAL导出了 EXP-00091: 正 ...

  5. Android Animation学习(一) Property Animation原理介绍和API简介

    Android Animation学习(一) Property Animation介绍 Android Animation Android framework提供了两种动画系统: property a ...

  6. 【读书笔记】iOS-苹果的网络

    一,网络结构. 1,客户端服务器网络结构. 客户端服务器网络结构是一种主从网络结构.服务器是被动的,有点像餐厅的服务员.而客户端是主动的,像在餐厅吃饭的顾客. 2,对等网络结构. 对等网络也叫点对点网 ...

  7. 使用Android Annotations开发

    使用Android Annotations框架gradle配置1.修改Module下的build.gradle apply plugin: 'com.android.application' appl ...

  8. iOS之 Xcode7下 bitcode的工作流程及安全评估

    文章参考来自http://www.freebuf.com/articles/others-articles/89806.html 很多朋友在升级Xcode7以后原有正常运行的工程在Xcode7下编译会 ...

  9. maven编译java代码报“非法字符: \65279 ”错误的解决方法

    前段时间开发一个项目,使用maven进行编译时报“非法字符: \65279 ”,但是眼睛看怎么都正确的,文件转成utf-8编码还是报错. 最后没办法,暴力解决了这个问,步骤如下 1.把文件内容复制出来 ...

  10. TinyFox v2.3.2 正式发布,跨平台的.NET OWIN WEB服务器

    TinyFox 是一款按照 OWIN 协议开发的以支持各类 OWIN 应用为主要特征的高性能 WEB 服务器,2.3.2版已经正式发布,下载地址 http://www.linuxdot.net/.   ...