标签:des style http color os strong
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 26911 | Accepted: 9285 |
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
East Central North America 2001
AC代码:
#include<iostream> #include<vector> #include<stack> #include<algorithm> #include<cstring> #include<stdio.h> using namespace std; int n,m; int in[30],tmp_in[30]; int f1,f2; int str[30]; vector <int> G[30]; void top_sort(){ //拓扑排序 f1=0; //没有环 f2=1; //唯一排序 for(int i=0;i<n;i++) tmp_in[i]=in[i]; stack <int> s; while(!s.empty()) s.pop(); for(int i=0;i<n;i++) if(tmp_in[i]==0) s.push(i); int counter=0; while(!s.empty()){ if(s.size()>=2){ f2=0; } int tmp=s.top(); s.pop(); str[counter++]=tmp; for(int i=0;i<G[tmp].size();i++){ tmp_in[G[tmp][i]]--; if(!tmp_in[G[tmp][i]]) s.push(G[tmp][i]); } } if(counter<n) f1=1; } int main(){ while(cin>>n>>m&&(n!=0||m!=0)){ memset(in,0,sizeof(in)); int i; for(i=0;i<n;i++) if(!G[i].empty()) G[i].clear(); for(i=0;i<m;i++){ char ch[5]; cin>>ch; G[ch[0]-'A'].push_back(ch[2]-'A'); in[ch[2]-'A']++; top_sort(); if(f1){ cout<<"Inconsistency found after "<<i+1<<" relations."<<endl; for(int j=i+1;j<m;j++) //注意这里要输入剩下的信息才能退出 cin>>ch; break; } else if(f2){ cout<<"Sorted sequence determined after "<<i+1<<" relations: "; for(int j=0;j<n;j++) cout<<char (str[j]+'A'); cout<<'.'<<endl; for(int j=i+1;j<m;j++) //注意这里要输入剩下的信息才能退出 cin>>ch; break; } } if(i>=m) cout<<"Sorted sequence cannot be determined."<<endl; } return 0; }
标签:des style http color os strong
原文地址:http://blog.csdn.net/my_acm/article/details/38032289