POJ Sorting It All Out 1094(拓扑排序)
来源:互联网 发布:程序员推荐书单 编辑:程序博客网 时间:2024/05/16 10:17
Sorting It All Out
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 32801 Accepted: 11394
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.
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 6A<BA<CB<CC<DB<DA<B3 2A<BB<A26 1A<Z0 0
Sample Output
Sorted sequence determined after 4 relations: ABCD.Inconsistency found after 2 relations.Sorted sequence cannot be determined.题意:1给出m条关系,如果可以确定一个唯一的关系,则把这个关系输出来
2如果在i条关系存在环,则输出在i条关系存在环
3其他情况输出不确定
每给出一条关系都需要判断一下,如果有1或2的情况,则只录入,不判断
拓扑排序,用一个队列,如果度等于0,入队,然后找以这个点为前缀的点,改变度,直到队列为空
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <queue>#include <algorithm>using namespace std;int v[50],tep[50],du[50],mp[50][50],k[50];int n;void init(){ for(int i=0;i<n;i++) { du[i]=0; } memset(mp,0,sizeof(mp));}int topsort(){ memset(v,0,sizeof(v)); queue<int >q; while(!q.empty())q.pop(); int i,j; for(i=0;i<n;i++) { if(du[i]==0) { v[i]=1; q.push(i); } } int ff=0,pos=0; while(!q.empty()) { if(q.size()>1)ff=1; int t=q.front(); q.pop(); k[pos++]=t; for(i=0;i<n;i++) { if(mp[t][i]) du[i]--; } for(i=0;i<n;i++) { if(!v[i] && du[i]==0) { v[i]=1; q.push(i); } } } if(pos<n) return 1; if(ff) return 2; return 3;}int main(){ int m,t,i,j; while(~scanf("%d%d",&n,&m)) { if(n==0&&m==0) { break; } init(); char s[10]; int flag=2,ff=0,stop; for(j=1;j<=m;j++) { scanf("%s",s); if(ff)continue; int a=s[0]-'A'; int b=s[2]-'A'; if(!mp[a][b]) { mp[a][b]=1; du[b]++; } for(i=0;i<n;i++) tep[i]=du[i]; flag=topsort(); for(i=0;i<n;i++) du[i]=tep[i]; if(flag!=2) { ff=1; stop=j; } } if(flag==3) { printf("Sorted sequence determined after %d relations: ",stop); for(i=0;i<n;i++) { printf("%c",k[i]+'A'); } printf(".\n"); } else if(flag==1) { printf("Inconsistency found after %d relations.\n",stop); } else { printf("Sorted sequence cannot be determined.\n"); } }}
1 0
- POJ 1094 Sorting It All Out(拓扑排序)
- Poj 1094-Sorting It All Out [拓扑排序]
- POJ 1094-Sorting It All Out 拓扑排序
- zoj 1060 || poj 1094 Sorting It All Out(拓扑排序)
- POJ 1094 Sorting It All Out 拓扑排序
- POJ 1094 Sorting It All Out 拓扑排序
- POJ 1094 Sorting It All Out 拓扑排序
- poj 1094 Sorting It All Out 拓扑排序
- POJ 1094 Sorting It All Out (拓扑排序)
- POJ 1094 Sorting It All Out 拓扑排序
- poj 1094 Sorting It All Out(拓扑排序)
- poj 1094 Sorting It All Out 拓扑排序
- poj 1094 Sorting It All Out 拓扑排序
- poj 1094 Sorting It All Out (拓扑排序)
- poj 1094 Sorting It All Out (拓扑排序)
- Poj 1094 Sorting It All Out——拓扑排序
- POJ 1094 Sorting It All Out 拓扑排序+Floyd算法
- POJ 1094 Sorting It All Out (拓扑排序) - from lanshui_Yang
- la4287(有向图的强连通分量和DAG)
- SQL语句返回多表联合查询的结果集的数据条数
- nyoj 599 奋斗的蜗牛
- HDU 3938 Portal
- HDU 3435 KM算法或者最小费用最大流
- POJ Sorting It All Out 1094(拓扑排序)
- HDOJ 1070 Milk
- C++模版和泛型编程
- 顺序表应用2:多余元素删除之建表算法
- bzoj 4621: Tc605 动态规划
- 数据结构实验之串二:字符串匹配
- 变更设计的流程
- 【C++11】随机数函数库random
- 7. PHP接入支付宝有密批量转款接口