POJ 1094 Sorting It All Out
来源:互联网 发布:淘宝卖家发货地址规则 编辑:程序博客网 时间:2024/05/17 04:29
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.
题目大意
给出由n个变量(2<=n<=26,变量名为一个大写字母,不同的变量字母不同,保证变量名是从‘A’开始依次选取的n个)组成的m个不等式(形如A<B),要求判断这一系列不等式能不能确定所有变量的大小关系;若能,输出在第几个不等式时可以得出结论,并输出最终排好序的变量名;若有不确定的大小关系,输出“Sorted sequence cannot be determined.”,若不能,输出在第几个不等式时冲突。
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.
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 6A<BA<CB<CC<DB<DA<B3 2A<BB<A26 1A<Z0 0
Sample Output
Sorted sequence determined after 4 relations: ABCD.Inconsistency found after 2 relations.Sorted sequence cannot be determined.
题解
思想是:依次加入每一个条件,用拓扑排序检验是否有环即可。只是需要在拓扑排序时注意一些特殊的情况……否则wa得飞起……
#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<cmath>#include<algorithm>using namespace std;int n,m,in[30],pd[30],map[30][30];int ans[30],top;char a[10];int toposort(){int i,t[30],ct,j,tag=1,loc;top=0;for(i=1;i<=n;i++) t[i]=in[i];for(i=1;i<=n;i++) {ct=0; for(j=1;j<=n;j++) {if(t[j]==0) {ct++; loc=j;} }if(ct==0) return 0;if(ct>1) tag=-1;ans[++top]=loc;t[loc]=-1;for(j=1;j<=n;j++) {if(map[loc][j]==1) t[j]--;} }return tag;}void work(){int i,j,x,y,s,tag=0;memset(map,0,sizeof(map));memset(in,0,sizeof(in));for(i=1;i<=m;i++) {scanf("%s",a); if(tag) continue; x=a[0]-'A'+1; y=a[2]-'A'+1; map[x][y]=1; in[y]++; s=toposort(); if(s==0) {printf("Inconsistency found after %d relations.\n",i); tag=1; }else if(s==1) {printf("Sorted sequence determined after %d relations: ",i); for(j=1;j<=top;j++) printf("%c",ans[j]-1+'A'); puts("."); tag=1; } }if(!tag) puts("Sorted sequence cannot be determined.");}int main(){while(scanf("%d%d",&n,&m)&&n!=0&&m!=0) work();return 0;}
0 0
- poj 1094 Sorting It All Out
- poj 1094 Sorting It All Out
- poj 1094 Sorting It All Out
- [POJ] 1094 Sorting It All Out
- POJ-1094 Sorting It All Out
- POJ-1094-Sorting It All Out
- POJ 1094 Sorting It All Out
- POJ 1094 Sorting It All Out
- POJ 1094 Sorting It All Out
- poj-1094-Sorting It All Out
- POJ 1094 Sorting It All Out
- poj 1094 Sorting It All Out
- POJ 1094 Sorting It All Out
- POJ-1094-Sorting It All Out
- poj 1094 Sorting It All Out
- poj 1094 Sorting It All Out(图论)
- poj 1094 Sorting It All Out
- POJ 1094 Sorting It All Out
- iBatis 中 Like '%iBatis%' 的写法实现模糊查询
- Django学习——setting.py常用配置
- Cocos2d-x 3.x的Windows Phone 8工程
- SQLite 事务处理、锁及冲突解决
- linux自定义命令
- POJ 1094 Sorting It All Out
- Android Screen Monitor同步手机屏幕到PC
- notepad++更改字体大小和颜色
- FatFS文件系统详解-附移植建议
- 代码度量工具——SourceMonitor的学习和使用
- 怎么把pdf转换成word文字
- PANDA: Pose Aligned Networks for Deep Attribute Modeling
- HDU 2047 阿牛的EOF牛肉串(第一次写博客)
- Spring MVC学习:配置简解