POJ 1094 Sorting It All Out(拓扑排序)
来源:互联网 发布:mysql多主键 编辑:程序博客网 时间:2024/05/16 11:42
Sorting It All Out
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 30159 Accepted: 10430
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.
每次输入都要进行一次拓扑排序,有环直接输出Inconsistency found after i relations.(i为输入的次数)
如果排序完成 输出在第几次结束 并输出这个排序结果
两者都没有 最后输出 Sorted sequence cannot be determined.
#include <iostream>#include <stdlib.h>#include <cstdio>#include <cstring>#include <algorithm>#define N 30using namespace std;int a[N][N],v[N],ans[N];int n,m;int Solve(){ int ss=1; int b[N]; for(int i=0;i<n;i++) b[i]=v[i]; for(int i=0; i<n; i++) { int sum=0; for(int j=0;j<n;j++) { if(b[j]==0) sum++; } if(sum==0) ss=0; //没有入度为0的点 说明有环 if(sum>1) ss=-1; //入度为0的点不止一个 无法判定顺序 for(int j=0; j<n; j++) { if(b[j]==0) { ans[i]=j; b[j]--; for(int k=0; k<n; k++) { if(a[j][k]) { b[k]--; } } break; } } } return ss;}int main(){ while(scanf("%d%d",&n,&m) && (m ||n)) { memset(a,0,sizeof(a)); memset(v,0,sizeof(v)); bool flag = true; for(int i=1; i<=m; i++) { char x,y,z; cin>>x>>z>>y; if(a[x-'A'][y-'A']==0) { a[x-'A'][y-'A']=1; v[y-'A']++; } if(flag) { int ss=Solve(); if(ss==0) { flag=false; printf("Inconsistency found after %d relations.\n",i); } if(ss==1) { flag=false; printf("Sorted sequence determined after %d relations: ",i); for(int j=0; j<n-1; j++) printf("%C",ans[j]+'A'); printf("%C.\n",ans[n-1]+'A'); } } } if(flag) 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
- 在一个字符串中找空格的个数,字母的个数,数字的个数,和其他字符的个数
- MySQL优化三之order by
- Android-- 高仿QQ5.2双向侧滑菜单 使用DrawerLayout
- 黑马程序员-网络编程(一)
- boost 字符串分割 split, split_regex
- POJ 1094 Sorting It All Out(拓扑排序)
- scp不用输入密码的拷贝
- 轻松让INPUT框只能输入整数(转载)
- start
- Guava学习笔记:Guava cache
- python_学习笔记0810
- hdu 2203 亲和串
- codeforces 527D D. Clique Problem(二分+线段树+贪心+dp)
- Android 的事件传递机制,详细解释