Sorting It All Out (拓扑排序+思维)

Posted staceyacm

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sorting It All Out (拓扑排序+思维)相关的知识,希望对你有一定的参考价值。

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.
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<cmath>
const int maxn=1e5+5;
typedef long long ll;
using namespace std;
vector<int>vec[30];
int n,m;
int du[30];
int chu[30];
int du1[30];
int flag;
vector<int>ans1;
void Tpsort()

    priority_queue<int,vector<int>,greater<int> >q;
    priority_queue<int>q1;
    
    int s=0;
    for(int t=0;t<n;t++)
    
        if(chu[t]==0&&du[t]==0)
        
         s=1;    
        
    
    for(int t=0;t<n;t++)
    
        du1[t]=du[t];
    
    for(int t=0;t<n;t++)
    
        if(du1[t]==0)
        
            q.push(t);
            q1.push(t);
        
    
    vector<int>ans,ans2;
    while(!q.empty())
    
        int now=q.top();
        int now2=q1.top();
        q.pop();
        q1.pop();
        ans.push_back(now);
        ans2.push_back(now2);
        for(int t=0;t<vec[now].size();t++)
        
            int next=vec[now][t];
            du1[next]--;
            if(du1[next]==0)
            
                q.push(next);
                q1.push(next);
            
        
    
    if(ans.size()!=n)
    
        flag=1;
    
//    cout<<ans.size()<<" "<<s<<endl;
     
    if(ans.size()==n&&s==0)
    
        int sss=0;
        for(int t=0;t<ans.size();t++)
        
            if(ans[t]!=ans2[t])
            
                sss=1;
            
        
        if(sss==0)
        
            flag=2;
            for(int t=0;t<ans.size();t++)
            
                ans1.push_back(ans[t]);
            
        
        
        
    
    

int main()
 
  
   while(cin>>n>>m)
   
    if(n==0&&m==0)
    
        break;
    
    for(int t=0;t<n;t++)
    
        vec[t].clear();
    
       char str[5];
       memset(du,0,sizeof(du));
       memset(chu,0,sizeof(chu));
       flag=0;
       int ss=0;
       int k;
       ans1.clear();
       for(int t=1;t<=m;t++)
       
           scanf("%s",str);
           vec[str[0]-A].push_back(str[2]-A);
           du[str[2]-A]++;
           chu[str[0]-A]++;
           if(ss)
           
           continue;    
        
           Tpsort();
           if(flag==1)
           
               printf("Inconsistency found after %d relations.\n",t); 
               ss=1;
        
        else if(flag==2)
        
            ss=1;
            printf("Sorted sequence determined after %d relations: ",t); 
            for(int j=0;j<n;j++)
            
                printf("%c",ans1[j]+A);
            
            printf(".\n");
            
        
    
    if(ss==0)
    puts("Sorted sequence cannot be determined.");
    
   
   return 0;

 

以上是关于Sorting It All Out (拓扑排序+思维)的主要内容,如果未能解决你的问题,请参考以下文章

POJ 1094: Sorting It All Out( 拓扑排序 )

POJ 1094 Sorting It All Out(拓扑排序判环)

POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)

POJ 1094-Sorting It All Out(拓扑排序)

poj1094Sorting It All Out——拓扑排序

[poj1094]Sorting It All Out_拓扑排序