POJ1094 Sorting It All Out(拓扑排序)

来源:互联网 发布:万能mac地址修改器下载 编辑:程序博客网 时间:2024/04/30 07:46

题目:

Sorting It All Out
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 35190 Accepted: 12340

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 6A<BA<CB<CC<DB<DA<B3 2A<BB<A26 1A<Z0 0

Sample Output

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

Source

East Central North America 2001

[Submit]   [Go Back]   [Status]   [Discuss]

思路;

题目给出了很多对关系,有n个顶点,m对关系,让你判断最后这个序列存不存在。

1.如果这个序列有序,那么输出这个序列

2.如果序列不能判断是否有序,那么输出不能判断

3.如果序列存在环,那么就输出,当前已经处理了多少对序列


设定一个变量determined来表示状态,为0时序列无法判断,为1时输出对应序列,为-1时输出处理到了第几个存在环

因为要输出处理到第几个存在环,所以我们每输入一组样例,就要进行一次拓扑排序,当出现一种矛盾时,后面的输入就不用处理了。

排序的时候如果出现了矛盾就返回-1,每次删除入度为0的点,并且删除边


代码:

#include <cstdio>#include <cstring>#include <cctype>#include <string>#include <set>#include <iostream>#include <stack>#include <cmath>#include <queue>#include <vector>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define mod 1000007#define N 20100#define M 100000+50#define ll long longusing namespace std;int id[26];//记录顶点的入度int temp[26];//count的复制,拓扑排序时进行修改char str[3],seq[26];//seq用来记录最终的序列int vis[26];int n,m;vector<vector<char>> v;int toposort(int s){  int j,r=0,cnt;//cnt表示入度为0的顶点的个数,r表示得到的序列中元素的个数  int flag=1;//表示排序结束后是否可以得到序列  for(int i=0; i<n; i++)      temp[i]=id[i];  while(s--)  {      cnt=0;      for(int i=0; i<n; i++)          if(temp[i]==0)          {              j=i;//找到入度为0的点的位置              cnt++;          }      if(cnt>=1)      {          if(cnt>1) flag=0;//无序          //cnt==1表示有且只有一个入度为0的顶点,则该顶点必然处于序列最前端          for(int i=0; i<v[j].size(); i++)              temp[v[j][i]]--;//删除顶点和出边          seq[r++]=j+'A';          temp[j]=-1;          seq[r]='\0';      }      else if(cnt==0)          return -1;//没有入度为0的点必然存在环  }  if(flag) return r;  else return 0;}int main(){  int t,k,c;//k用来处理已经处理的关系数,c用来记录当前已经读入的对象数  int determined;//-1表示关系矛盾,0表示无法得到序列,1表示正常得到序列  while(~scanf("%d%d",&n,&m)&&(n||m))  {      mem(id,0);      mem(vis,0);      v.clear();      v.resize(n);//重新调整大小      c=0,determined=0;      for(int i=0; i<m; i++)      {          scanf("%s",str);          int x=str[0]-'A';          int y=str[2]-'A';          id[y]++;          v[x].push_back(y);          if(!vis[x])//记录读入对象数          {              c++;              vis[x]=1;          }          if(!vis[y])          {              c++;              vis[y]=1;          }          if(determined==0)          {              t=toposort(c);//得到序列中的元素的个数              if(t==-1)//存在环  {  determined=-1;  k=i+1;  }  else if(t==n)  {  determined=1;  k=i+1;  }          }      }      if(determined==-1)          printf("Inconsistency found after %d relations.\n",k);//输出的k是已经成功处理了几个      else if(determined==0)//无法处理          printf("Sorted sequence cannot be determined.\n");      else//输出对应的序列          printf("Sorted sequence determined after %d relations: %s.\n",k,seq);  }  return 0;}



原创粉丝点击