标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 33184 | Accepted: 11545 |
Description
Input
Output
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.
Source
// // main.cpp // poj1094 // // Created by Candy on 9/11/16. // Copyright © 2016 Candy. All rights reserved. // #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int N=30; int n,m,ind[N],ch[N][N]; char tmp[5],topo[N]; int q[N]; int topoSort(){ int flag=2;//only sorted int indt[N]; for(int i=1;i<=n;i++) indt[i]=ind[i]; for(int i=1;i<=n;i++){//printf("%d %d %d \n",i,ind[i],ch[i][0]); int zero=0,u=0; for(int j=1;j<=n;j++) if(indt[j]==0) zero++,u=j; if(zero==0) return 1;//circle if(zero>1) flag=3;//no sorted indt[u]--; topo[i-1]=(char)u+‘A‘-1; q[i-1]=u; for(int j=1;j<=ch[u][0];j++) indt[ch[u][j]]--; } return flag; } int main(int argc, const char * argv[]) { while(cin>>n>>m){ if(n==0&&m==0) break; memset(ch,0,sizeof(ch)); memset(ind,0,sizeof(ind)); memset(topo,0,sizeof(topo)); int sign=0; for(int i=1;i<=m;i++){ scanf("%s",tmp); if(sign) continue; int x=tmp[0]-‘A‘+1,y=tmp[2]-‘A‘+1; ind[y]++; ch[x][++ch[x][0]]=y; int flag=topoSort(); if(flag==1){sign=1;printf("Inconsistency found after %d relations.\n",i);} if(flag==2){ sign=1;printf("Sorted sequence determined after %d relations: %s.\n",i,topo); // printf("Sorted sequence determined after %d relations: ",i); // for(int j=0;j<n;j++) // printf("%c",q[j]+‘A‘-1); // printf(".\n"); // sign=1; } } if(!sign) printf("Sorted sequence cannot be determined.\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/candy99/p/5863329.html