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 ...
随机推荐
- android获取View上某点的颜色
//根据坐标获取 ImageView imageView = ((ImageView)v); Bitmap bitmap = ((BitmapDrawable)imageView.getDrawabl ...
- PE文件简单介绍
PE(Portable Execute)文件是WIN32下可运行文件遵循的数据格式,也是反汇编调试不可缺少的文件,常见的pe文件有.exe和.dll文件.本文主要介绍pe文件的结构和虚拟内存地址转换到 ...
- C# - 接口的继承
代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...
- linux 下vi中关于删除某段,某行,或者全部删除的命令
1,先打开某个文件: vi filename 2,转到文件结尾 在命令模式输入 G 转到10行 在命令模式输入 10G 4,删除所有内容:先用G 转到文件尾,然后使用下面命令: :1, ...
- CCIE路由实验(9) -- IPv6
1.IPv6地址的各种情况2.配置通过DHCP-PD方式分配前缀信息3.IPv6路由基本配置4.IPv6路由--RIPng5.IPv6路由--EIGRPv66.IPv6路由--OSPFv37.IPv6 ...
- 我的mysql数据库sql优化原则
原文 我的mysql数据库sql优化原则 一.前提 这里的原则 只是针对mysql数据库,其他的数据库 某些是殊途同归,某些还是存在差异.我总结的也是mysql普遍的规则,对于某些特殊情况得特殊对待. ...
- C++经典书目索引及资源下载
C++经典书目索引: 严重申明 : 本博文未经原作者(jerryjiang)同意,不论什么人不得转载和抄袭 ! Essential C++ 中文版 层次:0基础 导读:<Essential C+ ...
- python 获取当前日期 星期
from datetime import datetime d =datetime.today() #获取当前日期时间 d.isoweekday() #获取时间周几
- 6月19日 NSFileHandle文件类的常用方法
NSFileManager类主要对文件的操作(删除,修改,移动,复制等): NSFileHandle类主要对文件内容进行读取和写入操作 NSFileHandle处理文件的步骤 ...
- 【ASP.NET Web API教程】2.3.4 创建Admin视图
原文:[ASP.NET Web API教程]2.3.4 创建Admin视图 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的内容. Part 4: ...