1094

/*
一个用来检查环,一个用来检查是否确定了排序
*/

// include file #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <cctype> #include <ctime> #include <iostream> #include <sstream> #include <fstream> #include <iomanip> #include <bitset> #include <algorithm> #include <string> #include <vector> #include <queue> #include <set> #include <list> #include <functional> using namespace std; // typedef typedef __int64 LL; // #define read freopen("in.txt","r",stdin) #define write freopen("out.txt","w",stdout) #define Z(a,b) ((a)<<(b)) #define Y(a,b) ((a)>>(b)) const double eps = 1e-6; const double INFf = 1e100; const int INFi = 1000000000; const LL INFll = (LL)1<<62; const double Pi = acos(-1.0); template<class T> inline T sqr(T a){return a*a;} template<class T> inline T TMAX(T x,T y) { if(x>y) return x; return y; } template<class T> inline T TMIN(T x,T y) { if(x<y) return x; return y; } template<class T> inline T MMAX(T x,T y,T z) { return TMAX(TMAX(x,y),z); } template<class T> inline T MMIN(T x,T y,T z) { return TMIN(TMIN(x,y),z); } template<class T> inline void SWAP(T &x,T &y) { T t = x; x = y; y = t; } // code begin int N,M; bool mp[26][26]; int in[26],in2[26]; char tmp[28]; int next() { int cnt = 0,ans; for(int i=0;i<N;i++) { if(in2[i]==0) { cnt++; ans = i; } } if(cnt==1) return ans; return -1; } int next2() { int ans; for(int i=0;i<N;i++) if(in2[i]==0) return i; return -1; } bool checksort(int dx) { for(int i=0;i<N;i++) in2[i] = in[i]; int head; for(int i=0;i<N;i++) { head = next(); if(head<0) return false; tmp[i] = head+'A'; for(int j=0;j<N;j++) { if(mp[head][j]) { in2[j]--; } } in2[head] = -1; } tmp[N] = 0; printf("Sorted sequence determined after %d relations: %s.\n",dx,tmp); return true; } int checkcircle() { for(int i=0;i<N;i++) in2[i] = in[i]; int head; for(int i=0;i<N;i++) { head = next2(); if(head<0) return true; for(int j=0;j<N;j++) if(mp[head][j]) in2[j]--; in2[head]=-1; } return false; } int main() { read; write; int a,b,f,q; char strin[5]; while(scanf("%d %d",&N,&M)==2) { if(N+M==0) break; memset(in,0,sizeof(in)); memset(mp,0,sizeof(mp)); f= 0; int i; for(i=1;i<=M;i++) { scanf("%s",strin); a = strin[0]-'A'; b = strin[2]-'A'; mp[a][b] = 1; in[b]++; if(checkcircle()) { printf("Inconsistency found after %d relations.\n",i); f=1;break; } if(checksort(i)) { f=1;break; } } for(i++;i<=M;i++) scanf("%s",strin); if(!f) { printf("Sorted sequence cannot be determined.\n"); } } return 0; }
posted @ 2011-04-28 19:41  AC2012  阅读(114)  评论(0)    收藏  举报