<题目链接>

题目大意:

对于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. ORA-00845 MEMORY_TARGET not supported on this system 的解决

    本文来源:宁静致远 的<ORA-00845 MEMORY_TARGET not supported on this system 的解决> oracle11g数据库在执行dbca或者调整s ...

  2. Confluence 6 PostgreSQL 设置准备

    请查看 Supported Platforms 页面来获得 Confluence 系统支持的 PostgreSQL 数据库版本.你需要在安装 Confluence 之前升级你的 PostgreSQL ...

  3. Spark启动时的master参数以及Spark的部署方式

    我们在初始化SparkConf时,或者提交Spark任务时,都会有master参数需要设置,如下: conf = SparkConf().setAppName(appName).setMaster(m ...

  4. JavaScript(JS)之简单介绍

    一.JavaScript的历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中).后将其改名ScriptEase.(客户端执行的语言) N ...

  5. 常用ajax样例

    ---恢复内容开始--- // url(String):请求地址 // param(String):请求参数 // targetId(String):结果显示id function ajaxReq(u ...

  6. Coding配合git使用时遇到的问题

    转载整理: https://www.jianshu.com/p/b23cd00cffa6 https://www.cnblogs.com/yidoucai/p/5228763.html https:/ ...

  7. Controller中方法返回值其他类型需要添加jackson依赖

    第一个 第二个: 第三个 https://www.cnblogs.com/codejackanapes/p/5569013.html:json的博客园 springmvc默认的是:2.Jackson: ...

  8. Ajax增删改查-----------增

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. ZOJ 4057 XOR Clique(位运算)

    XOR Clique BaoBao has a sequence a​1​,a​2,...,a​n. He would like to find a subset S of {1,2,...,n} s ...

  10. hdfs数据到hbase过程

    需求:将HDFS上的文件中的数据导入到hbase中 实现上面的需求也有两种办法,一种是自定义mr,一种是使用hbase提供好的import工具 一.hdfs中的数据是这样的 hbase创建好表 cre ...