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_拓扑排序的更多相关文章

  1. nyoj349 poj1094 Sorting It All Out(拓扑排序)

    nyoj349   http://acm.nyist.net/JudgeOnline/problem.php?pid=349poj1094   http://poj.org/problem?id=10 ...

  2. POJ1094 Sorting It All Out —— 拓扑排序

    题目链接:http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Tot ...

  3. poj 1094 Sorting It All Out_拓扑排序

    题意:是否唯一确定顺序,根据情况输出 #include <iostream> #include<cstdio> #include<cstring> #include ...

  4. ACM: poj 1094 Sorting It All Out - 拓扑排序

    poj 1094 Sorting It All Out Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & ...

  5. poj 1094 Sorting It All Out (拓扑排序)

    http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  6. POJ- 1094 Sorting It All Out---拓扑排序是否唯一的判断

    题目链接: https://vjudge.net/problem/POJ-1094 题目大意: 该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列.是典型的拓扑排序,但输出格式上 ...

  7. POJ1094 Sorting It All Out LUOGU 排序

        Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 40012   Accepted ...

  8. POJ 1094:Sorting It All Out拓扑排序之我在这里挖了一个大大的坑

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29984   Accepted: 10 ...

  9. [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 ...

随机推荐

  1. tar (child): jdk-7u71-linux-x64.tar.gz:无法 open: 没有那个文件或目录

    1 错误描述 youhaidong@youhaidong:~$ sudo mkdir /usr/lib/jvm [sudo] password for youhaidong: youhaidong@y ...

  2. Error Curves HDU - 3714

    Josephina is a clever girl and addicted to Machine Learning recently. She pays much attention to a m ...

  3. Luogu P1410 子序列

    题目大意: 给定一个长度为\(N\)(\(N\)为偶数)的序列,] 问能否将其划分为两个长度为\(\frac{N}{2}\)的严格递增子序列, 输入一共有\(50\)组数据,每组数据保证\(N \le ...

  4. (1)Deep Learning之感知器

    What is deep learning? 在人工智能领域,有一个方法叫机器学习.在机器学习这个方法里,有一类算法叫神经网络.神经网络如下图所示: 上图中每个圆圈都是一个神经元,每条线表示神经元之间 ...

  5. CodeFirst之深入了解EntityFramework

    一.概要 本文在基于CodeFirst思想之上 深入了解EntityFramework.其实我个人一直头疼的问题就是每次Entity类一有变动,无论是新增表,更改表结构等 EF一律把数据库删掉重建,这 ...

  6. UWP 使用UCT的Markdown控件

    之前在网上偶然碰到过 一个在线的Markdown Text编辑器 http://mahua.jser.me/,功能很齐全. 然后就突然有了一个大胆的想法 这个玩意要是在uwp中实现,用来做更新日志说明 ...

  7. Oracle-数据类型为date 日期查询技巧

    date类型是oracle中存储日期类的一种常用类型,其处理也是在数据库使用中比较多需要注意的地方.如我们可以使用to_char函数将其转化为任意时间格式的字符串,也可使用to_date函数转化相应的 ...

  8. PDB调试python代码常用命令

    常用命令 where(w) 找出当前代码运行位置 list(l) 显示当前代码的部分上下文 list n(line number) 显示指定行的上下文 list m, n(line number) 显 ...

  9. pdf文件中截取eps图片并压缩

    最近遇到了一个问题,需要从pdf裁剪出其中部分的矢量图格式的图片,并保存为eps格式,方便使用. 最简单的方法就是先用acrobat pro将pdf进行页面抽取,并裁剪,剩下所需要的图片部分,然后另存 ...

  10. Python3基础教程1——Python的环境搭建

    2018年3月8日 当然推荐一个比较系统的教程 http://www.runoob.com/python3/python3-tutorial.html 人家也写的也比我好啦 本教程为新手向的,请大佬跳 ...