每次加入一个关系都要进行拓扑排序,不过在排序过程中需要判断是否出现多个入度为0的点,如果出现了就说明不能确定大小关系。不论出不出现多个入度为0的点拓扑排序都要进行到最后来判断是否出现环,因为一旦出现环就不用再比了,一定是不能排序的。

另外,注意输出序列之后的那个逗号。。。。。。

Sorting It All Out

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 24   Accepted Submission(s) : 13
Problem Description
An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D implies that A < B, B < C and C < D. in this problem, we will give you a set of relations of the form A < B and ask you to determine whether a sorted order has been specified or not.
 
Input
Input consists of multiple problem instances. Each instance starts with a line containing two positive integers n and m. the first value indicated the number of objects to sort, where 2 <= n <= 26. The objects to be sorted will be the first n characters of the uppercase alphabet. The second value m indicates the number of relations of the form A < B which will be given in this problem instance. Next will be m lines, each containing one such relation consisting of three characters: an uppercase letter, the character "<" and a second uppercase letter. No letter will be outside the range of the first n letters of the alphabet. Values of n = m = 0 indicate end of input.
 
Output
For each problem instance, output consists of one line. This line should be one of the following three:

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.
 
Source
PKU
#include <iostream>
#include <cstring>
#include <stdio.h>
#define inf 0xfffffff
#define maxn 27 using namespace std; int n,m;
char les,first,second;
int mp[maxn][maxn],ans[maxn]; int topo()
{
int i,j,res=0;
int into[maxn]={0};
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(mp[i][j]>0)
into[j]++;
} }
for(i=0;i<n;i++)
{
int cnt=0,tmp=-1;
for(j=0;j<n;j++)
{
if(into[j]==0)
{
cnt++;
tmp=j;
}
}
if(cnt==0)
return 1;
if(cnt>1)
res=2;
ans[i]=tmp;
into[tmp]=-1;
for(j=0;j<n;j++)
{
if(mp[tmp][j]>0)
into[j]--;
}
}
if(res==2)
return 2;
else
return 3;
} int main()
{
// freopen("in.txt","r",stdin);
while(cin>>n>>m&&(n+m))
{
memset(mp,0,sizeof(mp));
memset(ans,0,sizeof(ans));
int i,tp=2,last=0,num=0;
for(i=0;i<m;i++)
{
cin>>first>>les>>second;
int a=first-'A',b=second-'A';
mp[a][b]=1;
if(tp==2)
{
tp=topo();
}
if(last==2&&(tp==1||tp==3))
{
num=i+1;
}
last=tp;
}
if(tp==3)
{
cout<<"Sorted sequence determined after "<<num<<" relations: ";
for(i=0;i<n;i++)
cout<<char(ans[i]+'A');
cout<<'.';
cout<<endl;
}
else if(tp==2)
cout<<"Sorted sequence cannot be determined."<<endl;
else if(tp==1)
cout<<"Inconsistency found after "<<num<<" relations."<<endl; }
return 0;
}

  

