Sorting It All Out (拓扑排序+floyd)
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. 题意:n个字母,m个不等式,从1到m个不等式,问到第几个不等式的时候能确定字母间的大小关系,或者会出现矛盾(拓扑环),如果到m个不等式都无法确定,那就是无序的。::
注:这个从1到m是题目中没有给出的,但是题目确实是判断最少不等式确定有序,或者确定矛盾,如果都无法确定才认为是无序的。 思路:其实主要就是拓扑排序,拓扑排序过程中,如果没有入度为0的点,那么就存在环,就是矛盾的,floyd可有可无。
如果出现多个零点进入队列,那么就是无序的,但是无序优先级最低,所以不能立即退出,需要继续拓扑排序,判断完所有点入度情况,看看是否存在矛盾。
至于加入的floyd,由于floyd可以直接处理传递关系,就可以直接判出是否存在矛盾,然后,如果这时候再出现无序就能直接退出。 (若不使用floyd,代码如注释)
#include<queue>
#include<cstdio>
#include<cstring> using namespace std; int n,m; int maps[][];
struct Node
{
int a,b;
int val;
Node(int a=,int b=,int val=):a(a),b(b),val(val) {}
} node[]; int ans[];
bool topsort()
{
for(int k=;k<=n;k++)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
maps[i][j] |= maps[i][k] & maps[k][j];
}
}
}
for(int i=;i<=n;i++)if(maps[i][i])return ;
return ;
} int check()
{
if(topsort())return -;
int ind[];
memset(ind,,sizeof(ind));
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
if(maps[i][j])
{
ind[j]++;
}
}
}
int tot,top,flag=;
for(int i=;i<=n;i++)
{
tot = ;
for(int j=;j<=n;j++)
{
if(!ind[j])
{
tot++;
top = j;
}
}
if(tot >= ) return ;
// if(tot >= 2)flag = 0;
// if(!tot)return -1;
ans[i] = top;
ind[top] = -;
for(int i=;i<=n;i++)
{
if(maps[top][i])ind[i]--;
}
}
// return flag;
return ;
} int main()
{
while(~scanf("%d%d",&n,&m)&&n&&m)
{
int flag = ;
memset(maps,,sizeof(maps));
char a,b,c;
for(int i=; i<=m; i++)
{
scanf(" %c %c %c",&a,&b,&c);
if(b == '<')
maps[a-'A'+][c-'A'+] = ;
else
maps[c-'A'+][a-'A'+] = ;
if(!flag)
{
flag = check();
if(flag == )
{
printf("Sorted sequence determined after %d relations: ",i);
for(int j=; j<=n; j++)
printf("%c",ans[j]+'A'-);
puts(".");
}
else if(flag == -)
{
printf("Inconsistency found after %d relations.\n",i);
}
}
if(i == m && !flag)
printf("Sorted sequence cannot be determined.\n");
}
}
}
Sorting It All Out (拓扑排序+floyd)的更多相关文章
- [ACM_模拟] POJ 1094 Sorting It All Out (拓扑排序+Floyd算法 判断关系是否矛盾或统一)
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 & ...
- poj 1094 Sorting It All Out (拓扑排序)
http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- [poj1094]Sorting It All Out_拓扑排序
Sorting It All Out poj-1094 题目大意:给出一些字符串之间的大小关系,问能否得到一个唯一的字符串序列,满足权值随下标递增. 注释:最多26个字母,均为大写. 想法:显然,很容 ...
- POJ1094 Sorting It All Out —— 拓扑排序
题目链接:http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Tot ...
- POJ 1094:Sorting It All Out拓扑排序之我在这里挖了一个大大的坑
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29984 Accepted: 10 ...
- nyoj349 poj1094 Sorting It All Out(拓扑排序)
nyoj349 http://acm.nyist.net/JudgeOnline/problem.php?pid=349poj1094 http://poj.org/problem?id=10 ...
- POJ 1094 Sorting It All Out (拓扑排序) - from lanshui_Yang
Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...
- poj 1094 Sorting It All Out_拓扑排序
题意:是否唯一确定顺序,根据情况输出 #include <iostream> #include<cstdio> #include<cstring> #include ...
随机推荐
- 【CQOI2017】【BZOJ4813】小Q的棋盘 DFS
题目描述 有一棵树,你要从\(0\)号点开始走,你可以走\(m\)步,问你最多能经过多少个不同的点. \(n\leq 100\) 题解 出题人的做法是DP(一个简单的树形DP),但是可以直接通过一次D ...
- nginx服务器的基本配置
nginx作为反向代理搭建服务器的优点. 处理响应请求很快:单次请求会得到更快的响应.在高峰期,Nginx 可以比其它的 Web 服务器更快的响应请求 高并发连接:理论上,Nginx 支持的并发连接上 ...
- Codeforces 1037C Equalize
原题 题目大意: 给你两个长度都为\(n\)的的\(01\)串\(a,b\),现在你可以对\(a\)串进行如下两种操作: 1.交换位置\(i\)和位置\(j\),代价为\(|i-j|\) 2.反转位置 ...
- 精通Dubbo——Dubbo支持的协议的详解
转: 精通Dubbo——Dubbo支持的协议的详解 2017年06月02日 22:26:57 孙_悟_空 阅读数:44500 Dubbo支持dubbo.rmi.hessian.http.webse ...
- cacti报警邮件的设置
众所周知,用Linux系统自带的sendmail发送邮件是有限制的,可能对有些邮箱无法正常发送,导致报警邮件不能够及时发送到,因此就可能会产生不必要的麻烦!对此,我们可以用其他方法来发送邮件,就是在c ...
- kubernetes云平台管理实战:如何创建deployment更好(九)
一.文件创建带--record 1.文件 [root@k8s-master ~]# cat nginx_deploy.yml apiVersion: extensions/v1beta1 kind: ...
- SpringCloud笔记三:Eureka服务注册与发现
目录 什么是Eureka? Eureka注册的三大步 第一步,引用Maven 第二步,配置yml 第三步,开启Eureka注解 新建Eureka子项目 把provider子项目变成服务端 Eureka ...
- ubuntu文件搜索统计
一.在ubuntu下如何搜索文件 1.特点:快速,但是是模糊查找,例如 找 #whereis mysql 它会把mysql,mysql.ini,mysql.*所在的目录都找出来.我一般的查找都用这条命 ...
- MySQL学习6 - 完整性约束
一 介绍 二 not null 与default 三 unique 四 primary key 五 auto_increment 六 foreign key 快速理解foreign key 创建两张表 ...
- 可变与不可变类型数据,列表的copy方法
我们先来了解一下可变与不可变类型的数据 (1)可变类型:列表,字典(内存中的数据允许被修改) 不可变类型:数字,字符串,元组(内存中的数据不允许被修改) 接着我们通过一个实例来看一看可变与不可变类型数 ...