poj 1094(拓扑排序)
来源:互联网 发布:aes256 加密java代码 编辑:程序博客网 时间:2024/05/20 04:08
Language:
Sorting It All Out
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 26943 Accepted: 9309
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.
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.
Source
East Central North America 2001
这是做的拓扑排序的第一题。
题意:输入n和m,n 表示给出的字符的个数,然后输入m行 字符之间的关系,每输入一行便要判断是不是有环,是不是有n个字符唯一的排列顺序。若全部输入完毕后没有唯一的排列顺序,也没有环便输出 Sorted sequence cannot be determined.
分析:每输入一行 字符间的关系便调用拓扑排序的函数判断,在拓扑排序中,先定义一个flag=1,如果有环,直接返回0;如果不唯一,则改变flag的值;有唯一情况时,保存入度为0的字符。循环完毕后,返回flag的值。
在主函数中如果调用的函数返回1,那么表示有唯一排序,输出字符串;如果返回0,表示有环,停止循环。如果已输入m行,判断结果返回-1,那么输出 Sorted sequence cannot be determined.
#include <stdio.h>#include <string.h>int map[30][30];//记录图int ans[30];//记录结果int into[30];//记录入度int into1[30];//同上int n,m;int topu(){int i;int k;int flag=1;for(int j=1;j<=n;j++)//将入度保存到另一个数组中 ,确保into数组不变; { into1[j]=into[j];}for(i=1;i<=n;i++){int count=0;for(int j=1;j<=n;j++)//找是否存在入度为0的点 {if(into1[j]==0){ count++; k=j;}}if(count>1)flag= -1;//入度为0的点不唯一,所以顺序也不唯一 if(count==0)return 0;//不存在入度为0的点,有环; ans[i]=k; into1[k]=-1; for(int j=1;j<=n;j++)//将从这个字符出发的到达其他的节点的入度减1; { if(map[k][j]==1) into1[j]--; }}return flag;}int main(){while(scanf("%d%d",&n,&m),m,n){memset(map,0,sizeof(map));//清空数组 memset(ans,0,sizeof(ans));memset(into,0,sizeof(into));char a[5];int i;int flag=0; for(i=1;i<=m;i++){scanf("%s",&a);if(flag)continue; //已经输出过了,所以continue int x=a[0]-'A'+1;int y=a[2]-'A'+1;if(!map[x][y])//消除重边 {map[x][y]=1;into[y]++;}int ans1=topu();//调用拓扑排序 if(ans1==0){printf("Inconsistency found after %d relations.\n",i);flag=1;}if(ans1==1){printf("Sorted sequence determined after %d relations: ",i);for(int j=1;j<=n;j++)printf("%c",ans[j]+'A'-1);printf(".\n");flag=1;}}if(!flag)//表示没有输出过 printf("Sorted sequence cannot be determined.\n");}return 0;}
0 0
- poj 1094(拓扑排序)
- POJ 1094(拓扑排序)
- poj 1094(拓扑排序)
- poj 1094(拓扑排序)
- POJ 1094(拓扑排序)
- POJ 1094(拓扑排序)
- 拓扑排序,poj 1094
- 拓扑排序,poj 1094
- POJ 1094 拓扑排序
- poj-1094拓扑排序
- poj 1094 拓扑排序
- poj 1094 拓扑排序
- POJ 1094(拓扑排序)
- POJ 1094(拓扑排序)
- poj 1094 拓扑排序
- poj 1094 (拓扑排序)
- POJ 1094 拓扑排序
- POJ 1094 拓扑排序
- http://wx.233.com/share/Album/1234351
- 关于win8下搭建android开发环境的问题
- MATLAB 小知识 第二篇 数据类型
- 【数学】博弈论
- 算法随笔
- poj 1094(拓扑排序)
- 黑马程序员——Java中的动态代理
- 把梳子卖给和尚
- 地图基础术语学习
- 【歌词】さくらの季節 中恵光城 找了好久的(→_→)
- ghfhhj更丰富和分红计划经济困境
- 第六章 挖掘频繁模式、关联和相关性:基本概念和方法
- HDOJ 题目2057 A + B Again(水题)
- [copy]oracle数据库的服务都是什么意思