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. (二十七)mongodb操作学习

    在不使用mongodbVUE图形界面工具的情况下,打开数据库和集合后,如果要:    插入一条字段名为name,内容为java的记录,可以写成x={name:"java"};,然后 ...

  2. pat1111-1120

    1111 比较麻烦的最短路 #include<cmath> #include<map> #include<iostream> #include<cstring ...

  3. 【转载】Spark运行架构

    1. Spark运行架构 1.1 术语定义 lApplication:Spark Application的概念和Hadoop MapReduce中的类似,指的是用户编写的Spark应用程序,包含了一个 ...

  4. 如何通过java反射的方式对java私有方法进行单元测试

    待测试的私有方法: import org.testng.Assert;import org.testng.annotations.BeforeClass;import org.testng.annot ...

  5. 【Luogu3121】审查(AC自动机)

    题面 Description 农夫约翰为他的奶牛们购买了一份名字叫Good Hooveskeeping的定期杂志,因此奶牛们在挤奶期间就有了大量的阅读素材.遗憾的是在最新的一期上,有一篇有点儿不适当的 ...

  6. ssr panel魔改前端登陆面板配置邮件服务

    1.注册 打开mailgun官网http://www.mailgun.com/ (昨天他反应打开比较慢,所以你得有点耐心,或者跟换你的DNS试试)   QQ截图20140731095618.png 点 ...

  7. Python爬取豆瓣音乐存储MongoDB数据库(Python爬虫实战1)

    1.  爬虫设计的技术 1)数据获取,通过http获取网站的数据,如urllib,urllib2,requests等模块: 2)数据提取,将web站点所获取的数据进行处理,获取所需要的数据,常使用的技 ...

  8. 如何为开发项目编写规范的README文件(windows),此文详解

    为什么要写这篇博客? 其实我是一个入坑已经半年的程序员,因为不是计算机专业,只能自己摸索,所以我深知博客的重要性.每次我的学习笔记啊,项目的,面试题啊,有的,只要有时间,我肯定上传上来,一方面自己可以 ...

  9. mui实现切换选项卡

    mui切换选项卡头目对应选项内容是webAPP应用最多的功能 引入mui文件: 引入mui-segmented-control类实现顶部头目: 引入mui-content-padded类加入各选项卡内 ...

  10. 关于 promise 吃到错误的理解

    关于 promise 吃到错误的理解 下面的内容需要对浏览器原生支持的 promise 的基本用法有了解,如果你还不知道 promise 和 promise 的 catch 方法,你可能需要先在 这里 ...