Sorting It All Out
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 41082 | Accepted: 14402 |
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
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.
题意:给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列。典型的拓扑排序,但输出格式上确有三种形式:
1.该字母序列有序,并依次输出;
2.该序列不能判断是否有序(序列不唯一);
3.该序列字母次序之间有矛盾,即有环存在。
题解:
每加入一条边需要进行一次拓扑排序,如果拓扑排序发现有环,或者排序唯一那么就可以直接输出结果,如果所有的边输入完成还不是上述两种情况,那就输出存在多种可能。
每次找到入度为0的顶点个数只能是1个,是0个说明存在环,是多个说明拓扑排序有多种可能。
需要注意的是,如果有一次找出了多个0度顶点,不能直接返回无序,只能暂时标记,因为可能之后的循环中存在环。遇到这种情况应优先输出有环的答案(看样例第二组数据),或者是我当前存在多个入度为0的点,也只能暂时标记,但不能直接得出最终答案,因为后面可能存在还有约束条件,只有当所有的m个条件都读完,那时候还存在多个入度为0的点,这样才能说明存在多种答案序列(看不太懂就看代码吧)
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#define N 1005
using namespace std;
int n,m,flag,ansi,cricleflag,ansflag;
char s[5],ans[N];
bool vis[105][105];
int in[N];
vector<int>v[N];
int make()
{
int temp[N],many=0,num=0;
queue<int>q;
memcpy(temp,in,sizeof(in));
for(int i=0;i<n;i++)
{
if(in[i]==0)
{
q.push(i);
}
}
while(!q.empty())
{
if(q.size()>1)many=1;
int now=q.front();
q.pop();
num++;
ans[num]=now+'A';
for(int i=0;i<v[now].size();i++)
{
int next=v[now][i];
temp[next]--;
if(temp[next]==0)
{
q.push(next);
}
}
}
if(num!=n)return -1;//说明有环
if(many==1)return 0;//说明存在多种情况
return 1;//说明序列唯一
}
int main()
{
while(scanf("%d%d",&n,&m)&&n!=0&&m!=0)
{
cricleflag=0;ansflag=0;
memset(vis,0,sizeof(vis));
memset(in,0,sizeof(in));
for(int i=0;i<n;i++)v[i].clear();
for(int i=1;i<=m;i++)
{
scanf("%s",s);
if(cricleflag==0&&ansflag==0)
{
if(!vis[s[0]-'A'][s[2]-'A'])
{
v[s[0]-'A'].push_back(s[2]-'A');
in[s[2]-'A']++;
vis[s[0]-'A'][s[2]-'A']=1;
}
flag=make();
if(flag==-1)
{
cricleflag=1;
printf("Inconsistency found after %d relations.\n",i);
continue;
}
if(flag==1)
{
ansi=i;
ansflag=1;
continue;
}
}
}
if(cricleflag==0&&ansflag==0)printf("Sorted sequence cannot be determined.\n");
else if(ansflag==1)
{
printf("Sorted sequence determined after %d relations: ",ansi);
for(int i=1;i<=n;i++)printf("%c",ans[i]);
printf(".\n");
}
}
return 0;
}
这个题目写了我将近一下午,还是没有做出来,发现自己一个很大的缺点就是,每次写代码喜欢拖拖拉拉,写到最后面越写越乱,一开始逻辑思维就不清晰,到后面各种变量名,各种复杂的都写出来了,明明可以很简单的写的,之前打比赛也是,越写到后面代码涂涂改改越乱越毒瘤,不得不要队友重构。。。。哎,下次还是要想清楚点再下手,防止不停的改来改去,到最后脑袋都混乱了,下次一定要想清楚再下手,还好只是平时训练,比赛时候就是一个大锅了。。。。