题目: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. javaSwing文本域文件

    public class JTextAreaTest extends JFrame{    public JTextAreaTest()    {            setSize(200, 40 ...

  2. 继承自NSObject的不常用又很有用的函数(2)

    函数调用 Objective-C是一门动态语言,一个函数是由一个selector(SEL),和一个implement(IML)组成的.Selector相当于门牌号,而Implement才是真正的住户( ...

  3. 树莓派make 360wifi2报错

    输入make命令后报错 make: *** /lib/modules/3.10.25+/build: No such file or directory. Stop. 系统缺少编译模块所需要的内核头文 ...

  4. ZC706以太网扩展板接口

    端口 BANK-VADJ PIN NAME PIN_NAME RGMII0 rgmii_0_txc 10 AB14 LA15_N PHY_0_GTXCLK rgmii_0_rxc 10 AE13 LA ...

  5. 利用 Gulp 处理前端工作流程

    最近做项目,因为每次做完后都要手动压缩CSS.JS 等文件,压缩后另存为 *.min.xxx. Less 还要手动输入命令进行编译,调整页面也经常要手动刷新页面看效果,很麻烦,就尝试用 gulp 去处 ...

  6. 第23章 排序算法(包括merge等)

      第23章 排序算法  Sorting:1 sort Sort elements in range (function template)2 stable_sort Sort elements pr ...

  7. XGBoost参数调优完全指南(附Python代码)

    XGBoost参数调优完全指南(附Python代码):http://www.2cto.com/kf/201607/528771.html https://www.zhihu.com/question/ ...

  8. Extjs学习笔记--Ext.tree.Panel

    Ext.create('Ext.tree.Panel', { title: 'Simple Tree', width: 200, height: 150, store: store, rootVisi ...

  9. POJ1201 区间

    题目大意: 给定n个整数区间[ai,bi]和n个整数ci,求一个最小集合Z,满足|Z∩[ai,bi]|>=ci(Z里边在闭区间[ai,bi]的个数不小于ci). 多组数据: n(1<=n& ...

  10. 树莓派(raspberry pi)学习4: 更改键盘布局(转)

    树莓派(raspberry pi)用了几次后,发现键盘老是按错,一些字符打不出来或打错 这个问题,折腾我半天.还是把心得分享一下吧 上网查,发现是键盘布局不对,树莓派(raspberry pi)是英国 ...