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进制. 如果数据不大的话,那么这个题还是很简单的,但这个题就是数据范围太大,所以这里可以采用短除法来做. ...
随机推荐
- 关于Dagger 2 的使用方式
什么是Dagger2 Dagger是为Android和Java平台提供的一个完全静态的,在编译时进行依赖注入的框架,原来是由Square公司维护,现在由Google维护. 我们知道Dagger是一个依 ...
- applicationContext.xml简单笔记
applicationContext.xml简单笔记 <?xml version="1.0" encoding="UTF-8"?> <bean ...
- Using Spring Boot without the parent POM
Using Spring Boot without the parent POM: 问题 spring boot项目一般情况下的parent如下: <parent> <groupId ...
- C#窗体布局方式
DataGridView:显示数据表后台数据绑定: List<xxx> list = new List<xxx>(); dataGridView1.DataSource = l ...
- 关于电脑安装多个版本JDK后使用时的切换
描述:刚到新公司,自己安装了jdk1.7和开发工具myeclipse10,但是由于公司项目的需要(具体原因不详细描述了),需要使用myeclipse6.5和jdk1.6.于是在切换jdk1.7和jdk ...
- Maven引入本地jar包
<dependency> <groupId>${gorup}</groupId> <artifactId>${artifact}</artifac ...
- 一张图让你看懂各开源License[转]
你是否遇到过开源License,精炼而又晦涩的文字通常要读半天才能理解,而且大多数License差别不大,容易混淆.下面这张图让你段时间迅速掌握各种开源的License. 图片来源:阮一峰的博客. f ...
- window.requestAnimationFrame
今天小猪在看一个html5的demo时一直在找他的动画是怎么实现的,按照我的理解就应该是调用setInterval来循环调用动画函数来实现.但是在Demo中就是找不到这个函数.干着急的小猪只好一步一步 ...
- C#导出excel
public FileResult ExportExcels(string StartTime, string EndTime, string SendType) { List<SMSSExpo ...
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
题意:给定一个环形字符串,让他把它分成k份,使得最大的字典序 最小. 思路:二分答案,首先很明显答案所有可能是 n*n种 排序可以先求出最长公共前缀,这样比较就只需要比较公共前缀的下一位就能比较出两 ...