poj 1094
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 26911 | Accepted: 9285 |
Description
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
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
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
AC代码:
#include<iostream>
#include<vector>
#include<stack>
#include<algorithm>
#include<cstring>
#include<stdio.h>
using namespace std;
int n,m;
int in[30],tmp_in[30];
int f1,f2;
int str[30];
vector <int> G[30];
void top_sort(){ //拓扑排序
f1=0; //没有环
f2=1; //唯一排序
for(int i=0;i<n;i++)
tmp_in[i]=in[i];
stack <int> s;
while(!s.empty())
s.pop();
for(int i=0;i<n;i++)
if(tmp_in[i]==0)
s.push(i);
int counter=0;
while(!s.empty()){
if(s.size()>=2){
f2=0;
}
int tmp=s.top();
s.pop();
str[counter++]=tmp;
for(int i=0;i<G[tmp].size();i++){
tmp_in[G[tmp][i]]--;
if(!tmp_in[G[tmp][i]])
s.push(G[tmp][i]);
}
}
if(counter<n)
f1=1;
}
int main(){
while(cin>>n>>m&&(n!=0||m!=0)){
memset(in,0,sizeof(in));
int i;
for(i=0;i<n;i++)
if(!G[i].empty())
G[i].clear(); for(i=0;i<m;i++){
char ch[5]; cin>>ch;
G[ch[0]-'A'].push_back(ch[2]-'A');
in[ch[2]-'A']++;
top_sort();
if(f1){
cout<<"Inconsistency found after "<<i+1<<" relations."<<endl;
for(int j=i+1;j<m;j++) //注意这里要输入剩下的信息才干退出
cin>>ch;
break;
}
else if(f2){
cout<<"Sorted sequence determined after "<<i+1<<" relations: ";
for(int j=0;j<n;j++)
cout<<char (str[j]+'A');
cout<<'.'<<endl;
for(int j=i+1;j<m;j++) //注意这里要输入剩下的信息才干退出
cin>>ch;
break;
}
}
if(i>=m)
cout<<"Sorted sequence cannot be determined."<<endl;
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
poj 1094的更多相关文章
- ACM: poj 1094 Sorting It All Out - 拓扑排序
poj 1094 Sorting It All Out Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & ...
- POJ 1094 (传递闭包 + 拓扑排序)
题目链接: POJ 1094 题目大意:有 1 ~ N 个大写字母,且从 A 开始依次 N 个.再给你 M 个小于的关系,比如 A < B ,让你判断三种可能: 1.在第 i 个关系罗列之后,是 ...
- poj 1094(拓扑排序)
http://poj.org/problem?id=1094 题意:给你m个字母,有n个判断语句.求在哪个语句就可以判断出这个是不是一个环,或者在哪个语句可以判断出这些字母的排序规则,或者就是不能确定 ...
- POJ 1094 Sorting It All Out 拓扑排序 难度:0
http://poj.org/problem?id=1094 #include <cstdio> #include <cstring> #include <vector& ...
- POJ 1094 (TopoSort)
http://poj.org/problem?id=1094 题意:该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列.是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序 ...
- poj 1094 Sorting It All Out (拓扑排序)
http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- poj 1094 Sorting It All Out(图论)
http://poj.org/problem?id=1094 这一题,看了个大牛的解题报告,思路变得非常的清晰: 1,先利用floyd_warshall算法求出图的传递闭包 2,再判断是不是存在唯一的 ...
- poj 1094 Sorting It All Out 解题报告
题目链接:http://poj.org/problem?id=1094 题目意思:给出 n 个待排序的字母 和 m 种关系,问需要读到第 几 行可以确定这些字母的排列顺序或者有矛盾的地方,又或者虽然具 ...
- POJ 1094 Sorting It All Out【拓扑排序】
题目链接: http://poj.org/problem?id=1094 题意: 给定前n个字母的大小关系,问你是否 根据前xxx个关系得到上升序列 所有关系都无法确定唯一的一个序列 第xxx个关系导 ...
- 拓扑排序 +Floyd(poj 1094)
题目:Sorting It All Out 题意:字母表前n个字母,有m组他们中的大小关系,判断n个字母是否构成唯一序列: 1.Sorted sequence determined after xxx ...
随机推荐
- UVA 11292 - The Dragon of Loowater (water)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=sh ...
- HDU5090模拟,hash
/* HDU 5090 算是一道简单模拟题.但当中有非常深的hash思想 这是本人的第一道hash题 更是本人的第一道纸质代码不带编译不带执行提交AC的题 值得纪念 废话讲这么多之后,讲述题中思想 因 ...
- 读书与写论文的引导书——leo鉴书60
我是专科直接考的研究生.在论文写作方面基本能够算是初级.MBA毕业那会儿要写论文,在网上找了不少这方面的书,<论文与治学>是当中之中的一个. 这本那时为应景儿卖的书,成了我之后学习与工作的 ...
- 矩形、占位符组件——axure线框图部件库介绍
矩形组件和占位符没有太多的区别,这里我们主要讲解矩形组件的操作和使用,占位符的操作各位可以按照矩形的操作方法进行练习一下. 矩形组件是一个矩形,它可以用来做很多的工作,比如页面上需要一块蓝色的背景,就 ...
- Linux 安装Redis全过程日志
wget http://download.redis.io/redis-stable.tar.gz tar xvzf redis-stable.tar.gz cd redis-stable make ...
- awakeFromNib小总结
awakeFromNib 在使用IB的时候才会涉及到此方法的使用,当.nib文件被载入的时候,会发送一个awakeFromNib的消息到.nib文件里的每一个对象,每一个对象都能够定义自己的awake ...
- Matlab图像处理系列2———空间域平滑滤波器
注:本系列来自于图像处理课程实验,用Matlab实现最主要的图像处理算法 本文章是Matlab图像处理系列的第二篇文章.介绍了空间域图像处理最主要的概念----模版和滤波器,给出了均值滤波起和中值滤波 ...
- 碰撞回避算法(一) Velocity Obstacle
碰撞回避是机器人导航,游戏AI等领域的基础课题.几十年来,有很多算法被提出.注意这里主要指的是局部的碰撞回避算法.尽管和全局的路径规划算法(A*算法等)有千丝万缕的联系.可是还是有所不同的(局部的碰撞 ...
- 浅谈C#中的泛型
1.什么是泛型? 泛型是程序设计语言的一种特性.允许程序员在强类型程序设计语言中编写 代码时定义一些可变部分,那些部分在使用前必须作出指明.各种程序设计语言和其编译器.运行环境对泛型的支持均不一样.将 ...
- 使用JDBC获取能自动增加的主键
本篇讲述如何使用JDBC获取能自动增加的主键的值.有时候我们在向数据库插入数据时希望能返回主键的值,而不是通过查询的方式.一般来说,在多表相互关联主键约束,也就是说别的表的外键约束是该表的主键,那么在 ...