Poj/OpenJudge 1094 Sorting It All Out
2014-02-13 15:47
507 查看
1.链接地址:
http://poj.org/problem?id=1094 http://bailian.openjudge.cn/practice/1094
2.题目:
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
3.思路:
4.代码:
http://poj.org/problem?id=1094 http://bailian.openjudge.cn/practice/1094
2.题目:
Sorting It All Out
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 25547 | Accepted: 8861 |
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
3.思路:
4.代码:
#include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> using namespace std; int n,m; int map[30][30]; int reg[100]; int in[30],out[30]; char ans[30]; int stack[30]; void fun() { int i,j; for(i='A',j=1;i<='Z';i++,j++)reg[i]=j; } void toposort(char *ans) { int i,top=0,u,s=0; for(i=1;i<=n;i++) if(in[i]==0)stack[top++]=i; while(top!=0) { u=stack[--top]; ans[s++]=u+64; for(i=1;i<=n;i++) { if(map[u][i]) { in[i]--; if(!in[i])stack[top++]=i; } } } ans[s]=0; } int main() { int i,j,x,y,k,flag1,flag2,flag; fun(); char ch[5]; while(1) { flag1=flag2=0; memset(map,0,sizeof(map)); scanf("%d%d",&n,&m); if(n==0&&m==0)break; for(i=1;i<=m;i++) { flag=1; scanf("%s",ch); x=reg[ch[0]]; y=reg[ch[2]]; map[x][y]=1; if(x==y)flag1=i; memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); if(!flag1&&!flag2) for(j=1;j<=n;j++) for(k=1;k<=n;k++) { if(j!=x&&k!=y)map[j][k]=map[j][k]||(map[j][x]&&map[y][k]); if(j==x&&k!=y)map[j][k]=map[j][k]||map[y][k]; if(j!=x&&k==y)map[j][k]=map[j][k]||map[j][x]; if(map[j][k]) { out[j]++; in[k]++; } } j=1; if(!flag1) for(j=1;j<=n;j++) { if(map[j][j])flag1=i; if(in[j]+out[j]!=n-1)flag=0; } if(flag&&!flag2&&j>n){flag2=i;toposort(ans);} } if(flag2) { printf("Sorted sequence determined after %d relations: %s.\n",flag2,ans); continue; } if(flag1) { printf("Inconsistency found after %d relations.\n",flag1); continue; } printf("Sorted sequence cannot be determined.\n"); } return 0; }
相关文章推荐
- [POJ](1094)Sorting It All Out ---拓扑排序(图)
- 拓扑排序 POJ 1094 Sorting It All Out
- poj_1094 Sorting It All Out(拓扑排序 + floyd)
- POJ 1094 && ZOJ 1060 Sorting It All Out 【拓扑排序入门】
- 【POJ 1094】Sorting It All Out 【topo排序 的三种情况的特判】
- POJ 1094: Sorting It All Out( 拓扑排序 )
- POJ1094 Sorting It All Out 拓扑排序
- POJ 1094 Sorting It All Out (拓扑排序)
- POJ 1094 Sorting It All Out(经典拓扑,唯一排序)
- poj1094——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(拓扑排序 + 邻接表)
- poj1094 Sorting It All Out(拓扑排序)
- poj 1094 Sorting It All Out
- POJ1094-Sorting It All Out
- poj 1094 Sorting It All Out (拓扑排序)
- poj 1094 Sorting It All Out (拓扑排序)
- POJ-1094 Sorting It All Out 解题报告