标签:cas letters tar map tor ber typedef element proc
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.
代码:
#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; }
标签:cas letters tar map tor ber typedef element proc
原文地址:https://www.cnblogs.com/Staceyacm/p/11261298.html