Poj 1094 Sorting It All Out
2016-01-16 16:52
429 查看
Sorting It All Out
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
Sample Output
Source
East Central North America 2001
思路:简单的拓扑排序,但是要注意这里的排序是根据每一次的输入的,也就是每一个输入,如果能够得出成环 或者已经能够确定拓扑序列,那么接下来输入的东西应该忽略,直到最后输入结束还不能确定关系的话 答案就是 不能确定
AC代码如下:
总结:这道题目是很不错、很经典的拓扑排序的题目,增加对拓扑排序过程的了解,加油。
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 31190 | Accepted: 10839 |
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
East Central North America 2001
思路:简单的拓扑排序,但是要注意这里的排序是根据每一次的输入的,也就是每一个输入,如果能够得出成环 或者已经能够确定拓扑序列,那么接下来输入的东西应该忽略,直到最后输入结束还不能确定关系的话 答案就是 不能确定
AC代码如下:
#include <iostream> #include <cstring> #include <map> using namespace std; int n,m; const int maxn=30; int mm[maxn][maxn]; int dis[maxn]; int ans[maxn]; int tmp[maxn]; int toposort(){ int flag=1; for(int i=1;i<=n;i++){ // cout<<dis[i]<<" "; tmp[i]=dis[i]; } //cout<<endl; int sign=0; for(int i=0;i<n;i++){ int m=0,lol; for(int j=1;j<=n;j++){ if(tmp[j]==0){ lol=j,m++; } } // cout<<"m"<<m<<endl; if(m==0) return 0; if(m>1) flag=-1; ans[sign++]=lol; tmp[lol]--; for(int j=1;j<=n;j++) if(mm[lol][j]) tmp[j]--; } return flag; } int main(){ map<char,int> mp; for(int i=0;i<26;i++) mp[(char)(i+'A')]=i+1; while(cin>>n>>m &&n &&m){ memset(mm,0,sizeof(mm)); memset(dis,0,sizeof(dis)); int sign=0; for(int i=1;i<=m;i++){ char a,b,c; cin>>a>>b>>c; if(sign) continue; mm[mp[a]][mp[c]]=1; dis[mp[c]]++; int ttmp=toposort(); if(ttmp==0){ cout<<"Inconsistency found after "<<i<<" relations."<<endl; sign=1; } if(ttmp==1){ cout<<"Sorted sequence determined after "<<i<<" relations: "; for(int i=0;i<n;i++) cout<<(char)(ans[i]+'A'-1); cout<<"."<<endl; sign=1; } } if(!sign) cout<<"Sorted sequence cannot be determined."<<endl; } return 0; }
总结:这道题目是很不错、很经典的拓扑排序的题目,增加对拓扑排序过程的了解,加油。
相关文章推荐
- 基于J2EE技术的SSH框架的改进-笔记
- FLASH做出夜空中星星眨的效果
- OpenResty 点滴
- ubuntu常见错误--Could not get lock /var/lib/dpkg/lock解决
- gcc编译
- hbase RowFilter如何根据rowkey查询以及实例实现代码
- HBase + Phoenix 安装试用
- HP VC模块Shared uplink Sets配置参考
- Java之集合类应用总结
- 函数适配器
- modis数据下载汇总
- solaris 文件系统管理
- AJAX设为同步
- STL sort简单用来快速排序
- Android 开发绕不过的坑:你的 Bitmap 究竟占多大内存?
- [Leetcode]29. Divide Two Integers @python
- ubuntu下配置nginx支持phpt(unix:/var/run/php5-fpm.sock failed (2: No such file or directory) )
- Unix环境高级编程学习笔记(十一) 网络IPC:套接字
- android中AudioRecord使用
- hdu 5597 GTW likes function(欧拉函数)