每次加入一个关系都要进行拓扑排序,不过在排序过程中需要判断是否出现多个入度为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. zigbee学习之路(四):按键控制(中断方式)

    一.前言 通过上次的学习,我们学习了如何用按键控制led,但是在实际应用中,这种查询方式占用了cpu的时间,如果通过中断控制就可以解决这个问题,我们今天就来学习按键控制的中断方式. 二.原理分析 传统 ...

  2. Mayor's posters(线段树+离散化POJ2528)

    Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 51175 Accepted: 14820 Des ...

  3. prototype与原型链

    1.今天翻看 阮一峰老师的博客看到了,一篇讲javascript为什么要设计出prototype,跳转      大意就是new 的方式有缺陷,没有共同的属性,一下明白了很多. 在来一张原型链的图:

  4. NSISの堆栈操作

    一 .堆栈 堆栈是 NSIS 维护的一堆数据,你可以根据需要往堆栈中存入任意大小的数据(as big as you like),所以你可以向堆栈中推入或读取数据,堆栈只有一个,堆栈遵守 LIFO (后 ...

  5. 2016年12月22日 星期四 --出埃及记 Exodus 21:17

    2016年12月22日 星期四 --出埃及记 Exodus 21:17 "Anyone who curses his father or mother must be put to deat ...

  6. 使用ajax登录格式

    登录页面: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...

  7. iotop命令

    简介: iotop – simple top-like I/O monitor iotop是一个用来监视磁盘I/O使用状况的 top 类工具,可监测到哪一个程序使用的磁盘IO的信息(requires ...

  8. Python3基础 reverse 将列表倒序排列

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  9. Bootstrap_按钮工具栏

    单个按钮在Web页面中的运用有时候并不能满足我们的业务需求,常常会看到将多个按钮组合在一起使用,比如富文本编辑器里的一组小图标按钮等. Bootstrap框架为大家提供了按钮组组件. <div ...

  10. 下载SRA文件

    sratoolkit.2.6.2-centos_linux64/bin/prefetch  下载SRA文件 fastq-dump    --split-3    SRR2923014.sra    转 ...