Source Code Problem: 1094 User: zhouxc Memory: 452K Time: 32MS Language: G++ Result: Accepted Source Code #include "iostream" #include "stack" using namespace std; int link[30][30],degree_in[30],degree[30],Top[30]; char a,b,c; bool only,visit_m[30]; int n,m,record,count_n,circle; int Top_sort() { only=true; record=0; stack<int> s; //寻找入度为零的点,入栈。 for(int i=1;i<=n;i++) { if(visit_m[i]&&!degree_in[i]) { s.push(i); degree[i]=-1; } } // cout<<" "<<d<<" "<<v[d]<<" "<<endl; //若度数为零的点大于1,则不唯一。 if(s.size()>1) only=false; //找与该点相连的点,并除去该点,查找新的入度为零的点,并入度。 while(!s.empty()) { int t=s.top(); // cout<<t<<endl; Top[record++]=t; s.pop(); for(int i=1;i<=n;i++) { if(link[t][i]==1) { degree[i]--; for(int i=1;i<=n;i++) { if(visit_m[i]&&!degree[i]) { // cout<<i<<endl; s.push(i); degree[i]=-1; } } } } //若入度为零的点大于1,则不唯一。 if(s.size()>1) only=false; } // cout<<record<<endl; //若存在入度为0的点删除后,还存在入度大于0的,则表示有圈 for(int j=1;j<=n;j++) { if(degree[j]>0)//-1表示有圈 return -1; } if(record==n&&only) { return 1;//1表示可以唯一排序 } return 0;//不唯一 } int main() { // freopen("data.in","r",stdin); while(cin>>n>>m&&n!=0&&m!=0) { int i,s; circle=-1;count_n=-1;record=0; memset(link,0,sizeof(link)); memset(degree_in,0,sizeof(degree_in)); memset(visit_m,false,sizeof(visit_m)); memset(degree,0,sizeof(degree)); for(i=1;i<=m;i++) { cin>>a>>c>>b; int a1=(int)(a-'A')+1; int b1=(int)(b-'A')+1; // cout<<count_n<<circle<<endl; //当有圈或排好序时结束 if(circle==-1&&count_n==-1) { visit_m[a1]=true; visit_m[b1]=true; if(link[a1][b1]!=1) degree_in[b1]++; link[a1][b1]=1; for(int j=1;j<=n;j++) degree[j]=degree_in[j]; //每输入一条边就排序 s=Top_sort(); if(s==1) count_n=i; else if(s==-1) circle=i; } } if(s==1) { cout<<"Sorted sequence determined after "<<count_n<<" relations: "; for(int i=0;i<n;i++) cout<<char(Top[i]+64); cout<<"."<<endl; } if(s==-1) cout<<"Inconsistency found after "<<circle<<" relations."<<endl; if(s==0) cout<<"Sorted sequence cannot be determined."<<endl; } // while(1); return 0; }