poj1094 拓扑排序
来源:互联网 发布:什么是淘宝营销词 编辑:程序博客网 时间:2024/06/05 09:48
http://poj.org/problem?id=1094
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.
/**poj 1094 拓扑排序题目大意: 对于N个大写字母,给定它们的一些偏序关系,要求判断出经过多少个偏序关系之后可以确定它们的排序或者存在冲突, 或者所有的偏序关系用上之后依旧无法确定唯一的排序。 解题思路:(来自网上) 利用拓扑排序即可解决这个问题,但由于题目要求的是经过多少个关系之后就可以确定答案,因此每读入一个关系, 就要进行一次拓扑排序,如果某一次拓扑排序之后可以确定它们的唯一排序或者发现冲突存在,则后面的直接略过。 若读入所有关系之后依然无法确定唯一关系,同时也没有冲突,则输出不能确定唯一排序。若拓扑排序的过程中每次 仅有一个点入度为0,则该排序是可以确定的排序,否则若多个点入度为0,则不知道哪个点先哪个点后。若拓扑排序 进行到某一步之后无法再继续,则说明存在回路,此时说明存在冲突。*/#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <queue>using namespace std;const int maxn=30;int head[maxn],ip,indegree[maxn];int n,m,seq[maxn];struct note{ int v,next;} edge[maxn*maxn];void init(){ memset(head,-1,sizeof(head)); ip=0;}void addedge(int u,int v){ edge[ip].v=v,edge[ip].next=head[u],head[u]=ip++;}int topo()///拓扑,可做模板{ queue<int>q; int indeg[maxn]; for(int i=0; i<n; i++) { indeg[i]=indegree[i]; if(indeg[i]==0) q.push(i); } int k=0; bool res=false; while(!q.empty()) { if(q.size()!=1)res=true; int u=q.front(); q.pop(); seq[k++]=u; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; indeg[v]--; if(indeg[v]==0) q.push(v); } } if(k<n)return -1;///存在有向环或者图不连通,总之不能进行拓扑排序 if(res)return 0;///可以进行拓扑排序,并且只有唯一一种方式,seq数组即是排序完好的序列 return 1;///可以进行拓扑排序,有多种情况,seq数组是其中一种序列}int main(){ while(~scanf("%d%d",&n,&m)) { if(n==0&&m==0) break; init(); int ans=-1,err=-1; memset(indegree,0,sizeof(indegree)); for(int i=0; i<m; i++) { char s[5]; scanf("%s",s); if(ans!=-1||err!=-1) continue; int u=s[0]-'A'; int v=s[2]-'A'; addedge(u,v); indegree[v]++; int res=topo(); if(res==1) ans=i+1; else if(res==-1) err=i+1; } if (ans != -1) { printf("Sorted sequence determined after %d relations: ", ans); for (int i = 0; i < n; ++i) putchar('A' + seq[i]); printf(".\n"); } else if (err != -1) { printf("Inconsistency found after %d relations.\n", err); } else { printf("Sorted sequence cannot be determined.\n"); } } return 0;}
0 0
- Poj1094拓扑排序总结
- 拓扑排序 poj1094
- poj1094 拓扑排序
- poj1094 拓扑排序
- poj1094拓扑排序
- POJ1094(拓扑排序)
- 拓扑排序poj1094
- poj1094 拓扑排序
- poj1094----拓扑排序
- 拓扑排序POJ1094
- poj1094(拓扑排序)
- POJ1094(简单拓扑排序)
- POJ1094 拓扑排序
- poj1094(拓扑排序)
- 拓扑排序(poj1094)
- POJ1094 拓扑排序
- poj1094(拓扑排序应用)
- poj1094 好一个拓扑排序
- Linux下SVN部署/安全及权限配置,实现web同步更新
- UVA 1225 - Digit Counting
- 计算机组成原理课程设计 Dais CMX16 模型机指令及微指令设计
- 线程化的性能障碍:它们将给 OpenMP 代码带来什么影响?
- Android中Parcelable接口法
- poj1094 拓扑排序
- 2014年国人开发的最热门的开源软件TOP 100
- Java “反射”的乐趣
- 类与对象:类模板
- VM 安装 linux 后 出现“Vmware Easy Install”的解决方法
- OpenMP中数据属性相关子句详解(1):private/firstprivate/lastprivate/threadprivate之间的比较
- Linux下安装JDK
- JSONRPC的HelloWorld应用
- 证明:MST中的最长边必定小于其他生成树中的最长边