Sorting It All Out 拓扑排序&&判断是否存在环,是否关系包含所有的点
2013-10-26 17:30
381 查看
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.
**************************************************************************************************************************经典的拓扑排序
***************************************************************************************************************************
View Code
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.
**************************************************************************************************************************经典的拓扑排序
***************************************************************************************************************************
#include<iostream> #include<string> #include<cstring> #include<cstdio> #include<queue> using namespace std; int a[26][26]; queue<char>Q1;//记录两者之间的关系 int de[26],de2[26]; int relations; bool flag; int kind,it,jt,kt,n,m; void carry(int n,int ret) { int i,j,sum=0; int num; bool sign=true; queue<int>Q; for(i=0;i<n;i++) { if(de[i]==0) Q.push(i); } while(!Q.empty()) { num=0; for(i=0;i<n;i++) { if(de[i]==0) num++; } if(num>1) sign=false;//存在不确定关系 i=Q.front(); Q.pop(); Q1.push(i+'A');// sum++; de[i]=-1; for(j=0;j<n;j++) { if(a[i][j]==true) { de[j]--; if(de[j]==0) Q.push(j); } } } //如果存在确定关系,记录位置,不再往下算 if(sum==n&&sign) { flag=true; kind=1; relations=ret; } else for(i=0;i<n;i++) if(de[i]>0)//如果存在环,不再往下算 { flag=true; kind=2; relations=ret; break; } if(!flag)//如果都不满足,恢复 { for(i=0;i<n;i++) de[i]=de2[i]; } return; } int main() { char left,mid,right; int ni,nj; while(scanf("%d%d",&n,&m)) { getchar(); flag=false; kind=0; relations=0; if(n==0&&m==0) break; for(it=0;it<n;it++)//初始化 { for(jt=0;jt<n;jt++) a[it][jt]=false; de[it]=-1; de2[it]=-1; } for(it=0;it<m;it++) { cin>>left>>mid>>right; getchar(); ni=left-'A';//两点有联系时,入度先都置0 nj=right-'A'; if(de[ni]==-1) { de[ni]=0; de2[ni]=0; } if(de[nj]==-1) { de[nj]=0; de2[nj]=0; } if(!a[ni][nj]) { a[ni][nj]=true;//ni和nj有关系 de[nj]++; de2[nj]++; } if(!flag) { while(!Q1.empty())//没找到确定关系时,要一直恢复 Q1.pop(); carry(n,it+1); } } if(flag) { if(kind==1) { printf("Sorted sequence determined after %d relations: ",relations); while(!Q1.empty()) { cout<<Q1.front(); Q1.pop(); } cout<<"."<<endl; } else if(kind==2) { printf("Inconsistency found after %d relations.\n",relations); } } else printf("Sorted sequence cannot be determined.\n"); } }
View Code
相关文章推荐
- Sorting It All Out poj 1094(判断严格小于关系&判断是否有环)
- 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 a b c e s f c s a d e e 矩阵中包含一条字符串"bccced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中
- shell 判断字符串是否存在包含关系
- mysql 判断两个字符串是否存在包含关系-------(1,2,3)与(2,3)
- 判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进
- 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。
- 函数-判断两个关键字是否存在包含关系
- shell 判断字符串是否存在包含关系
- 正则表达式判断是否存在中文和全角字符和判断包含中文字符串长度
- Window&nbsp;bat判断文件是否存在…
- 输入一个数,把它作为一个串,判断其中是否包含长度>=2的相同子串,如 果包含,返回1,不包含,返回0
- CL_GUI_FRONTEND_SERVICES=>FILE_EXIST(判断物理文件是否存在)
- 【编程之法】如何最快的判断出短字符串b中的所有字符是否都在长字符串a中?编写函数bool StringContain(string&b,string&a)实现此功能。
- 在一个长度为n(n < 1000)的整数序列中,判断是否存在某两个元素之和为k。
- 判断是否存在一条直线穿过所有的线段
- sql 查询所有数据库、表名、表字段总结,判断表/视图/存储过程是否存在
- Mysql判断两列字符串是否有包含关系
- SQLServer2005 判断数据库中是否存在某张表或是查找库中的所有表名,然后删除
- sqlite查看所有表名及字段名,及判断是否存在的方法
- 算法4— 判断一个字符串里是否包含另一个字符串所有的字符