POJ 1094 (TopoSort)
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)的更多相关文章
- POJ 1094 (传递闭包 + 拓扑排序)
题目链接: POJ 1094 题目大意:有 1 ~ N 个大写字母,且从 A 开始依次 N 个.再给你 M 个小于的关系,比如 A < B ,让你判断三种可能: 1.在第 i 个关系罗列之后,是 ...
- POJ题目(转)
http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法: (1)枚举. (poj1753,poj29 ...
- Repeater POJ - 3768 (分形)
Repeater POJ - 3768 Harmony is indispensible in our daily life and no one can live without it----may ...
- Booksort POJ - 3460 (IDA*)
Description The Leiden University Library has millions of books. When a student wants to borrow a ce ...
- Radar Installation POJ - 1328(贪心)
Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. ...
- Best Cow Fences POJ - 2018 (二分)
Farmer John's farm consists of a long row of N (1 <= N <= 100,000)fields. Each field contains ...
- E - The Balance POJ - 2142 (欧几里德)
题意:有两种砝码m1, m2和一个物体G,m1的个数x1, m2的个数为x2, 问令x1+x2最小,并且将天平保持平衡 !输出 x1 和 x2 题解:这是欧几里德拓展的一个应用,欧几里德求不定方程 ...
- poj 1094(拓扑排序)
http://poj.org/problem?id=1094 题意:给你m个字母,有n个判断语句.求在哪个语句就可以判断出这个是不是一个环,或者在哪个语句可以判断出这些字母的排序规则,或者就是不能确定 ...
- poj 1220(短除法)
http://poj.org/problem?id=1220 题意:进制转换,把a进制转换为b进制. 如果数据不大的话,那么这个题还是很简单的,但这个题就是数据范围太大,所以这里可以采用短除法来做. ...
随机推荐
- 浏览器何时发送一个Option请求
Http Options Method 简而言之,OPTIONS请求方法的主要用途有两个: 1.获取服务器支持的HTTP请求方法: 2.用来检查服务器的性能. CORS(跨域资源共享) CORS是一种 ...
- stm32cube--通用定时器--输入捕获
用定时器输入捕获做红外线接收实验.(此次试验以通道2为例) ①stm32cube配置 ② ③ ④程序中主要用到的输入捕获相关寄存器 uint16_t tim_sr,tim_ccer,tim_ccr; ...
- 函数指针与指针函数以及typedef
c难于理解的是指针,其魅力之处也是指针,函数方法结构,化繁为简可以理解为:返回值 函数名(形参表),具体来说: 返回值:1.可以为空void 2.基本数据类型char short int long f ...
- Android API 21 Toolbar Padding
up vote117down votefavorite 44 How do I get rid of the extra padding in the new Toolbar with Android ...
- 下载zip格式文件(压缩Excel文件为zip格式)
Mongodb配置文件参考这一篇:http://www.cnblogs.com/byteworld/p/5913061.html package util; import java.io.Buffer ...
- vue学习笔记之v-for与-repeat
今天看到一个v-repeat的例子 <body> <ul id="tags"> <li v-repeat="tags"> { ...
- jquery.uploadify 动态传递参数
最近 项目中使用到 uplaodify 来实现上传文件的功能.在传输动态参数的时候,遇到了问题! 使用官网提供的 settings 方法 官方例子function changeBtnText() { ...
- 查看Msi文件内容
1通过msiexec命令解压msi包 msiexec.exe /a c:\msi\installer.msi /qb targetdir=d:\msi\installer 2 使用Orca查看. Or ...
- 读javascript高级程序设计16-几条函数小技巧
内容概要 作用域安全的构造函数 惰性载入函数 函数绑定 函数节流 一.作用域安全的构造函数 我们知道,当使用new操作符调用构造函数时,构造函数内部的this会指向新创建对象的实例. function ...
- 编译安装php 5.5 缺少依赖包 及解决方案
必要时可以用 YUM 选择安装以下相关软件包: #yum install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel ...