[POJ] 1094 Sorting It All Out
2018-03-22 01:25
459 查看
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37417 Accepted: 13220 Description An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D implies that A < B, B < C and C < D. in this problem, we will give you a set of relations of the form A < B and ask you to determine whether a sorted order has been specified or not. Input Input consists of multiple problem instances. Each instance starts with a line containing two positive integers n and m. the first value indicated the number of objects to sort, where 2 <= n <= 26. The objects to be sorted will be the first n characters of the uppercase alphabet. The second value m indicates the number of relations of the form A < B which will be given in this problem instance. Next will be m lines, each containing one such relation consisting of three characters: an uppercase letter, the character "<" and a second uppercase letter. No letter will be outside the range of the first n letters of the alphabet. Values of n = m = 0 indicate end of input. Output For each problem instance, output consists of one line. This line should be one of the following three: Sorted sequence determined after xxx relations: yyy...y. Sorted sequence cannot be determined. Inconsistency found after xxx relations. where xxx is the number of relations processed at the time either a sorted sequence is determined or an inconsistency is found, whichever comes first, and yyy...y is the sorted, ascending sequence. 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
最失败的题之一,调了太久,很难受。
topo()的返回值写成bool,查良久,无果。。
本质就是拓扑排序,把字母当成节点,建图,不过加入了几个特殊的条件,有环或者无法完全排序都是特殊情况,要特殊判断,但是判断完了剩下的数据仍然得输入。
然后输出的部分,句号什么的,很难受。写的时候也是很混乱..
可能今天心情不好…
//Stay foolish,stay hungry,stay young,stay simple #include<iostream> #include<queue> #include<cstdio> #include<cstring> using namespace std; const int MAXN=20000; int n,m; struct Edge{ int next,to; }e[MAXN]; int ecnt,head[MAXN],ind[MAXN]; inline void add(int x,int y){ e[++ecnt].next = head[x]; e[ecnt].to = y; head[x] = ecnt; } int tmp[MAXN]; int ans[MAXN],anscnt; int topo(){ anscnt=0; memcpy(tmp,ind,sizeof(ind)); int ret=1; for(int i=1;i<=n;i++){ int cnt=0,pos; for(int j=1;j<=n;j++) if(tmp[j]==0) cnt++,pos=j; if(cnt==0) return 0;//circle if(cnt>1) ret=-1;//unsure tmp[pos]=-1;// ans[++anscnt]=pos; < 4000 span class="hljs-keyword">for(int j=head[pos];j;j=e[j].next){ int v=e[j].to; tmp[v]--; } } return ret; } void slove(){ bool succ=0; for(int i=1;i<=m;i++){ char s[5]; cin>>s; if(succ) continue; int x=s[0]-'A'+1; int y=s[2]-'A'+1; add(x,y); ind[y]++; int res=topo(); if(res==0){ printf("Inconsistency found after %d relations.\n",i); succ=1; } if(res==1){ printf("Sorted sequence determined after %d relations: ",i); for(int j=1;j<=anscnt;j++) cout<<char(ans[j]+'A'-1); printf(".\n"); succ=1;// } } if(!succ) printf("Sorted sequence cannot be determined.\n"); } int main(){ // freopen("test.txt","r",stdin); while(cin>>n>>m&&n){ ecnt=0,anscnt=0; memset(head,0,sizeof(head)); memset(ind,0,sizeof(ind)); slove(); } return 0; }
相关文章推荐
- POJ1094——Sorting It All Out
- poj 1094 Sorting It All Out(java + 拓扑排序)
- poj 1094 Sorting It All Out
- poj 1094 Sorting It All Out
- 初学拓扑排序---POJ1094(Sorting It All Out)
- POJ 1094 Sorting It All Out
- POJ-1094 Sorting It All Out
- Hoj 1336/Poj 1094 Sorting It All Out
- Sorting It All Out(POJ--1094
- POJ 1094 Sorting It All Out
- nyoj 349 poj 1094 Sorting It All Out
- poj 1094 Sorting It All Out
- POJ 1094 Sorting It All Out
- POJ 1094 Sorting It All Out
- poj1094 Sorting It All Out
- poj 1094 Sorting It All Out
- POJ 1094 Sorting It All Out(欧拉排序)
- POJ1094 Sorting It All Out
- POJ 1094 Sorting It All Out
- poj 1094 Sorting It All Out