题目来源:http://poj.org/problem?id=1094
Sorting It All Out
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 35447 | Accepted: 12444 |
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 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.
最开始做的时候理解错题意了,以为是有向图中找最小环的问题。。。。。。
事实上是一行一行读入,出现环就停止扩展边,这样用拓扑排序就行了。
要注意三种结果的优先级,出现环的优先级最高,不能被确定的优先级最低。
在拓扑排序的过程中,如果点未全删去时找不到入度为0的点,则存在环,如果同时有多个入度为0的点,那么不能够确定顺序,但要进行完全以便找到环。
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <queue>
using namespace std;
const int maxn=30;
const int maxm=10000;
int n,m;
int from[maxm];
queue<int> q;
int to[maxm];
bool vis[maxn];
int du[maxn];
int t[maxn];
int ans[maxn];
int g[maxn][maxn];
int topo();
int main()
{
ios::sync_with_stdio(false);
// freopen("std.out","w",stdout);
cin.tie(0);
cin>>n>>m;
while(n)
{
memset(du,0,sizeof(du));
memset(g,0,sizeof(g));
memset(from,0,sizeof(from));
memset(to,0,sizeof(to));
for(int i=1;i<=m;i++)
{
char c;cin>>c;
from[i]=c-'A'+1;
cin>>c;cin>>c;
to[i]=c-'A'+1;
}
int ok=0;
for(int k=1;k<=m;k++)
{
if(!g[from[k]][to[k]])
{
g[from[k]][to[k]]=1;
du[to[k]]++;
memset(t,0,sizeof(t));
memset(ans,0,sizeof(ans));
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)t[i]=du[i];
ok=topo();
if(ok==1)
{
cout<<"Sorted sequence determined after "<<k<<" relations: ";
for(int i=1;i<=n;i++)cout<<(char)(ans[i]-1+'A');
cout<<"."<<endl;
break;
}
if(ok==2)
{
cout<<"Inconsistency found after "<<k<<" relations."<<endl;
break;
}
}
}
if(!ok)cout<<"Sorted sequence cannot be determined."<<endl;
cin>>n>>m;
}
return 0;
}
int topo()
{
while(!q.empty())q.pop();
int ok=1;int cnt=0;
for(int k=1;cnt<n;k++)
{
int tot=0;
for(int i=1;i<=n;i++)if(vis[i]==0&&t[i]==0){tot++;q.push(i);}
if(tot>1)ok=0;
if(tot==0)return 2;
while(!q.empty())
{
int x=q.front();q.pop();
ans[++cnt]=x;
vis[x]=1;
for(int i=1;i<=n;i++)
{
if(vis[i]==0&&g[x][i])t[i]--;
}
}
}
return ok;
}