POJ 1094 Sorting It All Out 【拓扑排序】
<题目链接>
题目大意:
对于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 【拓扑排序】的更多相关文章
- ACM: poj 1094 Sorting It All Out - 拓扑排序
poj 1094 Sorting It All Out Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & ...
- poj 1094 Sorting It All Out (拓扑排序)
http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- [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 ...
- 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 ...
- poj 1094 Sorting It All Out_拓扑排序
题意:是否唯一确定顺序,根据情况输出 #include <iostream> #include<cstdio> #include<cstring> #include ...
- POJ 1094 Sorting It All Out 拓扑排序 难度:0
http://poj.org/problem?id=1094 #include <cstdio> #include <cstring> #include <vector& ...
- PKU 1094 Sorting It All Out(拓扑排序)
题目大意:就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列. 是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序列有序,并依次输出: 2.判断该序列是否唯一: 3.该序列字母次序之间 ...
- POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39602 Accepted: 13 ...
- [ACM] POJ 1094 Sorting It All Out (拓扑排序)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26801 Accepted: 92 ...
- POJ 1094:Sorting It All Out拓扑排序之我在这里挖了一个大大的坑
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29984 Accepted: 10 ...
随机推荐
- iOS -- Effective Objective-C 阅读笔记 (1)
1: 在类的头文件中尽量 少 的引用其他头文件,尽量用 @class xxxxxx; 理解: 当你创建了一个 A 类,这个类又 需要具有 B 类的实例, 你可以直接为 A 类添加 B 类类型的 属性, ...
- Idea-Java接入银联支付的Demo
注:本文来源于:< Idea-Java接入银联支付的Demo > 接入银联支付的Demo,希望能给大家节约一点时间 https://github.com/wangfei0904306/un ...
- Java的动手动脑(五)
日期:2018.11.1 星期四 博客期:021 Part1: 运行代码 class Grandparent { public Grandparent() { System.out.println(& ...
- sql中某条件不为空,可能有的小祖宗会喷了,这还用总结?emmm,我渣,我觉得有一点意思对于第二种(土味)
需求说明:存在父子关系的单表,父级别的parent_id为空,那么要得到所有的子级别的数据信息,查询的条件就是:父id不为空. 个人做法:where parent_id is not null or ...
- 左查询left join on简单总结
应用场景分析:(个人观点,欢迎小祖宗们指正补充) 适合存在父子关系的单表,以及多表的查询 话不多说上代码 代码:mapper里的sql 表名字段什么的本来是单独集中配置的,现在还原到sql中了 & ...
- 【linux】centos6.9通过virtualenv安装python3.5
参考:http://www.linuxidc.com/Linux/2015-08/121352.htm wget https://www.python.org/ftp/python/3.5.4/Pyt ...
- HTML&javaSkcript&CSS&jQuery&ajax(四)
一.HTML创建响应设计 Responsive Web Design 可以改变尺寸传递网页,对于平板和移动设备是必须的 1.<!DOCTYPE html><html lang=&qu ...
- js cookie 工具
var CookieUtil = { get: function(name) { var cookieName = encodeURIComponent(name) + "=", ...
- 步步为营-77-Ajax简介
AJax:异步JavaScript和XML.Asynchronous JavaScript and XML 优点:无刷新 1 JavaScript下的Ajax 1.1 XMLHttpRequest对象 ...
- python文件的分类
# 0.获取所有的文件名称列表import os import shutilos.chdir("files")file_list = os.listdir("./&quo ...