<题目链接>

题目大意:

对于N个大写字母,给定它们的一些关系,要求判断出经过多少个关系之后可以确定它们的排序或者排序存在冲突,或者所有的偏序关系用上之后依旧无法确定唯一的排序。

解题分析:

因为本题在确定发生冲突和能够确定唯一排序的时候要及时输出,所以必然是每输入一对关系,就进行一次拓扑排序来判断。然后判断冲突就是判断是否存在环,判断是否能够确定唯一排序就是在不存在环的情况下,同时任何时候都不存在多个入度为0点。

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int N = ;
int ind[N],output[N];
bool g[N][N]; //临接矩阵此题可以随便用
int n,m,ans,loc;
bool circle,order; int topsort( int n ) {
queue<int>Q;
int inorder = true;
int num = ,temp[N];
memcpy(temp,ind,sizeof(ind)); //复制当前所有点的入度,防止下面的操作对其造成影响
for( int i = ; i < n; i++ ) { //将所有入度为0的点入队列
if( !ind[i] ) Q.push(i);
}
while( !Q.empty() ) {
if( Q.size() > )inorder = false; //判断是否存在同时有多个入度为0的点的情况
int cur = Q.front();Q.pop();
output[++num] = cur; //num计数 以存到out里面
for(int i = ; i < n; i++ )
if(g[cur][i]== && --temp[i] == )
Q.push(i);
}
if( num != n ) return ; //存在环,即发生冲突
if( !inorder ) return ; //不能确定唯一顺序
return -; //能够确定唯一顺序
}
int main(){
char s[];
while( scanf("%d%d", &n, &m) != EOF && n ){
circle=order=false;
memset(g,,sizeof(g));
memset(ind,,sizeof(ind));
for( int i = ; i <= m; i++ ){
scanf("%s", s);
if( !circle && !order ){ //已经产生冲突或者已经能够确定排序,就不用读入了
int u=s[]-'A',v=s[]-'A'; //注意这里,所有点的编号是从0~n-1
if(g[u][v] == ){
g[u][v] = ;ind[v]++;
}
ans = topsort(n);
if( ans == ){
circle = true;
printf("Inconsistency found after %d relations.\n", i);
}
else if( ans == - ){
order = true;
loc=i; //记录位置
}
}
}
if(!circle && !order ) //如果没有产生环,并且不能够确定唯一的顺序
printf("Sorted sequence cannot be determined.\n");
else if(order){ //能够确定唯一的顺序
printf("Sorted sequence determined after %d relations: ", loc);
for(int i=; i<=n; i++)printf("%c", output[i]+'A');puts(".");
}
}
}

2018-11-21

POJ 1094 Sorting It All Out 【拓扑排序】的更多相关文章

  1. ACM: poj 1094 Sorting It All Out - 拓扑排序

    poj 1094 Sorting It All Out Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & ...

  2. poj 1094 Sorting It All Out (拓扑排序)

    http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  3. [ACM_模拟] POJ 1094 Sorting It All Out (拓扑排序+Floyd算法 判断关系是否矛盾或统一)

    Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...

  4. POJ 1094 Sorting It All Out (拓扑排序) - from lanshui_Yang

    Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...

  5. poj 1094 Sorting It All Out_拓扑排序

    题意:是否唯一确定顺序,根据情况输出 #include <iostream> #include<cstdio> #include<cstring> #include ...

  6. POJ 1094 Sorting It All Out 拓扑排序 难度:0

    http://poj.org/problem?id=1094 #include <cstdio> #include <cstring> #include <vector& ...

  7. PKU 1094 Sorting It All Out(拓扑排序)

    题目大意:就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列. 是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序列有序,并依次输出: 2.判断该序列是否唯一: 3.该序列字母次序之间 ...

  8. POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39602   Accepted: 13 ...

  9. [ACM] POJ 1094 Sorting It All Out (拓扑排序)

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26801   Accepted: 92 ...

  10. POJ 1094:Sorting It All Out拓扑排序之我在这里挖了一个大大的坑

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29984   Accepted: 10 ...

随机推荐

  1. RCNN--目标检测

    原博文:http://www.cnblogs.com/soulmate1023/p/5530600.html 文章简要介绍RCNN的框架,主要包含: 原图-->候选区域生成-->对每个候选 ...

  2. Markdown 简介及基础语法

    一.Markdown 简介 Markdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式. 二.Markdown 基础语法 1. Markdown ...

  3. yolov3 安装训练

    https://blog.csdn.net/helloworld1213800/article/details/79749359 https://blog.csdn.net/lilai619/arti ...

  4. linux cp命令使用

    功能: 复制文件或目录说明: cp指令用于复制文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中.若同时指定多个文件或目录, ...

  5. java----面对对象

    面对对象: public class Demo { public static void main(String[] args){ Horse h = null; h = new Horse(); / ...

  6. loadrunner出现报错operands of = have illegal types `pointer to char' and `int'

    原始代码: void split(char * p,char * str){ /* 传入一个数组进行p和一个以什么进行分割的str,返回切片后的值 */ int i = 0, j = 0; char ...

  7. 怎样在win7 IIS中部署网站

    怎样在win7 IIS中部署网站? IIS作为微软web服务器的平台,可以轻松的部署网站,让网站轻而易举的搭建成功,那么如何在IIS中部署一个网站呢,下面就跟小编一起学习一下吧. 第一步:发布IIS文 ...

  8. spring cloud 自定义ribbon客户端

    一.自定义Ribbon客户端-[方式一]配置类 1.1.自定义负载规则 增加RibbonConfiguration.java配置类 public class RibbonConfiguration { ...

  9. python面向对象三大特性之封装

    一. 概述 定义:隐藏对象的属性和实现细节,仅对外提供公共访问方式 封装的原则:把不需要对外提供的内容都隐藏起来,提供公共的方法访问这些隐藏属性 二.封装手段 使用双下划线将属性和方法隐藏起来 cla ...

  10. Tomcat使用

    打开Tomcat官网 在浏览器地址栏输入: tomcat.apache.org,按回车 下载Tomcat 把目光移动至网页左边红匡处,点击Tomcat8 网页下移 点击箭头所指的链接 运行Tomcat ...