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
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 ASample Output
Sorted sequence determined after 4 relations: ABCD. Inconsistency found after 2 relations. Sorted sequence cannot be determined./** poj 1094 拓扑排序 题目大意: 对于N个大写字母,给定它们的一些偏序关系,要求判断出经过多少个偏序关系之后可以确定它们的排序或者存在冲突, 或者所有的偏序关系用上之后依旧无法确定唯一的排序。 解题思路:(来自网上) 利用拓扑排序即可解决这个问题,但由于题目要求的是经过多少个关系之后就可以确定答案,因此每读入一个关系, 就要进行一次拓扑排序,如果某一次拓扑排序之后可以确定它们的唯一排序或者发现冲突存在,则后面的直接略过。 若读入所有关系之后依然无法确定唯一关系,同时也没有冲突,则输出不能确定唯一排序。若拓扑排序的过程中每次 仅有一个点入度为0,则该排序是可以确定的排序,否则若多个点入度为0,则不知道哪个点先哪个点后。若拓扑排序 进行到某一步之后无法再继续,则说明存在回路,此时说明存在冲突。 */ #include#include #include #include #include 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 q; int indeg[maxn]; for(int i=0; i