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 ...
随机推荐
- 【iOS】网页中调用JS与JS注入
非常多应用为了节约成本,做出同一时候在Android与iOS上都能使用的界面,这时就要使用WebView来做.Android和IOS上都有WebView,做起来非常省事.当然这时就要考虑怎样在Andr ...
- 再淡spring jdbc 连接池断开重连设置
先看一段错误日志: ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConne ...
- Swift - 单例模式的实现
过去Swift要实现单例,无非是这三种方式:全局变量,内部变量和dispatch_once方式.但都略显繁琐. 后来从1.2版本起,Swift中添加了如 static let 和 static var ...
- 08-使用for循环输出杨辉三角(循环)
/** * 使用循环输出杨辉三角 * * */ public class Test6 { public static void main(String[] args) { // 创建二维数组 int ...
- java-创建线程的两种方式
1. 继承Thread类 定义类继承Thread类. 覆盖run方法. 实例化子类对象,调用start()方法,从而调用run方法. 2.实现Runnable接口 定义类实现Runnable接口. 覆 ...
- Java Thread 那些事
这篇文章被压在草稿箱许久,最近公司内部的技术社区有同学贴出了几篇分享 Java线程的文章,发觉有很多概念没有讲清楚,所以花点时间继续撰写,便有了这篇博文. 本文只聚焦 JVM 层面的线程模型,不考虑和 ...
- Windows Azure入门教学系列 (九):Windows Azure 诊断功能
本文是Windows Azure入门教学的第九篇文章. 本文将会介绍如何使用Windows Azure 诊断功能.跟部署在本地服务器上的程序不同,当我们的程序发布到云端之后,我们不能使用通常的调试方法 ...
- Linux的grep命令详解
简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它 ...
- JavaScript 进阶(四)解密闭包closure
闭包(closure)是什么东西 我面试前端基本都会问一个问题"请描述一下闭包".相当多的应聘者的反应都是断断续续的词,“子函数”“父函数”“变量”,支支吾吾的说不清楚.我提示说如 ...
- 反射API
反射,是指一种能在运行时动态加载.分析类的能力.反射被广泛地用于那些需要在运行时检测或修改程序行为的程序中.这是一个相对高级的特性,使用反射技术应当具备相当的Java语言基础.我们可以通过反射机制让应 ...