题目:
Sorting It All Out
时间限制:
3000 ms | 内存限制:
65535 KB
难度:
3
-
描述
-
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.
样例输入
-
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
样例输出
-
Sorted sequence determined after 4 relations: ABCD. Inconsistency found after 2 relations. Sorted sequence cannot be determined.
-
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.
题目大意:
给m个关系,求是否能确定n个数的大小关系,如果能,输出能开始确定的位置和关系,如果形成错乱关系,输出开始错乱的位置.否则输出不能.
题目思路:
1、题目是aov网,排顺序.要用拓扑排序
2、题目要求还要给出最先能排成的位置,所以要不断地添加,看是否能排成
3、注意三个要求的顺序 有圈>有顺序>无顺序
题目优化:
1、如果找到答案,就不必搜索
2、如果输入的与前面相同,不用搜索
程序:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cctype>
#include <fstream>
#include <limits>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cassert>
using namespace std;
int n,m;
int in[30],t_in[30],ans[30];//in:入度 t_in入度的复制 ans 答案保存
bool t[30][30];//优化1 防止重复搜索
vector < vector <int> >ve;
int aov();
int main()
{
while(~scanf("%d %d",&n,&m)&&n+m)
{
memset(in,-1,sizeof(in));
memset(t,0,sizeof(t));
int f=0;
ve.clear();
ve.resize(n);
for(int i=1; i<=m; i++)
{
int smart,bigger;
char s[10];
scanf("%s",s);
smart=s[0]-'A',bigger=s[2]-'A';
if(t[smart][bigger]) continue;//优化1 剔除重复
t[smart][bigger]=1;
ve[smart].push_back(bigger);//储存关系
if(in[smart]==-1)//-1 标记是否出现 0以上是入度
in[smart]=0;
if(in[bigger]==-1)
in[bigger]=0;
in[bigger]++;
if(!f)//优化2 搜索成立就不用搜索了
f=aov()*i;
}
if(!f)//输出结果
printf("Sorted sequence cannot be determined.\n");
else if(f>0)
{
printf("Sorted sequence determined after %d relations: ",f);
for(int i=0; i<n; i++)
printf("%c",ans[i]+'A');
printf(".\n");
}
else
printf("Inconsistency found after %d relations.\n",-f);
}
}
int aov()
{
int t_in[30],flag=1;//t_in:复制in[] flag标记是否出现入度为0的
for(int i=0; i<30; i++)//复制
t_in[i]=in[i];
for(int k=0; k<n; k++)//每次消掉一个数
{
int time=0,w,f=0;
for(int i=0; i<n; i++)//遍历是否有入度为0的
{
if(t_in[i]==0)//入度为0
{
w=i;
time++;
}
else if(t_in[i]>0)//查找是否有大于0的入度
f=1;
}
if(time==1)//有顺序
{
t_in[w]=-1;
ans[k]=w;
for(int j=0; j<ve[w].size(); j++)
t_in[ve[w][j]]--;
}
else if(time==0&&f) return -1;//有圈
else//无顺序 但不知道有没有圈
{
flag=0;//一旦没有圈 就是无顺序
t_in[w]=-1;
ans[k]=w;
for(int j=0; j<ve[w].size(); j++)
t_in[ve[w][j]]--;
}
}
return flag;
}