主题链接:

啊哈哈,选我

题目:

Sorting It All Out
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 26897   Accepted: 9281

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

这个题目对拓扑排序考虑的很细致。。

考虑了成环的情况。成环的情况也就是最后存在入度不为0的点。。

则计数后最后的num不等于n。。

这个题目还有就是这个题目不是对全部的信息进行综合推断,而是依据前面的假设可以得到已经成环了,或者可以得到n的大小顺序了,则后面的就不用推断了。。。所以用ok1,ok2两个变量进行控制。。

代码为:

#include<cstdio>
#include<stack>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=26+10;
int n,m;
int in[maxn],Copy[maxn],map[maxn][maxn],temp[maxn];
stack<int>S;
int topo()
{
int flag=0,num=0;
while(!S.empty()) S.pop();
memcpy(Copy,in,sizeof(in));
for(int i=0;i<n;i++)
{
if(Copy[i]==0)
S.push(i);
}
while(!S.empty())
{
if(S.size()>1)
flag=1;
int Gery=S.top();
S.pop();
temp[num++]=Gery;
for(int i=0;i<n;i++)
{
if(map[Gery][i])
{
if(--Copy[i]==0)
S.push(i);
}
}
}
if(num!=n)
return 0;//成环。则已经能够确定关系了,能够标记。
if(flag)
return 1;//有多个入度为0的点,则还不确定。继续输入信息。添加条件,看能否够得到顺序。
return 2;//顺序已经得到确定。能够标记。 } int main()
{
char str[maxn];
int ok1,ok2,u,v,i,is_n;
while(~scanf("%d%d",&n,&m),n,m)
{
is_n=0;
memset(in,0,sizeof(in));
memset(map,0,sizeof(map));
ok1=ok2=0;
for(i=1;i<=m;i++)
{
scanf("%s",str);
if(!ok1&&!ok2)
{
u=str[0]-'A';
v=str[2]-'A';
if(map[u][v]==0)
{
map[u][v]=1;
in[v]++;
}
int ans=topo();
if(ans==0)
{
is_n=i;
ok2=1;
}
else if(ans==2)
{
is_n=i;
ok1=1;
}
}
}
if(ok1)
{
printf("Sorted sequence determined after %d relations: ",is_n);
for(int i=0;i<n-1;i++)
printf("%c",temp[i]+'A');
printf("%c.\n",temp[n-1]+'A');
}
if(ok2)
printf("Inconsistency found after %d relations.\n",is_n);
if(ok1==0&&ok2==0)
printf("Sorted sequence cannot be determined.\n");
}
return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

poj1094Sorting It All Out的更多相关文章

  1. poj1094-Sorting It All Out-拓扑排序

    题意: 1).给你一些大写字母,共n个:大写字母间有m条关系: 2).举例:关系:A<B,意思就是A要排在B的前面(也就是说可能B排在A的前面 3).输出:有三种情况: 1.n个字母在前 i 条 ...

  2. POJ--1094--Sorting It All Out||NYOJ--349--Sorting It All Out(拓扑排序)

    NYOJ的数据水一点,POJ过了是真的过了 /* 拓扑排序模板题: 每次输入都要判断有环与有序的情况,如果存在环路或者已经有序可以输出则跳过下面的输入 判断有序,通过是否在一个以上的入度为0的点,存在 ...

  3. poj1094Sorting It All Out——拓扑排序

    题目:http://poj.org/problem?id=1094 看到此题,首先觉得这是一种层层递进的关系,所以可以想到用拓扑排序: 就像人工排序,每次需要找到一个最小的,再找到新的最小的……所以用 ...

  4. poj1094Sorting It All Out 拓扑排序

    做拓扑排序的题目,首先要知道两条定理: 1.最后得到的拓扑数组的元素个数如果小于n,则不存在拓扑序列.  (有圈) 2.如果一次入队的入度为零的点数大于1,则拓扑序列不唯一. (关系不确定) 本题有一 ...

随机推荐

  1. quartz 2.1学习(一)

    quartz是一种开源任务调度框架,提供了强大的任务调度机制,Quartz允许开发人员灵活地定义触发器的调度时间表,并可对触发器和任务进行关联映射.废话不多说了,介绍一下编程的基本步骤: 实现Job接 ...

  2. [置顶] PMBOOK第四版-ITO与数据流图总结

    具体文档下载地址: 点击打开文档下载地址 :http://download.csdn.net/detail/lyjluandy/6694205 一.过程组与知识领域表(简图) 二.输入 - 工具 - ...

  3. 【ASP.NET Web API教程】2.3.5 用Knockout.js创建动态UI

    原文:[ASP.NET Web API教程]2.3.5 用Knockout.js创建动态UI 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的内容 ...

  4. Android Sip学习(三)Android Voip实现

    Android Sip学习(三)Android Voip实现   Android Sip学习(准备知识)SIP 协议完整的呼叫流程 Android Sip学习(一)Android 2.3 APIs S ...

  5. android 多媒体数据库详解

    主要分为几节: 1. Android的媒体文件内部是如何存储的? 2. Andoid的媒体文件如何获取? 3. 在使用媒体文件的一些小技巧. 1. Android的多媒体如何存储的? Android的 ...

  6. centos 安装ganglia监控工具

    一个.ganglia基本介绍 ganglia它是一个分布式监控系统,那里有两个Daemon,每间:clientGangliaMonitoring Daemon (gmond)和服务端GangliaMe ...

  7. TsFltMgr.sys其原因是,该系统蓝屏QQ计算机管理器!

    同事一WindowsXP系统,正常执行,关闭后,第二天无法启动.详细症状为: (1)安全模式以及带网络功能的安全模式都能够进入. (2)正常模式.还没出现WindowXP滚动栏就開始重新启动: (3) ...

  8. ExtJs4 笔记(3) Ext.Ajax 对ajax的支持

    本篇主要介绍一下ExtJs常用的几个对JS语法的扩展支持,包括Ajax封装,函数事件操作封装,还有扩展的常用函数等.Ajax服务端交互式操作是提交到.NET MVC.后续服务端交互都采用这一方式实现. ...

  9. android面试题 不仅仅是面试是一个很好的学习

    下面的问题是在网上找到的总结,感谢您分享!希望,我们的共同进步,找到自己心仪的公司,: 1.android dvm 流程和Linux这个过程.无论是应用程序对同一概念: 答案:dvm是dalivk虚拟 ...

  10. iotop 分析系统那些进程占用io资源

    iotop -b -o  -t  -qqq >> /tmp/iotop.log 1.直接yum安装,rh6的光盘里有包. yum install iotop   2.命令参数介绍   -o ...