<题目链接>

题目大意:

对于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. swift 实践- 07 -- UISwitch 开关

    import UIKit class ViewController: UIViewController { var uiswitch: UISwitch? override func viewDidL ...

  2. Go 开源博客平台 Pipe 1.0.0 发布!

    这是 Pipe 博客平台的第一个正式版,欢迎大家使用和反馈建议! 简介 Pipe 是一款小而美的开源博客平台,通过黑客派账号登录即可使用. 动机 产品层面: 市面上缺乏支持多独立博客的平台级系统 实现 ...

  3. Vux使用经验

    Vux使用心得 参考链接 布局 简单平分:水平布局和垂直布局 <template> <divider>Horizontal</divider> <flexbo ...

  4. selenium 获取input输入框中的值的方法

    方法一:获取input的文本值 <input class="form-text-normal" id="txtName" name="Name& ...

  5. D3.js force力导向图用指定的字段确定link的source和target,默认是索引

    json.links.forEach(function (e) { var sourceNode = json.nodes.filter(function (n) { return n.name == ...

  6. 【mongo】centos6.9安装mongo2.6.3

    参考:http://www.haorooms.com/post/3m 注意:centos6上就不要装mongo3了,容易出错. 1. 下载 curl -O http://downloads.mongo ...

  7. rpm 命令使用 和 lsof -p 1406 使用

    #安装RPM -v 显示详细信息 -h 显示进度 -i 安装 -U 升级 -q 查询 -ql 查看rpm 包装的文件 - qf 查看命令属于哪个RPM 包 -qi 查看RPM包的详细信息 [root@ ...

  8. Web的几种上传方式总结

    问题 文件上传在WEB开发中应用很广泛. 文件上传是指将本地图片.视频.音频等文件上传到服务器上,可以供其他用户浏览或下载的过程. 以下总结了常见的文件(图片)上传的方式和要点处理. 表单上传 这是传 ...

  9. 【第一部分】07Leetcode刷题

    二.寻找旋转排序数组中的最小值 题目:153. Find Minimum in Rotated Sorted Array C++ Soution 1: class Solution { public: ...

  10. 基本 TCP 的回射服务器

    实验一 代码:链接[01项目] 1. 先启动服务器,如图: 2. 然后启动客户端,如图: 3. 输出结果: [注意]:在服务器终止时,给父进程发送了一个SIGCHILD信号,这一点本例发生了,但是我们 ...