题目: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. mybatis中当实体类的字段名和表结构中的字段名不一致的时候的处理

    1.在sql语句中使用列的别名 比如:select order_id id,orderNo orderno ,order_price price from order where order_id = ...

  2. [计算机取证技术] VDI-in-a-Box Analysis Results

    原文跳转: http://dig4n6.blogspot.tw/2013/07/vdi-in-box-analysis-results.html *文中引用图片如无法浏览,请科学上网* VDI-in- ...

  3. C#字符操作

    //字符串转ASCII码 // str1:字符串 str2:ASCII码 ] })[] == )//判断输入是否为字母 { str2= Encoding.GetEncoding(].ToString( ...

  4. The default for KeyValuePair

    if (getResult.Equals(new KeyValuePair<T,U>())) or this: if (getResult.Equals(default(KeyValueP ...

  5. 重置SQL Server连接池

    EXEC sp_configure 'show advanced options', 1 GO -- To update the currently configured value for adva ...

  6. Flex 加载tiff

    gis系统常常要加载tiff,因为好多土地证书,各种文件都是扫描件,如果你是用as来写的前台,怎么加载呢,顺便说下用插件AlternaTIFF也是可以得不过浏览器加载这么多插件是不太好的. 首先TIF ...

  7. sqlserver同步后在不重新初始化快照的情况下新增表

           在已有事务复制中,时长需要新增表.索引,这些变更时不会同步到从库中.如果采用默认的设置,每次都需要重新初始化快照,从库重新应用快照和未执行的同步命令,这显然是无法在线上实践的方法.另一种 ...

  8. 开源文档管理工具Joomla的网站安装

    1.配置PHP开发环境(Apache.PHP.MySQL) 2.安装Joomla网站: 1. 下载安装包     http://www.joomla.org/download.html 2. 登陆Jo ...

  9. linux基础知识(四)

    •查看ip地址,ifconfig命令 •重启.启动.停止网络命令 •service network restart/start/stop   •VMnet0:用于虚拟桥接网络下的虚拟交换机 •VMne ...

  10. C++Builder Berlin 编译

    编译工程,一个单元有错误,就停下了, 能不能把所有单元都编译,一次处理所有单元的错误?