Sorting It All Out (拓扑排序)

# Sorting It All Out

##### Total Submission(s) : 64   Accepted Submission(s) : 21
Problem 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:<br><br>Sorted sequence determined after xxx relations: yyy...y.<br>Sorted sequence cannot be determined.<br>Inconsistency found after xxx relations.<br><br>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.<br>

Sample Input
```4 6A

```

Sample Output
`Sorted sequence determined after 4 relations: ABCD.Inconsistency found after 2 relations.Sorted sequence cannot be determined.`

Source
PKU

1. 在第x个关系中可以唯一的确定排序，并输出。

2. 在第x个关系中发现了有回环（Inconsisitency矛盾）

3.全部关系都没有发现上面两种情况，输出第3种.

1. 找到所有入度为0的点， 加入队列Q

2.取出队列Q的一个点，把以这个点为起点，所有它的终点的入度都减1. 如果这个过程中发现经过减1后入度变为0的，把这个点加入队列Q。

3.重复步骤2，直到Q为空。

```#include

#include

#include

#include

#include

#include

#define inf 0x3f3f3f3fusing namespace std;int n,m;int mp[30][30],dis[30],ans[30],temp[30];int topo(){
stack

q;
//用栈存储入度为0的点
memcpy(temp,dis,sizeof(dis));
for(int i=0;i

{
if(!temp[i])
{
q.push(i);
}
}
int sum=0;
bool flag0=0;
while(!q.empty())
{
if(q.size()>1){flag0=1;}
//如果同时有多个入度为0的点，则排列不是唯一确定的
int tem=q.top();
//取栈顶元素，即顶点
ans[sum++]=tem;
//记录每个顶点的即大写字母出现顺序
q.pop();
//清除栈顶元素
for(int i=0;i

{
if(mp[tem][i]&&--temp[i]==0)//如果入度减一后变为0，将这个店的后继点入栈
{
q.push(i);
}
}
}
if(sum!=n){return 1;}//有回路信息，即输出的顶点数小于总的
else if(flag0)
{
return 2;
}
return 0;//有唯一的排列}int main(){
int i,j;
char str[3];
while(cin>>n>>m,n||m)
{
bool flag1=0,flag2=0;
memset(mp,0,sizeof(mp));//记录关系
memset(dis,0,sizeof(dis));//存储入度
for(i=1;i<=m;i++)
{
cin>>str;
if(!flag1&&!flag2)
{
if(mp[str[2]-'A'][str[0]-'A']==1){
//当存在 A

< A
flag2=1;
cout<<"Inconsistency found after "<
<<" relations."<

continue;
}
if(!mp[str[0]-'A'][str[2]-'A'])  //记录关系了
{
mp[str[0]-'A'][str[2]-'A']=1;
dis[str[2]-'A']++;
}
int flag=topo();
if(!flag){
cout<<"Sorted sequence determined after "<
<<" relations: ";for(j=0;j

<

cout<

flag1=1;
}
else if(flag==1)
{
cout<<"Inconsistency found after "<
<<" relations."<

flag2=1;
}
}
}
if(!flag1&&!flag2){cout<<"Sorted sequence cannot be determined."<

```

#### 最新留言

[***.172.111.71]2022年05月22日 09时42分16秒

## 关于作者

喝酒易醉，品茶养心，人生如梦，品茶悟道，何以解忧？唯有杜康！
-- 愿君每日到此一游！