POJ 1094 Sorting It All Out(拓扑排序)
来源:互联网 发布:linux ftp更改下载目录 编辑:程序博客网 时间:2024/06/10 14:24
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 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.
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.
4 6A<BA<CB<CC<DB<DA<B3 2A<BB<A26 1A<Z0 0
Sorted sequence determined after 4 relations: ABCD.Inconsistency found after 2 relations.Sorted sequence cannot be determined.
该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列。是典型的拓扑排序,但输出格式上确有三种形式:
1.该字母序列有序,并依次输出;
2.该序列不能判断是否有序;
3.该序列字母次序之间有矛盾,即有环存在。
而这三种形式的判断是有顺序的:先判断是否有环(3),再判断是否有序(1),最后才能判断是否能得出结果(2)。注意:对于(2)必须遍历完整个图,而(1)和(3)一旦得出结果,对后面的输入就不用做处理了。
#include<stdio.h>#include<string.h>int map[27][27],indegree[27],q[27]; //map代表有向图,map[i][j]=1代表字母i<j。//indegree[i]代表结点i的入度,即直接小于i的个数int TopoSort(int n) //拓扑排序,n指共有多少字母如abcd=4,abccd=4{ int c=0,temp[27],loc,m,flag=1,i,j; ////flag=1:有序 flag=-1:不确定 //m:入度为零的顶点个数 for(i=1;i<=n;i++) temp[i]=indegree[i];//temp=indegree=入度数,是为了在后面的主函数的循环中不改变indegree的值 for(i=1;i<=n;i++) { m=0; for(j=1;j<=n;j++) if(temp[j]==0) { m++; loc=j; } //查找入度为零的顶点个数,设置为m if(m==0) return 0; //有环 if(m>1) flag=-1; // 无序,m大于1,则出现一个以上没有入度的,证明无序 q[c++]=loc; //入度为零的点入队 temp[loc]=-1; //标记,将入度为0的点删除,并将两个出度取消 for(j=1;j<=n;j++) if(map[loc][j]==1) temp[j]--; //如果j含有入度,则入度-- } return flag;}int main(){ int m,n,i,sign; //当sign=1时,已得出结果 char str[5]; while(scanf("%d%d",&n,&m)) //n代表字母个数,m代表要输入的关系的行数 { if(m==0&&n==0) break;//都等于0时,结束输入 memset(map,0,sizeof(map)); memset(indegree,0,sizeof(indegree)); sign=0; for(i=1;i<=m;i++) { scanf("%s",str); //str[0]=字母,str[1]=空格,str[2]=字母 if(sign) continue; //一旦得出结果,对后续的输入不做处理 int x=str[0]-'A'+1; int y=str[2]-'A'+1; map[x][y]=1; indegree[y]++; //y的入度+1 int s=TopoSort(n); if(s==0) //有环,即x<y && y<x的情况 { printf("Inconsistency found after %d relations.\n",i);//表示在输入了第几个之后出的结果,后面的就不作处理了。 sign=1; } if(s==1) //有序,正常情况 { printf("Sorted sequence determined after %d relations: ",i); for(int j=0;j<n;j++) printf("%c",q[j]+'A'-1); printf(".\n"); sign=1; } } if(!sign) //不确定 printf("Sorted sequence cannot be determined.\n"); } return 0;}
阅读全文
0 0
- POJ 1094 Sorting It All Out(拓扑排序)
- Poj 1094-Sorting It All Out [拓扑排序]
- POJ 1094-Sorting It All Out 拓扑排序
- zoj 1060 || 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 拓扑排序+Floyd算法
- POJ 1094 Sorting It All Out (拓扑排序) - from lanshui_Yang
- gdb 之 mac下调试
- iOS解决键盘遮挡输入框问题
- 变量更新和控制依赖
- windows+tensorflow+fasterRcnn---1
- 模仿tmall商城网站
- POJ 1094 Sorting It All Out(拓扑排序)
- JavaScript bind()方法介绍
- ----時區----
- python爬虫进阶(五):数据的存储
- LightOJ
- java 使用ObjectMapper的方式实现Json和bean的转换
- JS延迟处理函数setInterval
- 使用insertRule动态管理样式
- Activemq 常见的一些问题 心得