[poj1094]Sorting It All Out_拓扑排序
Sorting It All Out poj-1094
题目大意:给出一些字符串之间的大小关系,问能否得到一个唯一的字符串序列,满足权值随下标递增。
注释:最多26个字母,均为大写。
想法:显然,很容易想到用toposort处理,对于每一个刚刚读入的大小关系,我们对进行一次拓扑排序,由于点数最多是26,所以总时间复杂度是$10^2$的。然后通过题面,可以发现第一个和第三个判定依据是可以中途退出的,而第二个条件是必须最后才可以判断的。但是由于poj的多组数据,我们必须要将所有的数据都读入完毕才能达到题目要要求。
最后,附上丑陋的代码... ...
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int map[27][27],indegree[27],q[27];
int TopoSort(int n) //拓扑排序
{
int c=0,temp[27],loc,m,flag=1,i,j; ////flag=1:有序 flag=-1:不确定
for(i=1;i<=n;i++)
temp[i]=indegree[i];
for(i=1;i<=n;i++)
{
m=0;
for(j=1;j<=n;j++)
if(temp[j]==0) { m++; loc=j; } //查找入度为零的顶点个数
if(m==0) return 0; //有环
if(m>1) flag=-1; // 无序
q[c++]=loc; //入度为零的点入队
temp[loc]=-1;
for(j=1;j<=n;j++)
if(map[loc][j]==1) temp[j]--;
}
return flag;
} int main()
{
int m,n,i,sign; //当sign=1时,已得出结果
char str[5];
while(scanf("%d%d",&n,&m))
{
if(m==0&&n==0) break;
memset(map,0,sizeof(map));
memset(indegree,0,sizeof(indegree));
sign=0;
for(i=1;i<=m;i++)
{
scanf("%s",str);
if(sign) continue; //一旦得出结果,对后续的输入不做处理
int x=str[0]-'A'+1;
int y=str[2]-'A'+1;
map[x][y]=1;
indegree[y]++;
int s=TopoSort(n);
if(s==0) //有环
{
printf("Inconsistency found after %d relations.\n",i);
sign=1;
}
if(s==1) //有序
{
printf("Sorted sequence determined after %d relations: ",i);
for(int j=0;j<n;j++)
printf("%c",q[j]+'A'-1);
printf(".\n");
sign=1;
}
}
if(!sign) //不确定
printf("Sorted sequence cannot be determined.\n");
}
return 0;
}
小结:toposort还是很有用的。
[poj1094]Sorting It All Out_拓扑排序的更多相关文章
- nyoj349 poj1094 Sorting It All Out(拓扑排序)
nyoj349 http://acm.nyist.net/JudgeOnline/problem.php?pid=349poj1094 http://poj.org/problem?id=10 ...
- 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_拓扑排序
题意:是否唯一确定顺序,根据情况输出 #include <iostream> #include<cstdio> #include<cstring> #include ...
- 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 ...
- POJ- 1094 Sorting It All Out---拓扑排序是否唯一的判断
题目链接: https://vjudge.net/problem/POJ-1094 题目大意: 该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列.是典型的拓扑排序,但输出格式上 ...
- POJ1094 Sorting It All Out LUOGU 排序
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 40012 Accepted ...
- POJ 1094:Sorting It All Out拓扑排序之我在这里挖了一个大大的坑
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29984 Accepted: 10 ...
- [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 ...
随机推荐
- org.apache.jasper.JasperException
1.错误描述 2014-7-13 17:20:50 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.service() ...
- Exynos4412交叉编译环境搭建
Exynos4412交叉编译环境搭建 交叉编译:在PC机(x86平台)上开发程序,在ARM板上运行,提高开发.编译速度. 环境: Tiny4412SDK1506开发板 需要软件: arm-linux- ...
- google浏览器插件推荐
http://www.tuicool.com/articles/eQ32Ur http://blog.jobbole.com/1386/ https://www.oschina.net/news/46 ...
- hihocoder Challenge 29 D. 不上升序列
这场爆零比赛题目还是要补的 这道题据说是出烂掉的原题,我找了下 CF13.C/ CF371 div1 C,一模一样 我服这群原题大佬 为 当时,使 不严格递增的最小步数,那么 Otherwise 显然 ...
- 2015 Multi-University Training Contest 4 Walk Out
Walk Out Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total S ...
- ASP.NET CORE的Code Fist后Models更改了怎么办?
上次我写到MVC的code fist后,自动生成数据库并自动生成web页面了 点击打开链接 那么随着项目需求的逐步明确,model变化了怎么办呢?其实和上次一样的,有两条关键的语句要记住 Add-Mi ...
- Halcon算子--区域特征
当我们想要提取Region时,图像处理后,往往存在几个类似的Region,此时,需要根据Region的一些特殊特征,来选择指定的Region. 求Region指定特征值:region_features ...
- mysql下如何删除本节点下的所有子节点小记
在开发过程中,经常会遇到树形结构的数据,在删除某个节点时候其所有的子节点都要被删除,可以使用如下方法: 1.添加记录该节点所有父节点的ID的字段(parent_ids),并用逗号隔开(一定是逗号),如 ...
- jquery实现简单的搜索
对一个简单的ul列表进行输入框的搜索功能,搜索之前及搜索后显示效果如下: 用到的主要jquery技术有filter()和match()方法以及正则匹配,基础HTML+div设置: <div cl ...
- tree、find、tail命令重要实战
tree -L 1 -d ln -s ext msn 创建软连接 ls -lF| sed -n‘/^d/p’ ls -lF|awk ‘/^d’ ls -lrt 按时间倒着排 vi /etc/pro ...