poj 1094 Sorting It All Out 拓补排序
Description
Input
Output
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. 题意:给一些大写字母的偏序关系。判断是否存在唯一的关系,或者他们的关系是矛盾的,即造成了回路,或者到最后不能确定他们的关系。
用拓扑排序:
1.拓扑排序可以用栈来实现,每次入栈的是入度为0的节点。
1.拓扑排序的结果一般分为三种情况:1、可以判断 2、有环出现了矛盾 3、条件不足,不能判断.
2.这道题不仅需要判断这三种情况,而且还要判断在处理第几个关系时出现前两种情况,对于本道题来说三种情况是有优先级的。前两种情况是平等的谁先出现先输出谁的相应结果,对于第三种情况是在前两种情况下都没有的前提下输出相应结果的.
#include <iostream>
#include <cstdio>
#include <stack>
#include <cstring>
using namespace std;
const int maxn=;
int n,m;
int pb[maxn][maxn],rd[maxn],list[maxn];
int toposort()
{
int i,j=;
int in[maxn];
memcpy(in,rd,sizeof(rd)); //复制入度数组,以防改变原数组
stack <int> s;
for(i=; i<n; i++) //入度为0的入栈
{
if(!in[i])
s.push(i);
}
bool flag=false;
while(!s.empty())
{
if(s.size()>)
flag=true;
int t=s.top(); // 记录出栈数字
s.pop();
list[j++]=t;
for(i=; i<n; i++)
{
if(pb[t][i])
{
in[i]--; //入度减一
if(!in[i])
s.push(i); //入度为0的继续入栈
}
}
}
if(j!=n) //存在回路
return ;
else if(flag) //有多重排序方式
return ;
return ;
}
int main()
{
while(cin>>n>>m,n||m)
{
memset(pb,,sizeof(pb));
memset(rd,,sizeof(rd));
char a,b;
int frist,scend;
frist=scend=;
for(int i=; i<=m; i++)
{
getchar();
scanf("%c<%c",&a,&b);
if(!frist && !scend)
{
if(pb[b-'A'][a-'A']==) //存在反向边
{
scend=;
printf("Inconsistency found after %d relations.\n",i);
continue;
}
if(!pb[a-'A'][b-'A'])
{
pb[a-'A'][b-'A']=;
rd[b-'A']++;
}
int res=toposort(); //进行拓扑排序
if(res==) //序列能够唯一被确定
{
printf("Sorted sequence determined after %d relations: ",i);
for(int j=; j<n; j++)
printf("%c",list[j]+'A');
printf(".\n");
frist=;
}
else if(res==) //存在回路
{
scend=;
printf("Inconsistency found after %d relations.\n",i);
}
} }
if(!frist&&!scend) //即不能唯一约定,又不构成回路
printf("Sorted sequence cannot be determined.\n");
}
return ;
}
poj 1094 Sorting It All Out 拓补排序的更多相关文章
- POJ 1094 Sorting It All Out【拓扑排序】
题目链接: http://poj.org/problem?id=1094 题意: 给定前n个字母的大小关系,问你是否 根据前xxx个关系得到上升序列 所有关系都无法确定唯一的一个序列 第xxx个关系导 ...
- POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39602 Accepted: 13 ...
- POJ 1094 Sorting It All Out【拓扑排序 / 比较字母大小】
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 38100 Accepted: 13453 ...
- [ACM] POJ 1094 Sorting It All Out (拓扑排序)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26801 Accepted: 92 ...
- POJ 1094 Sorting It All Out (拓扑排序,判断序列是否唯一,图是否有环)
题意:给出n个字符,m对关系,让你输出三种情况: 1.若到第k行时,能判断出唯一的拓扑序列,则输出: Sorted sequence determined after k re ...
- POJ 1094 Sorting It All Out 【拓扑排序】
<题目链接> 题目大意: 对于N个大写字母,给定它们的一些关系,要求判断出经过多少个关系之后可以确定它们的排序或者排序存在冲突,或者所有的偏序关系用上之后依旧无法确定唯一的排序. 解题分析 ...
- POJ - 1094 Sorting It All Out(拓扑排序)
https://vjudge.net/problem/POJ-1094 题意 对于N个大写字母,给定它们的一些关系,要求判断出经过多少个关系之后可以确定它们的排序或者排序存在冲突,或者所有的偏序关系用 ...
- 题解报告:poj 1094 Sorting It All Out(拓扑排序)
Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...
- ACM: poj 1094 Sorting It All Out - 拓扑排序
poj 1094 Sorting It All Out Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & ...
随机推荐
- SideBar 选择城市时右侧边上的 选择bar
需要定义一个SideBar的视图类 在布局文件中引用 同时在布局中设置一个textView默认不可见 当触摸时才显示 在调用的Activity中 sideBar.setOnTouchingL ...
- springboot 日志1
技术交流群: 816227112 Spring Boot在所有内部日志中使用Commons Logging,但是默认配置也提供了对常用日志的支持,如:Java Util Logging,Log4J, ...
- Memcached使用与纠错(附代码和相关dll)
今天没事研究一下,谁想到遇到了几个dll找不到,网上也不好找到,索性功夫不负有心人.贴出代码和相关的dll Memcached代码:(网上都是的,很多人都保存了这个代码) using Memcache ...
- 让 div中的div垂直居中的方法!!同样是抄袭来的(*^__^*)
同样 ,水平居中很简单,给子div设置margin:0px auto; 垂直居中也不难::给父div设置display:table-cell;vertical-align:middle; 重点是dis ...
- 4N - 素数回文
xiaoou33对既是素数又是回文的数特别感兴趣.比如说151既是素数又是个回文.现在xiaoou333想要你帮助他找出某个范围内的素数回文数,请你写个程序找出 a 跟b 之间满足条件的数.(5 &l ...
- OpenGL3D图形、旋转、纹理、键盘移动、光照、滤波、透明(完整) 转自http://www.cnblogs.com/tiandsp/archive/2012/01/23/2329049.html
#include <windows.h> // Windows的头文件 #include <stdio.h> #include <gl\gl.h> // OpenG ...
- PAT 1036 跟奥巴马一起编程(15)(代码)
1036 跟奥巴马一起编程(15)(15 分) 美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统.2014年底,为庆祝"计算机科学教育周& ...
- laravel 服务提供者介绍和使用
#安装传送门 安装composer,以及通过composer安装laravel #讲解使用 服务提供者这个具体表现都是围绕着依赖注入 在根目录config/app.php的providers中的数组中 ...
- 20172325 2018-2019-1 《Java程序设计》第二周学习总结
20172325 2018-2019-1 <Java程序设计>第二周学习总结 教材学习内容总结 3.1集合 集合是一种聚集.组织了其他对象的对象.集合可以分为两大类:线性集合和非线性集合. ...
- libnet 库使用(一)
该库的相关资料主要从源码包中获得(假设当前路径为源码包路径): ./sample 中有代码示例 ./doc/html 中html文件可以通过浏览器打开,参看函数定义 想要的基本上sample中都 ...