http://poj.org/problem?id=1094

题意:该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列。是典型的拓扑排序,但输出格式上确有三种形式:

1.该字母序列有序,并依次输出;

2.该序列不能判断是否有序

3.该序列字母次序之间有矛盾,即有环存在。

但是注意顺序,必须要先判断环,其次是无序,最后才是有序。

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std; #define Max 30 int gragh[Max][Max];
int ingreed[Max];
char ans[Max];
int n,m; int TopoSort()
{
int temp[Max],pos,flag = ,all = ; for(int i = ;i < n; i++)
temp[i] = ingreed[i]; for(int i = ;i < n; i++){
int num = ;
for(int j = ;j < n; j++){
if(temp[j] == ){
pos = j;
num++;
}
}
if(num == )
return ;
if(num > ) flag = -;
temp[pos] = -;
ans[all++] = pos;
for(int j = ;j < n; j++)
if(gragh[pos][j] == )
temp[j]--;
}
return flag;
} int main()
{
while(cin>>n>>m){
if(n == && m == )
break; memset(gragh,,sizeof(gragh));
memset(ingreed,,sizeof(ingreed)); char str[];
int sign = ;
for(int i = ;i <= m; i++){
cin>>str;
if(sign) continue;
gragh[str[] - 'A'][str[] - 'A'] = ;
ingreed[str[] - 'A']++; int s = TopoSort(); if(s == ){
printf("Inconsistency found after %d relations.\n",i);
sign = ;
} else if(s == ){
printf("Sorted sequence determined after %d relations: ",i);
for(int j = ;j < n; j++)
printf("%c",ans[j] + 'A' );
printf(".\n");
sign = ;
}
} if(!sign)
printf("Sorted sequence cannot be determined.\n");
}
return ;
}

POJ 1094 (TopoSort)的更多相关文章

  1. POJ 1094 (传递闭包 + 拓扑排序)

    题目链接: POJ 1094 题目大意:有 1 ~ N 个大写字母,且从 A 开始依次 N 个.再给你 M 个小于的关系,比如 A < B ,让你判断三种可能: 1.在第 i 个关系罗列之后,是 ...

  2. POJ题目(转)

    http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法:     (1)枚举. (poj1753,poj29 ...

  3. Repeater POJ - 3768 (分形)

    Repeater POJ - 3768 Harmony is indispensible in our daily life and no one can live without it----may ...

  4. Booksort POJ - 3460 (IDA*)

    Description The Leiden University Library has millions of books. When a student wants to borrow a ce ...

  5. Radar Installation POJ - 1328(贪心)

    Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. ...

  6. Best Cow Fences POJ - 2018 (二分)

    Farmer John's farm consists of a long row of N (1 <= N <= 100,000)fields. Each field contains ...

  7. E - The Balance POJ - 2142 (欧几里德)

    题意:有两种砝码m1, m2和一个物体G,m1的个数x1,  m2的个数为x2, 问令x1+x2最小,并且将天平保持平衡 !输出  x1 和 x2 题解:这是欧几里德拓展的一个应用,欧几里德求不定方程 ...

  8. poj 1094(拓扑排序)

    http://poj.org/problem?id=1094 题意:给你m个字母,有n个判断语句.求在哪个语句就可以判断出这个是不是一个环,或者在哪个语句可以判断出这些字母的排序规则,或者就是不能确定 ...

  9. poj 1220(短除法)

    http://poj.org/problem?id=1220 题意:进制转换,把a进制转换为b进制. 如果数据不大的话,那么这个题还是很简单的,但这个题就是数据范围太大,所以这里可以采用短除法来做. ...

随机推荐

  1. 从零开始HTML(三 2016/9/20)

    1.HTML表单 HTML 表单用于搜集不同类型的用户输入.<form> 元素,HTML 表单用于收集用户输入.<form> 元素 ①<input> 元素,< ...

  2. CSS 3 盒子属性

    #box1{ width: 100px;height: 40px; border: 1px solid black;(1)内容沾满盒子的处理方式 所有的都要添加前缀,以便更好的浏览器兼容  1,ove ...

  3. nodejs的第一天学习笔记

    一. js的模块化 什么是模块化: 模块化的概念最早是后台,随着ajax技术的兴起,js在编程中所占的地位越来越高,同时js的文件也相应的越来越多.为了方便文件的管理和更新,提出了js文件的模块 化, ...

  4. C学习笔记(八)字符输入输出和输入确认

    缓冲区 缓冲区分为两类:完全缓冲(fully buffered)I/O和行缓冲(line-buffered)I/O.完全缓冲在缓冲区满时被清空(内容被发送至目的地).这种类型常出现在文件输入中.缓冲区 ...

  5. 004_kafka_安装运行

    1.下载和安装 目前kafka的稳定版本为0.10.0.0 下载地址:http://kafka.apache.org/downloads.html 下载后解压缩安装包到系统即可完成安装 > ta ...

  6. DEV GridControl TableView隔行换色/奇偶行换色

    GridControl中的TableView“奇偶行换色”这件事情纠结了我好几天,虽然已经是上个月的事情,好歹记录一下吧,万一有谁要用到呢. GridControl是长这个样子的, <dxg:G ...

  7. OLE填充EXCEL 多SHEET

    "1 设置行高 "参数说明:行/列号.行高/列宽.R-行 C-列 FORM row_column USING p_r p_width p_type. CASE p_type. WH ...

  8. ShopNC小实例

    shopnc 之模式: shopnc一般的url访问为:http://localhost/shopnc1/admin/index.php?act=sui&op=hello Controller ...

  9. oracle中存储过程中调用存储过程

    存储过程中调用存储过程 create or replace package body PF_Role_Pack is procedure sp_GetPage_Role(pageSize_ in nu ...

  10. HDU 2955 01背包(思维)

    Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...