POJ1094(拓扑排序)

来源:互联网 发布:阿里云升级系统盘 编辑:程序博客网 时间:2024/05/22 13:59

依次读入边,每次读入之后都做一次拓扑排序,来判断是否有环或者已经确定序列。

题意有些混淆人,每次读入都要判断 ①是否有环②是否已经确定序列。

上述两个条件若有一个满足则立即输出,后面的边都可以不管了。

比如这组测试数据:

2 2
A<B
B<A
0 0
Sorted sequence determined after 1 relations: AB.

在第一条边的时候已经确定了序列,即使是后面成环也无关紧要了。

当所有的边读完后都没有环也不能确定序列的话,就输出Sorted sequence cannot be determined。

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <stack>using namespace std;int main(){int n,m;  while (1)  {  int i,j;  int edge[100][100]={{0}};  int degree[100]={0};  int temp_degree[100];  stack<int> S;  int flag=0;  scanf("%d%d",&n,&m);  getchar();  if (n==0 && m==0)  break;  for (i=0;i<=m-1;i++)  {    char str[100];    int length=0;  char a,b;  int undetermined=0;    int counter=0;  scanf("%c<%c",&a,&b);  getchar();  if (!edge[a-'A'][b-'A'])  degree[b-'A']++;  edge[a-'A'][b-'A']=1;  if (flag)  continue;  for (j=0;j<=n-1;j++)  {  if (!degree[j])  S.push(j);  }  memcpy(temp_degree,degree,sizeof(degree));  int k;  for (k=0;k<=n-1 && !S.empty();k++)  {  if (S.size()>1)    undetermined=1;  int top=S.top();  S.pop();  str[length++]=top+'A';  str[length]='\0';  counter++;  for (j=0;j<=n-1;j++)  if (edge[top][j] && (--temp_degree[j])==0 )  S.push(j);  }  if (i==m-1 && undetermined==1)  {  printf("Sorted sequence cannot be determined.\n");  }  else if (counter==n && S.empty() && !undetermined)  {  flag=1;  printf("Sorted sequence determined after %d relations: %s.\n",i+1,str);  }  else if (counter!=n)  {  flag=1;  printf("Inconsistency found after %d relations.\n",i+1);  }  }  }return 0;}


原创粉丝点击