三部曲一(数据结构)-1011-Sorting It All Out的更多相关文章

  1. 三部曲一(数据结构)-1024-Eqs

    解方程整数解的题,通过这道题我学会了这种题的一般做法,对于未知数较少.取值范围较小且解为整数的方程,把一半的未知数移到等式的另一边,然后对两边分别枚举,用哈希配对,如果有相同的结果就找到一组解.具体做 ...

  2. 三部曲一(数据结构)-1022-Gold Balanced Lineup

    Gold Balanced Lineup Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Othe ...

  3. 三部曲一(数据结构)-1020-Ultra-QuickSort

    通过这道题我大体理解了树状数组的原理和用法,完全用的别人的算法,我把别人算法看懂之后有自己敲了一遍,不得不说这算法真是高深巧妙啊,我开始看都看不懂,还是在别人的讲解下才看懂的,我觉得有必要写个博客记录 ...

  4. 数据结构(线段树):HDU 5649 DZY Loves Sorting

    DZY Loves Sorting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Oth ...

  5. COJ 1011 WZJ的数据结构(十一)树上k大

    题解:主席树&DFS序. PS:为什么我一开始Wa了N发 是因为有一个左区间我写成[L,M+1]了.......................... #include<iostream ...

  6. Python数据结构应用5——排序(Sorting)

    在具体算法之前,首先来看一下排序算法衡量的标准: 比较:比较两个数的大小的次数所花费的时间. 交换:当发现某个数不在适当的位置时,将其交换到合适位置花费的时间. 冒泡排序(Bubble Sort) 这 ...

  7. 数据结构与算法 Big O 备忘录与现实

    不论今天的计算机技术变化,新技术的出现,所有都是来自数据结构与算法基础.我们需要温故而知新.        算法.架构.策略.机器学习之间的关系.在过往和技术人员交流时,很多人对算法和架构之间的关系感 ...

  8. 数据结构图文解析之:哈夫曼树与哈夫曼编码详解及C++模板实现

    0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...

  9. 用python语言讲解数据结构与算法

    写在前面的话:关于数据结构与算法讲解的书籍很多,但是用python语言去实现的不是很多,最近有幸看到一本这样的书籍,由Brad Miller and David Ranum编写的<Problem ...

随机推荐

  1. mysql 插入中文报错: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value...

    总结写在前面, 总结: 当Java通过jdbc链接mysql插入中文时,要保证程序可以正常执行,而且插入的中文不会乱码, mysql服务器端,对数据表(不是数据库)的编码设置,要保证是支持中文的,例如 ...

  2. LA 5135 井下矿工

    题目链接:http://vjudge.net/contest/141787#problem/B 白书P318 题目大意:有N个矿井 ,由一些隧道连接起来,现在要修建尽量少的安全通道,使得无论哪里发生事 ...

  3. linux mysql导入导出

    linux下导入.导出mysql数据库命令 一.导出数据库用mysqldump命令(注意mysql的安装路径,即此命令的路径):1.导出数据和表结构:mysqldump -u用户名 -p密码 数据库名 ...

  4. JavaScript高级应用(一)

    1.尺寸 //各种尺寸 s += "\r\n网页可见区域宽(document.body.clientWidth):"+ document.body.clientWidth; s + ...

  5. (转)建站知识:域名/ 空间/ IP/ 端口之间的关系

    先说域名解析吧,比如说你的域名是 www.sunnymould.com,这个域名对应着一个IP地址,域名解析就是把上面的域名转换成这个IP地址的过程,这样你就可以域名访问了上面地址上的内容了. 端口映 ...

  6. 【RabbitMQ】RabbitMQ在Windows的安装和简单的使用

    版本说明 使用当前版本:3.5.4 安装与启动 在官网上下载其Server二进制安装包,在Windows上的安装时简单的,与一般软件没什么区别. 安装前会提示你,还需要安装Erlang,并打开下载页面 ...

  7. maven环境快速搭建

    ----------------准备工作------------- Jdk  1.5以上java开发环境. Eclipse IDE 一个. Maven 3.0.3下载地址: http://maven. ...

  8. CSS 3 阴影,倒影,渐变

    盒子阴影 box-shadow:盒子的阴影 第一个参数:设置的是阴影的水平偏移量 第二个参数:设置的是阴影的垂直偏移量 第三个参数:设置阴影的模糊程度 第四个参数:设置阴影外延值 第五个参数:阴影的颜 ...

  9. BLAST - 序列数据库搜索

    我生信入门,老师就要求我学好blast比对,说得也确实是很有道理,是个人都知道比对是最基本的东西,现在再想想那老师的建议,也只能呵呵一笑. 北大生物信息公开课有一章专门讲得序列数据库搜索,可以好好看看 ...

  10. laravel 控制器里 redirect url地址传两个参数的方法

    public function red_search() { $url=Request::all(); return redirect('search/'.$url['category'].'/'.$ ...