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. Dotfuscator混淆加密

    混淆加密 1. 需要安装Dotfuscator软件 2. 安装好后打开软件,找到编译好的DLL文件 3. 打开[setting]设置属性,如下图: 把 Disable String Encryptio ...

  2. SharePoint 2013 日历视图兼容性问题

    在IE11上访问SharePoint 2013 calendar视图,发现加入兼容性视图以后访问,正常,如下图: 不加入兼容性视图IE11访问,出现兼容性问题,如下图: 因为有些环境有问题,有些环境没 ...

  3. UIModalPresentationStyle和UIModalTransitionStyle

    一.主要用途 弹出模态ViewController是IOS变成中很有用的一个技术,UIKit提供的一些专门用于模态显示的ViewController,如UIImagePickerController等 ...

  4. android绘制view的过程

    1 android绘制view的过程简单描述  简单描述可以解释为:计算大小(measure),布局坐标计算(layout),绘制到屏幕(draw):            下面看看每一步的动作到底是 ...

  5. pod install 和 pod update的区别

    pod install 和 pod update的区别 pod install(下载并安装pod) 1,当pod file文件中有“增加pod,删除pod,修改pod”的操作之后使用. 2,pod i ...

  6. OC与JS的交互

    现在APP的开发已经不局限于原生开发,很多都是原生+html5这种混合开发 我们可以通过webView这个控件,实现混合开发. 1.首先你需要创建一个html页面 <html> <h ...

  7. postgresql function 返回 select

    pq函数功能很强大,我打算把统计的功能都放在数据库端.优势让运算离数据更近一些,缺点无法服用代码.牺牲了django的灵魂性,项目必须依赖postgresql. 项目中,希望实现返回select内容 ...

  8. Oracle BIEE启停脚本

    作为BI的开发人员,经常启停BI服务在所难免,启动的过程又比较长,命令需要不同目录切换,简直烦死人呢, 特意整理了linux中的启动脚本,将以下脚本存成biee.sh,后面的过程就相当简单了, 启动: ...

  9. mysql datetime查询异常

    mysql datetime查询异常 异常:Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp (2011 ...

  10. 在JSP动态网页中输出九九乘法表

    <% StringBuffer sb = new StringBuffer(); ; i <= ; i++){ ; j <= i; j++){ sb.append(j + " ...