题目:Sorting It All Out

题意:字母表前n个字母,有m组他们中的大小关系,判断n个字母是否构成唯一序列:

  1.Sorted sequence determined after xxx relations: yyy...y. 
      2.Sorted sequence cannot be determined. 
  3.Inconsistency found after xxx relations.

思路:判断环好判断,但是判断是否唯一就搞不懂了,后来看了下别人的,

    用的是Floyd传递闭包:如果能构成唯一序列,则定点1~n,必定分布着0~n-1的入度,

    才能保证0个入度的定点排在第一位,1个入度的地点排在第2位,以此类推。

    

#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <set> #define c_false ios_base::sync_with_stdio(false); cin.tie(0)
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define zero_(x,y) memset(x , y , sizeof(x))
#define zero(x) memset(x , 0 , sizeof(x))
#define MAX(x) memset(x , 0x3f ,sizeof(x))
#define swa(x,y) {LL s;s=x;x=y;y=s;}
using namespace std ;
#define N 100 const double PI = acos(-1.0);
typedef long long LL ; int topo[N],t;
int G[N][N];
int n,m,flag,pos,i,num;
char s[N];
char output[N]; int Floyd(){
int i, j, k;
for(i = ; i<n ;i++)
for(k = ; k<n; k++)
for(j = ; j<n; j++)
if(G[k][i] && G[i][j])
G[k][j] = ;
for(i = ; i< n; i++){
if(G[i][i] == )
return ;
}
return ;
} int Topsort(){
int cou, ind, k ,i;
int degree[N],used[N];
num = ;
zero(used);
zero(degree);
for(i = ; i< n ;i++)
for(k = ; k< n ;k++)
if(G[i][k])
degree[k]++;
for(i = ; i< n; i++){
cou = ;
for(k = ; k< n ; k++)
if(degree[k] == && !used[k]){
output[num++] = k + 'A';
used[k] = ;
cou++;
ind = k;
}
if(cou > ) return ;
if(cou == ) return ;
for(k = ; k < n;k++)
if(G[ind][k]) degree[k]--;
}
output[num] = '\0';
return ;
}
int main(){
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(cin>>n>>m){
if(n == && m == ) break;
flag = ;
zero(G);
for(i = ;i < m; i++){
scanf("%s",s);
G[s[]-'A'][s[]-'A'] = ;
if(flag != ) continue;
flag = Floyd();
if(flag == )
pos = i;
if(flag == )
flag = Topsort();
else continue;
if(flag == )
pos = i;
}
pos++;
if(flag == ){
printf("Sorted sequence determined after %d relations: %s.\n",pos,output);
continue;
}
if(flag == ){
printf("Inconsistency found after %d relations.\n",pos);
continue;
}
printf("Sorted sequence cannot be determined.\n");
}
return ;
}

拓扑排序 +Floyd(poj 1094)的更多相关文章

  1. 【改了一天的拓扑排序】POJ 1094——Sorting It All Out

    来源:点击打开链接 不知道怎么回事,wa了整整一天..在绝望的时候AC了. 重点是分步处理和三种情况的判断. 1.判断是否成环,成环了直接输出错误信息. 2.然后一条边一条边的加入,进行拓扑排序,如果 ...

  2. 拓扑排序(poj 1094)

    前置知识:拓扑排序 详细注释都在代码里 //该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列. //是典型的拓扑排序,但输出格式上确有三种形式: // 1.该字母序列有序,并依 ...

  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. Sorting It All Out (拓扑排序+floyd)

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

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

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

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

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

  7. POJ 1094 (传递闭包 + 拓扑排序)

    题目链接: POJ 1094 题目大意:有 1 ~ N 个大写字母,且从 A 开始依次 N 个.再给你 M 个小于的关系,比如 A < B ,让你判断三种可能: 1.在第 i 个关系罗列之后,是 ...

  8. POJ 1094 Sorting It All Out (拓扑排序,判断序列是否唯一,图是否有环)

    题意:给出n个字符,m对关系,让你输出三种情况:     1.若到第k行时,能判断出唯一的拓扑序列,则输出:         Sorted sequence determined after k re ...

  9. 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 ...

随机推荐

  1. error BK1506 : cannot open file '.\Debug\????????.sbr': No such file or dire

    http://blog.csdn.net/shuilan0066/article/details/8738035 分类:            调试错误信息2013-03-29 19:08492人阅读 ...

  2. 关于MOD

    同余式:正整数a,b对p取模,它们的余数相同,(a % p)=(b % p) 记做 或者a ≡ b (mod p). 运算规则 模运算与基本四则运算有些相似,但是除法例外.其规则如下:         ...

  3. php rmdir使用递归函数删除非空目录

    我们大家都知道,php rmdir()函数用于删除空目录,但如果要删除非空目录,我们必须将非空目录中的文件或子目录删除,本文章向大家介绍php如何使用递归函数删除非空目录,需要的朋友可以参考一下.首先 ...

  4. crosswalk-webview

    https://github.com/crosswalk-project/cordova-plugin-crosswalk-webview https://cordova.apache.org/doc ...

  5. Create User - mysql

    Create User MariaDB [(none)]> CREATE USER 'DBAdmin'@'%' IDENTIFIED BY 'mypasswd';Query OK, 0 rows ...

  6. LoadRunner 多场景批处理

    @echo off echo *********************************echo ****多场景测试*************echo ******************** ...

  7. Mantis 1.2.19 on Windows Server 2012 r2 datacenter 安装及配置随笔

    一.前言 新的小团队需要搭建一个缺陷管理的工具,之前用过bugfree,感觉比较适合,但是 禅道不太适合,放弃之,于是又百度推荐的: .JTrac13.BugNet14.BugOnline15.eTr ...

  8. 一个CString的实现 拷贝构造函数的应用

    class CString { public: CString (char* s); CString(); ~CString(); private: char *str; int len; stati ...

  9. Egret版本更新(H5增加版本号)

    由于浏览器缓存问题.在服务器上更新了新的图片等资源以后,客户端并不会下载最新的. 关于浏览器缓存机制,可自行百度. Egret中资源更新解决方案有以下: 一 资源名后增加版本号 二 重写Egret引擎 ...

  10. Deep Learning(深度学习)学习笔记整理(二)

    本文整理了网上几位大牛的博客,详细地讲解了CNN的基础结构与核心思想,欢迎交流. [1]Deep learning简介 [2]Deep Learning训练过程 [3]Deep Learning模型之 ...